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

fix: route matching when regex ends with wildcard #933

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 24 additions & 5 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,17 +438,30 @@ func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
if p < 0 {
if xn.tail == '/' {
p = len(xsearch)
} else {
} else if xn.tail != '*' {
continue
}
} else if ntyp == ntRegexp && p == 0 {
continue
}

if ntyp == ntRegexp && xn.rex != nil {
if !xn.rex.MatchString(xsearch[:p]) {
// check if the pattern ends with wildcard, like {id}*
if xn.tail == '*' {
matchedString := xn.rex.FindString(xsearch)
if matchedString == "" {
continue
}
p = len(matchedString)
} else if !xn.rex.MatchString(xsearch[:p]) {
continue
}
} else if ntyp == ntParam && xn.tail == '*' {
// p will until / is encountered
p = strings.IndexByte(xsearch, '/')
if p == -1 {
p = len(xsearch)
}
} else if strings.IndexByte(xsearch[:p], '/') != -1 {
// avoid a match across path segments
continue
Expand Down Expand Up @@ -741,7 +754,13 @@ func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
if rexpat[0] != '^' {
rexpat = "^" + rexpat
}
if rexpat[len(rexpat)-1] != '$' {

if tail == '*' && rexpat[len(rexpat)-1] == '$' {
panic("chi: invalid pattern, wildcard '*' is not allowed if the regex just before ends with $")
}

// don't add end marker if the tail is '*' as that will make the wildcard invalid during matching
if rexpat[len(rexpat)-1] != '$' && tail != '*' {
rexpat += "$"
}
}
Expand Down Expand Up @@ -807,11 +826,11 @@ func (ns nodes) Len() int { return len(ns) }
func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }

// tailSort pushes nodes with '/' as the tail to the end of the list for param nodes.
// tailSort pushes nodes with '/' or '*' as the tail to the end of the list for param nodes.
// The list order determines the traversal order.
func (ns nodes) tailSort() {
for i := len(ns) - 1; i >= 0; i-- {
if ns[i].typ > ntStatic && ns[i].tail == '/' {
if ns[i].typ > ntStatic && (ns[i].tail == '/' || ns[i].tail == '*') {
ns.Swap(i, len(ns)-1)
return
}
Expand Down
17 changes: 17 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func TestTree(t *testing.T) {
hAdminCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppShowCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppPaymentsShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppPaymentsShowCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppUsersShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hAdminAppUsersShowCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserProfile := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserSuper := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
hUserAll := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
Expand Down Expand Up @@ -63,6 +67,11 @@ func TestTree(t *testing.T) {
tr.InsertRoute(mGET, "/admin/apps/{id}", hAdminAppShow)
tr.InsertRoute(mGET, "/admin/apps/{id}/*", hAdminAppShowCatchall)

tr.InsertRoute(mGET, "/admin/apps/payments/{id:[0-9]+}/status", hAdminAppPaymentsShow)
tr.InsertRoute(mGET, "/admin/apps/payments/{id:[0-9]+}*", hAdminAppPaymentsShowCatchall)
tr.InsertRoute(mGET, "/admin/apps/users/{id}/status", hAdminAppUsersShow)
tr.InsertRoute(mGET, "/admin/apps/users/{id}*", hAdminAppUsersShowCatchall)

tr.InsertRoute(mGET, "/admin/*", hStub) // catchall segment will get replaced by next route
tr.InsertRoute(mGET, "/admin/*", hAdminCatchall)

Expand Down Expand Up @@ -110,6 +119,14 @@ func TestTree(t *testing.T) {
{r: "/admin/lots/of/:fun", h: hAdminCatchall, k: []string{"*"}, v: []string{"lots/of/:fun"}},
{r: "/admin/apps/333", h: hAdminAppShow, k: []string{"id"}, v: []string{"333"}},
{r: "/admin/apps/333/woot", h: hAdminAppShowCatchall, k: []string{"id", "*"}, v: []string{"333", "woot"}},
{r: "/admin/apps/payments/333/status", h: hAdminAppPaymentsShow, k: []string{"id"}, v: []string{"333"}},
{r: "/admin/apps/payments/333/woot", h: hAdminAppPaymentsShowCatchall, k: []string{"id", "*"}, v: []string{"333", "/woot"}},
{r: "/admin/apps/payments/333/", h: hAdminAppPaymentsShowCatchall, k: []string{"id", "*"}, v: []string{"333", "/"}},
{r: "/admin/apps/payments/333", h: hAdminAppPaymentsShowCatchall, k: []string{"id", "*"}, v: []string{"333", ""}},
{r: "/admin/apps/users/test_user/status", h: hAdminAppUsersShow, k: []string{"id"}, v: []string{"test_user"}},
{r: "/admin/apps/users/test_user/not_status", h: hAdminAppUsersShowCatchall, k: []string{"id", "*"}, v: []string{"test_user", "/not_status"}},
{r: "/admin/apps/users/test_user/", h: hAdminAppUsersShowCatchall, k: []string{"id", "*"}, v: []string{"test_user", "/"}},
{r: "/admin/apps/users/test_user", h: hAdminAppUsersShowCatchall, k: []string{"id", "*"}, v: []string{"test_user", ""}},

{r: "/hubs/123/view", h: hHubView1, k: []string{"hubID"}, v: []string{"123"}},
{r: "/hubs/123/view/index.html", h: hHubView2, k: []string{"hubID", "*"}, v: []string{"123", "index.html"}},
Expand Down