@marcel91200 / caddy Mirror · read-only / commits / 6d0350d0
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: Fix host matching when host has a port

Matthew Holt committed Jun 20, 2019 at 20:24 UTC 6d0350d04ecd7074a65d57f03bd721e75537c13d
2 files changed +25 -13
modules/caddyhttp/matchers.go
+20 -13
@@ -113,11 +113,17 @@ func init() {
113
114 // Match returns true if r matches m.
115 func (m MatchHost) Match(r *http.Request) bool {
116 + reqHost, _, err := net.SplitHostPort(r.Host)
117 + if err != nil {
118 + // OK; probably didn't have a port
119 + reqHost = r.Host
120 + }
121 +
122 outer:
123 for _, host := range m {
124 if strings.Contains(host, "*") {
125 patternParts := strings.Split(host, ".")
120 - incomingParts := strings.Split(r.Host, ".")
126 + incomingParts := strings.Split(reqHost, ".")
127 if len(patternParts) != len(incomingParts) {
128 continue
129 }
@@ -130,10 +136,11 @@ outer:
136 }
137 }
138 return true
133 - } else if strings.EqualFold(r.Host, host) {
139 + } else if strings.EqualFold(reqHost, host) {
140 return true
141 }
142 }
143 +
144 return false
145 }
146
@@ -469,17 +476,17 @@ var wordRE = regexp.MustCompile(`\w+`)
476
477 // Interface guards
478 var (
472 - _ RequestMatcher = (*MatchHost)(nil)
473 - _ RequestMatcher = (*MatchPath)(nil)
474 - _ RequestMatcher = (*MatchPathRE)(nil)
475 - _ RequestMatcher = (*MatchMethod)(nil)
476 - _ RequestMatcher = (*MatchQuery)(nil)
477 - _ RequestMatcher = (*MatchHeader)(nil)
478 - _ RequestMatcher = (*MatchHeaderRE)(nil)
479 - _ RequestMatcher = (*MatchProtocol)(nil)
480 - _ RequestMatcher = (*MatchRemoteIP)(nil)
479 + _ RequestMatcher = (*MatchHost)(nil)
480 + _ RequestMatcher = (*MatchPath)(nil)
481 + _ RequestMatcher = (*MatchPathRE)(nil)
482 + _ RequestMatcher = (*MatchMethod)(nil)
483 + _ RequestMatcher = (*MatchQuery)(nil)
484 + _ RequestMatcher = (*MatchHeader)(nil)
485 + _ RequestMatcher = (*MatchHeaderRE)(nil)
486 + _ RequestMatcher = (*MatchProtocol)(nil)
487 + _ RequestMatcher = (*MatchRemoteIP)(nil)
488 _ caddy.Provisioner = (*MatchRemoteIP)(nil)
482 - _ RequestMatcher = (*MatchNegate)(nil)
489 + _ RequestMatcher = (*MatchNegate)(nil)
490 _ caddy.Provisioner = (*MatchNegate)(nil)
484 - _ RequestMatcher = (*MatchStarlarkExpr)(nil)
491 + _ RequestMatcher = (*MatchStarlarkExpr)(nil)
492 )
modules/caddyhttp/matchers_test.go
+5
@@ -87,6 +87,11 @@ func TestHostMatcher(t *testing.T) {
87 input: "sub.foo.example.net",
88 expect: false,
89 },
90 + {
91 + match: MatchHost{"example.com"},
92 + input: "example.com:5555",
93 + expect: true,
94 + },
95 } {
96 req := &http.Request{Host: tc.input}
97 actual := tc.match.Match(req)