From 4e1d6b4f1ec90426a3c80907fd41ff8f6e447a4c Mon Sep 17 00:00:00 2001 From: I am goroot Date: Fri, 12 Jul 2024 17:30:28 +0200 Subject: [PATCH 1/2] fix: Unclear behaviour of * in routes --- router.go | 16 ++++++++++++++++ router_test.go | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/router.go b/router.go index 49b56966d..db6005414 100644 --- a/router.go +++ b/router.go @@ -692,6 +692,22 @@ func (r *Router) Find(method, path string, c Context) { // update indexes/search in case we need to backtrack when no handler match is found paramIndex++ + if !currentNode.isLeaf { + i := 0 + l := len(search) + + for ; i < l && search[i] != '/'; i++ { + } + search = search[i:] + searchIndex = searchIndex + len(search) + previousBestMatchNode = currentNode + if len(search) != 0 { + if child := currentNode.findStaticChild(search[0]); child != nil { + currentNode = child + continue + } + } + } searchIndex += +len(search) search = "" diff --git a/router_test.go b/router_test.go index dca0d47bc..7a5ca2010 100644 --- a/router_test.go +++ b/router_test.go @@ -1579,13 +1579,19 @@ func TestRouterAnyMatchesLastAddedAnyRoute(t *testing.T) { assert.Equal(t, "/users/*/action*", c.Get("path")) assert.Equal(t, "xxx/action/sea", c.Param("*")) - // if we add another route then it is the last added and so it is matched r.Add(http.MethodGet, "/users/*/action/search", handlerHelper("case", 3)) + // should not match incomplete route with wildcard but match previous one r.Find(http.MethodGet, "/users/xxx/action/sea", c) c.handler(c) - assert.Equal(t, "/users/*/action/search", c.Get("path")) + assert.Equal(t, "/users/*/action*", c.Get("path")) assert.Equal(t, "xxx/action/sea", c.Param("*")) + + // should match route with wildcard and static suffix + r.Find(http.MethodGet, "/users/xxx/action/search", c) + c.handler(c) + assert.Equal(t, "/users/*/action/search", c.Get("path")) + assert.Equal(t, "xxx/action/search", c.Param("*")) } // Issue #1739 From 2ad2d151efee610117a6335bf2dabd2d0cab796a Mon Sep 17 00:00:00 2001 From: I am goroot Date: Fri, 12 Jul 2024 22:16:30 +0200 Subject: [PATCH 2/2] fix array out of bounds in some cases --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index db6005414..969ed8071 100644 --- a/router.go +++ b/router.go @@ -699,10 +699,10 @@ func (r *Router) Find(method, path string, c Context) { for ; i < l && search[i] != '/'; i++ { } search = search[i:] - searchIndex = searchIndex + len(search) previousBestMatchNode = currentNode if len(search) != 0 { if child := currentNode.findStaticChild(search[0]); child != nil { + searchIndex = searchIndex + len(child.prefix) currentNode = child continue }