From bc9f9448370d7a5535b33e7407760998b4875e45 Mon Sep 17 00:00:00 2001
From: Matthew Holt <mholt@users.noreply.github.com>
Date: Wed, 18 Sep 2019 09:45:21 -0600
Subject: [PATCH] host matcher: Strip [ ] from IPv6 addresses

---
 caddyconfig/httpcaddyfile/httptype.go | 2 +-
 modules/caddyhttp/matchers.go         | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 32791e90a..4588b8fde 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -268,7 +268,7 @@ func (st *ServerType) serversFromPairings(
 			}
 			if _, ok := sblock.pile["tls.off"]; ok {
 				// tls off: disable TLS (and automatic HTTPS) for server block's names
-				if srv.AutoHTTPS == nil {
+				if srv.AutoHTTPS == nil && len(autoHTTPSQualifiedHosts) > 0 {
 					srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
 				}
 				srv.AutoHTTPS.Skip = append(srv.AutoHTTPS.Skip, autoHTTPSQualifiedHosts...)
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index 9cf52dcc3..3eaf48f5c 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -112,6 +112,10 @@ func (m MatchHost) Match(r *http.Request) bool {
 	if err != nil {
 		// OK; probably didn't have a port
 		reqHost = r.Host
+
+		// make sure we strip the brackets from IPv6 addresses
+		reqHost = strings.TrimPrefix(reqHost, "[")
+		reqHost = strings.TrimSuffix(reqHost, "]")
 	}
 
 outer: