From 32cafbb6309c8d78cc7e2f2a75def9c633944ef8 Mon Sep 17 00:00:00 2001
From: Matthew Holt <mholt@users.noreply.github.com>
Date: Tue, 16 Jun 2020 10:02:06 -0600
Subject: [PATCH] httpcaddyfile: Fix ordering of catch-all site blocks

Catch-alls should always go last. Normally this is the case, but we have
a special case for comparing one wildcard-host site block to another
non-wildcard host site block; and a catch-all site block is also a
non-wildcard host site block, so now we have to special-case the
catch-all site block. Sigh.

This could be reproduced with a Caddyfile that has two site blocks:
":80" and "*.example.com", in that order.
---
 caddyconfig/httpcaddyfile/httptype.go | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 7be932fdf..3f37f0242 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -400,6 +400,11 @@ func (st *ServerType) serversFromPairings(
 					jLongestPath = addr.Path
 				}
 			}
+			if specificity(jLongestHost) == 0 {
+				// catch-all blocks (blocks with no hostname) should always go
+				// last, even after blocks with wildcard hosts
+				return true
+			}
 			if iWildcardHost != jWildcardHost {
 				// site blocks that have a key with a wildcard in the hostname
 				// must always be less specific than blocks without one; see