Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new & improved glob matcher #457

Merged
merged 10 commits into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
return nil, fmt.Errorf("invalid proxy.strategy: %s", cfg.Proxy.Strategy)
}

if cfg.Proxy.Matcher != "prefix" && cfg.Proxy.Matcher != "glob" {
if cfg.Proxy.Matcher != "prefix" && cfg.Proxy.Matcher != "glob" && cfg.Proxy.Matcher != "gobwas/glob" {
return nil, fmt.Errorf("invalid proxy.matcher: %s", cfg.Proxy.Matcher)
}

Expand Down
14 changes: 12 additions & 2 deletions route/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"log"
"path"
"strings"

"github.com/gobwas/glob"
)

// matcher determines whether a host/path matches a route
Expand All @@ -12,8 +14,9 @@ type matcher func(uri string, r *Route) bool
// Matcher contains the available matcher functions.
// Update config/load.go#load after updating.
var Matcher = map[string]matcher{
"prefix": prefixMatcher,
"glob": globMatcher,
"prefix": prefixMatcher,
"glob": globMatcher,
"gobwas/glob": gobwasGlobMatcher,
}

// prefixMatcher matches path to the routes' path.
Expand All @@ -30,3 +33,10 @@ func globMatcher(uri string, r *Route) bool {
}
return hasMatch
}


// gobwasGlobMatcher matches path to the routes' path using gobwas/glob.
func gobwasGlobMatcher(uri string, r *Route) bool {
var g = glob.MustCompile(r.Path)
return g.Match(uri)
}
33 changes: 33 additions & 0 deletions route/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ func TestGlobMatcher(t *testing.T) {
})
}
}

func TestGobwasGlobMatcher(t *testing.T) {
tests := []struct {
uri string
matches bool
route *Route
}{
// happy flows
{uri: "/foo", matches: true, route: &Route{Path: "/foo"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo?"}},
{uri: "/fool", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: true, route: &Route{Path: "/foo*"}},
{uri: "/foo/x/bar", matches: true, route: &Route{Path: "/foo/*/bar"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**"}},
{uri: "/foo/x/y/z/w/bar", matches: true, route: &Route{Path: "/foo/**/bar"}},

// error flows
{uri: "/fo", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo"}},
{uri: "/fo", matches: false, route: &Route{Path: "/foo*"}},
{uri: "/fools", matches: false, route: &Route{Path: "/foo.*"}},
{uri: "/foo/x/y/z/w/baz", matches: false, route: &Route{Path: "/foo/**/bar"}},
}

for _, tt := range tests {
t.Run(tt.uri, func(t *testing.T) {
if got, want := gobwasGlobMatcher(tt.uri, tt.route), tt.matches; got != want {
t.Fatalf("got %v want %v", got, want)
}
})
}
}
21 changes: 21 additions & 0 deletions vendor/github.com/gobwas/glob/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/gobwas/glob/bench.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading