0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-02-17 23:45:41 -05:00

caddyhttp: Fix host matching when host has a port

This commit is contained in:
Matthew Holt 2019-06-20 20:24:46 -06:00
parent 15647bdfb7
commit 6d0350d04e
2 changed files with 25 additions and 13 deletions

View file

@ -113,11 +113,17 @@ func init() {
// Match returns true if r matches m. // Match returns true if r matches m.
func (m MatchHost) Match(r *http.Request) bool { 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: outer:
for _, host := range m { for _, host := range m {
if strings.Contains(host, "*") { if strings.Contains(host, "*") {
patternParts := strings.Split(host, ".") patternParts := strings.Split(host, ".")
incomingParts := strings.Split(r.Host, ".") incomingParts := strings.Split(reqHost, ".")
if len(patternParts) != len(incomingParts) { if len(patternParts) != len(incomingParts) {
continue continue
} }
@ -130,10 +136,11 @@ outer:
} }
} }
return true return true
} else if strings.EqualFold(r.Host, host) { } else if strings.EqualFold(reqHost, host) {
return true return true
} }
} }
return false return false
} }
@ -469,17 +476,17 @@ var wordRE = regexp.MustCompile(`\w+`)
// Interface guards // Interface guards
var ( var (
_ RequestMatcher = (*MatchHost)(nil) _ RequestMatcher = (*MatchHost)(nil)
_ RequestMatcher = (*MatchPath)(nil) _ RequestMatcher = (*MatchPath)(nil)
_ RequestMatcher = (*MatchPathRE)(nil) _ RequestMatcher = (*MatchPathRE)(nil)
_ RequestMatcher = (*MatchMethod)(nil) _ RequestMatcher = (*MatchMethod)(nil)
_ RequestMatcher = (*MatchQuery)(nil) _ RequestMatcher = (*MatchQuery)(nil)
_ RequestMatcher = (*MatchHeader)(nil) _ RequestMatcher = (*MatchHeader)(nil)
_ RequestMatcher = (*MatchHeaderRE)(nil) _ RequestMatcher = (*MatchHeaderRE)(nil)
_ RequestMatcher = (*MatchProtocol)(nil) _ RequestMatcher = (*MatchProtocol)(nil)
_ RequestMatcher = (*MatchRemoteIP)(nil) _ RequestMatcher = (*MatchRemoteIP)(nil)
_ caddy.Provisioner = (*MatchRemoteIP)(nil) _ caddy.Provisioner = (*MatchRemoteIP)(nil)
_ RequestMatcher = (*MatchNegate)(nil) _ RequestMatcher = (*MatchNegate)(nil)
_ caddy.Provisioner = (*MatchNegate)(nil) _ caddy.Provisioner = (*MatchNegate)(nil)
_ RequestMatcher = (*MatchStarlarkExpr)(nil) _ RequestMatcher = (*MatchStarlarkExpr)(nil)
) )

View file

@ -87,6 +87,11 @@ func TestHostMatcher(t *testing.T) {
input: "sub.foo.example.net", input: "sub.foo.example.net",
expect: false, expect: false,
}, },
{
match: MatchHost{"example.com"},
input: "example.com:5555",
expect: true,
},
} { } {
req := &http.Request{Host: tc.input} req := &http.Request{Host: tc.input}
actual := tc.match.Match(req) actual := tc.match.Match(req)