Browse Source
caddyhttp: Support path matcher of "*" without panic
master
Matthew Holt
5 years ago
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
2 changed files with
29 additions and
1 deletions
-
modules/caddyhttp/matchers.go
-
modules/caddyhttp/matchers_test.go
|
|
@ -211,9 +211,17 @@ func (m MatchPath) Match(r *http.Request) bool { |
|
|
|
for _, matchPath := range m { |
|
|
|
matchPath = repl.ReplaceAll(matchPath, "") |
|
|
|
|
|
|
|
// special case: whole path is wildcard; this is unnecessary
|
|
|
|
// as it matches all requests, which is the same as no matcher
|
|
|
|
if matchPath == "*" { |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
// special case: first and last characters are wildcard,
|
|
|
|
// treat it as a fast substring match
|
|
|
|
if strings.HasPrefix(matchPath, "*") && strings.HasSuffix(matchPath, "*") { |
|
|
|
if len(matchPath) > 1 && |
|
|
|
strings.HasPrefix(matchPath, "*") && |
|
|
|
strings.HasSuffix(matchPath, "*") { |
|
|
|
if strings.Contains(lowerPath, matchPath[1:len(matchPath)-1]) { |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
@ -252,6 +252,26 @@ func TestPathMatcher(t *testing.T) { |
|
|
|
input: "/foo/BAR.txt", |
|
|
|
expect: true, |
|
|
|
}, |
|
|
|
{ |
|
|
|
match: MatchPath{"*"}, |
|
|
|
input: "/", |
|
|
|
expect: true, |
|
|
|
}, |
|
|
|
{ |
|
|
|
match: MatchPath{"*"}, |
|
|
|
input: "/foo/bar", |
|
|
|
expect: true, |
|
|
|
}, |
|
|
|
{ |
|
|
|
match: MatchPath{"**"}, |
|
|
|
input: "/", |
|
|
|
expect: true, |
|
|
|
}, |
|
|
|
{ |
|
|
|
match: MatchPath{"**"}, |
|
|
|
input: "/foo/bar", |
|
|
|
expect: true, |
|
|
|
}, |
|
|
|
} { |
|
|
|
req := &http.Request{URL: &url.URL{Path: tc.input}} |
|
|
|
repl := caddy.NewReplacer() |
|
|
|