@marcel91200 / caddy Mirror · read-only / commits / b62f8e05
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: Support path matcher of "*" without panic

Matthew Holt committed Mar 16, 2020 at 16:08 UTC b62f8e058270aff37621b69a9768235a01e8bd34
2 files changed +29 -1
modules/caddyhttp/matchers.go
+9 -1
@@ -211,9 +211,17 @@ func (m MatchPath) Match(r *http.Request) bool {
211 for _, matchPath := range m {
212 matchPath = repl.ReplaceAll(matchPath, "")
213
214 + // special case: whole path is wildcard; this is unnecessary
215 + // as it matches all requests, which is the same as no matcher
216 + if matchPath == "*" {
217 + return true
218 + }
219 +
220 // special case: first and last characters are wildcard,
221 // treat it as a fast substring match
216 - if strings.HasPrefix(matchPath, "*") && strings.HasSuffix(matchPath, "*") {
222 + if len(matchPath) > 1 &&
223 + strings.HasPrefix(matchPath, "*") &&
224 + strings.HasSuffix(matchPath, "*") {
225 if strings.Contains(lowerPath, matchPath[1:len(matchPath)-1]) {
226 return true
227 }
modules/caddyhttp/matchers_test.go
+20
@@ -252,6 +252,26 @@ func TestPathMatcher(t *testing.T) {
252 input: "/foo/BAR.txt",
253 expect: true,
254 },
255 + {
256 + match: MatchPath{"*"},
257 + input: "/",
258 + expect: true,
259 + },
260 + {
261 + match: MatchPath{"*"},
262 + input: "/foo/bar",
263 + expect: true,
264 + },
265 + {
266 + match: MatchPath{"**"},
267 + input: "/",
268 + expect: true,
269 + },
270 + {
271 + match: MatchPath{"**"},
272 + input: "/foo/bar",
273 + expect: true,
274 + },
275 } {
276 req := &http.Request{URL: &url.URL{Path: tc.input}}
277 repl := caddy.NewReplacer()