From f2491580e041d1f012bcf4d5df1dc851c1d8fef1 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 2 Feb 2019 14:28:08 -0700 Subject: [PATCH] httpserver: Fix address display and routing for IPv6 IPs/wildcards --- caddyhttp/httpserver/plugin.go | 5 +++-- caddyhttp/httpserver/vhosttrie.go | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go index 3015476b6..a62baf5d5 100644 --- a/caddyhttp/httpserver/plugin.go +++ b/caddyhttp/httpserver/plugin.go @@ -428,11 +428,12 @@ func (a Address) String() string { if s != "" { s += "://" } - s += a.Host if a.Port != "" && ((scheme == "https" && a.Port != DefaultHTTPSPort) || (scheme == "http" && a.Port != DefaultHTTPPort)) { - s += ":" + a.Port + s += net.JoinHostPort(a.Host, a.Port) + } else { + s += a.Host } if a.Path != "" { s += a.Path diff --git a/caddyhttp/httpserver/vhosttrie.go b/caddyhttp/httpserver/vhosttrie.go index ecf47d7e1..ee374431e 100644 --- a/caddyhttp/httpserver/vhosttrie.go +++ b/caddyhttp/httpserver/vhosttrie.go @@ -32,7 +32,12 @@ type vhostTrie struct { // newVHostTrie returns a new vhostTrie. func newVHostTrie() *vhostTrie { - return &vhostTrie{edges: make(map[string]*vhostTrie), fallbackHosts: []string{"0.0.0.0", ""}} + // TODO: fallbackHosts doesn't discriminate between network interfaces; + // i.e. if there is a host "0.0.0.0", it could match a request coming + // in to "[::1]" (and vice-versa) even though the IP versions differ. + // This might be OK, or maybe it's not desirable. The 'bind' directive + // can be used to restrict what interface a listener binds to. + return &vhostTrie{edges: make(map[string]*vhostTrie), fallbackHosts: []string{"0.0.0.0", "[::]", ""}} } // Insert adds stack to t keyed by key. The key should be