http: path matcher supports exact matching with = prefix

This commit is contained in:
Matthew Holt 2019-11-28 21:11:45 -07:00
parent 512b004332
commit 14d3fd7d03
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 33 additions and 4 deletions

View file

@ -166,16 +166,30 @@ func (m MatchPath) Provision(_ caddy.Context) error {
func (m MatchPath) Match(r *http.Request) bool { func (m MatchPath) Match(r *http.Request) bool {
lowerPath := strings.ToLower(r.URL.Path) lowerPath := strings.ToLower(r.URL.Path)
for _, matchPath := range m { for _, matchPath := range m {
// as a special case, if the first character is a // special case: first character is equals sign,
// wildcard, treat it as a quick suffix match // treat it as an exact match
if strings.HasPrefix(matchPath, "*") { if strings.HasPrefix(matchPath, "=") {
return strings.HasSuffix(lowerPath, matchPath[1:]) if lowerPath == matchPath[1:] {
return true
}
continue
} }
// special case: first character is a wildcard,
// treat it as a fast suffix match
if strings.HasPrefix(matchPath, "*") {
if strings.HasSuffix(lowerPath, matchPath[1:]) {
return true
}
continue
}
// can ignore error here because we can't handle it anyway // can ignore error here because we can't handle it anyway
matches, _ := filepath.Match(matchPath, lowerPath) matches, _ := filepath.Match(matchPath, lowerPath)
if matches { if matches {
return true return true
} }
if strings.HasPrefix(lowerPath, matchPath) { if strings.HasPrefix(lowerPath, matchPath) {
return true return true
} }

View file

@ -212,6 +212,21 @@ func TestPathMatcher(t *testing.T) {
input: "/foo/bar/bam", input: "/foo/bar/bam",
expect: false, expect: false,
}, },
{
match: MatchPath{"=/foo"},
input: "/foo",
expect: true,
},
{
match: MatchPath{"=/foo"},
input: "/foo/bar",
expect: false,
},
{
match: MatchPath{"=/foo"},
input: "/FOO",
expect: true,
},
{ {
match: MatchPath{"/foo"}, match: MatchPath{"/foo"},
input: "/FOO", input: "/FOO",