diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-06-20 20:24:46 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-06-20 20:24:46 -0600 |
commit | 6d0350d04ecd7074a65d57f03bd721e75537c13d (patch) | |
tree | ace5815f5938f92cd87ff4bbd396b604bed3084a /modules | |
parent | 15647bdfb757ab7b1d1c77446a0707e49f31deb7 (diff) |
caddyhttp: Fix host matching when host has a port
Diffstat (limited to 'modules')
-rw-r--r-- | modules/caddyhttp/matchers.go | 33 | ||||
-rw-r--r-- | modules/caddyhttp/matchers_test.go | 5 |
2 files changed, 25 insertions, 13 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 86a2c69..2395e13 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -113,11 +113,17 @@ func init() { // Match returns true if r matches m. func (m MatchHost) Match(r *http.Request) bool { + reqHost, _, err := net.SplitHostPort(r.Host) + if err != nil { + // OK; probably didn't have a port + reqHost = r.Host + } + outer: for _, host := range m { if strings.Contains(host, "*") { patternParts := strings.Split(host, ".") - incomingParts := strings.Split(r.Host, ".") + incomingParts := strings.Split(reqHost, ".") if len(patternParts) != len(incomingParts) { continue } @@ -130,10 +136,11 @@ outer: } } return true - } else if strings.EqualFold(r.Host, host) { + } else if strings.EqualFold(reqHost, host) { return true } } + return false } @@ -469,17 +476,17 @@ var wordRE = regexp.MustCompile(`\w+`) // Interface guards var ( - _ RequestMatcher = (*MatchHost)(nil) - _ RequestMatcher = (*MatchPath)(nil) - _ RequestMatcher = (*MatchPathRE)(nil) - _ RequestMatcher = (*MatchMethod)(nil) - _ RequestMatcher = (*MatchQuery)(nil) - _ RequestMatcher = (*MatchHeader)(nil) - _ RequestMatcher = (*MatchHeaderRE)(nil) - _ RequestMatcher = (*MatchProtocol)(nil) - _ RequestMatcher = (*MatchRemoteIP)(nil) + _ RequestMatcher = (*MatchHost)(nil) + _ RequestMatcher = (*MatchPath)(nil) + _ RequestMatcher = (*MatchPathRE)(nil) + _ RequestMatcher = (*MatchMethod)(nil) + _ RequestMatcher = (*MatchQuery)(nil) + _ RequestMatcher = (*MatchHeader)(nil) + _ RequestMatcher = (*MatchHeaderRE)(nil) + _ RequestMatcher = (*MatchProtocol)(nil) + _ RequestMatcher = (*MatchRemoteIP)(nil) _ caddy.Provisioner = (*MatchRemoteIP)(nil) - _ RequestMatcher = (*MatchNegate)(nil) + _ RequestMatcher = (*MatchNegate)(nil) _ caddy.Provisioner = (*MatchNegate)(nil) - _ RequestMatcher = (*MatchStarlarkExpr)(nil) + _ RequestMatcher = (*MatchStarlarkExpr)(nil) ) diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go index 08ab156..597d217 100644 --- a/modules/caddyhttp/matchers_test.go +++ b/modules/caddyhttp/matchers_test.go @@ -87,6 +87,11 @@ func TestHostMatcher(t *testing.T) { input: "sub.foo.example.net", expect: false, }, + { + match: MatchHost{"example.com"}, + input: "example.com:5555", + expect: true, + }, } { req := &http.Request{Host: tc.input} actual := tc.match.Match(req) |