From ffd91cfdc4eb91b29b36c2702e135793e24f37b4 Mon Sep 17 00:00:00 2001 From: Adam Magued Date: Wed, 14 Oct 2026 01:55:19 +0000 Subject: [PATCH 1/4] Fix named route matching when static route with trailing slash is registered --- gin_test.go | 70 ++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 + tree.go | 140 +++++++++++++++++++++++++++++++++------------------ tree_test.go | 75 +++++++++++++++++++++++++++ 5 files changed, 239 insertions(+), 50 deletions(-) diff --git a/gin_test.go b/gin_test.go index 71a817a7..7c0f934e 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1195,3 +1195,73 @@ func TestRouterQueryMethod(t *testing.T) { router.ServeHTTP(w2, req2) assert.Equal(t, http.StatusNotFound, w2.Code) } + +func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { + for _, redirectTrailingSlash := range []bool{false, true} { + router := New() + router.RedirectTrailingSlash = redirectTrailingSlash + + var matchedRoute string + var matchedParams Params + + router.GET("/b/", func(c *Context) { + matchedRoute = "/b/" + matchedParams = c.Params + c.String(http.StatusOK, "b_slash") + }) + router.GET("/:name", func(c *Context) { + matchedRoute = "/:name" + matchedParams = c.Params + c.String(http.StatusOK, "name") + }) + router.GET("/a/b", func(c *Context) { + matchedRoute = "/a/b" + matchedParams = c.Params + c.String(http.StatusOK, "ab") + }) + router.GET("/:p/b/", func(c *Context) { + matchedRoute = "/:p/b/" + matchedParams = c.Params + c.String(http.StatusOK, "pb_slash") + }) + + // GET /b -> should match /:name with name=b + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/b", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/:name", matchedRoute) + assert.Equal(t, Params{Param{Key: "name", Value: "b"}}, matchedParams) + + // GET /b/ -> should match /b/ + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/b/", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/b/", matchedRoute) + + // GET /c -> should match /:name with name=c + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/c", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/:name", matchedRoute) + assert.Equal(t, Params{Param{Key: "name", Value: "c"}}, matchedParams) + + // GET /a/b/ -> should match /:p/b/ with p=a + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/a/b/", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/:p/b/", matchedRoute) + assert.Equal(t, Params{Param{Key: "p", Value: "a"}}, matchedParams) + + // GET /a/b -> should match /a/b + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/a/b", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/a/b", matchedRoute) + } +} + diff --git a/go.mod b/go.mod index 99a8d5bb..34770a69 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/mattn/go-isatty v0.0.20 github.com/modern-go/reflect2 v1.0.2 github.com/pelletier/go-toml/v2 v2.2.4 - github.com/quic-go/quic-go v0.60.0 + github.com/quic-go/quic-go v0.63.0 github.com/stretchr/testify v1.12.1 github.com/ugorji/go/codec v1.3.1 go.mongodb.org/mongo-driver/v2 v2.5.0 diff --git a/go.sum b/go.sum index dc6ef902..6ebe4d14 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.60.0 h1:xcQioE8OM66UQLeUMHltK1CCcOu3JbVB4JAQdDQSB+0= github.com/quic-go/quic-go v0.60.0/go.mod h1:wpKpjmPpftl30sL6pFh7REVpjbcCVy4zt2vDyK1TuJk= +github.com/quic-go/quic-go v0.63.0 h1:LIFGHI4PFUhhw2dDD1ARHdCff143ffMHwZtbnbuJ78A= +github.com/quic-go/quic-go v0.63.0/go.mod h1:RAro2j2yN9a9EiPACLHT9IB2NXCvGQmmo/alT0yYI0w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/tree.go b/tree.go index 6842d225..8750ff0c 100644 --- a/tree.go +++ b/tree.go @@ -461,28 +461,28 @@ walk: // Outer loop for walking the tree } if !n.wildChild { - // If the path at the end of the loop is not equal to '/' and the current node has no child nodes - // the current node needs to roll back to last valid skippedNode - if path != "/" { - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - continue walk + // If the current node has no child nodes, + // roll back to last valid skippedNode + cTsr := path == "/" && n.handlers != nil + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] } + globalParamsCount = skippedNode.paramsCount + value.tsr = value.tsr || cTsr + continue walk } } // Nothing found. // We can recommend to redirect to the same URL without a // trailing slash if a leaf exists for that path. - value.tsr = path == "/" && n.handlers != nil + value.tsr = value.tsr || cTsr return value } @@ -537,20 +537,52 @@ walk: // Outer loop for walking the tree } // ... but we can't - value.tsr = len(path) == end+1 + cTsr := len(path) == end+1 + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] + } + globalParamsCount = skippedNode.paramsCount + value.tsr = value.tsr || cTsr + continue walk + } + } + value.tsr = value.tsr || cTsr return value } if value.handlers = n.handlers; value.handlers != nil { value.fullPath = n.fullPath + value.tsr = false return value } + cTsr := false if len(n.children) == 1 { // No handle found. Check if a handle for this path + a // trailing slash exists for TSR recommendation n = n.children[0] - value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") + cTsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") } + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] + } + globalParamsCount = skippedNode.paramsCount + value.tsr = value.tsr || cTsr + continue walk + } + } + value.tsr = value.tsr || cTsr return value case catchAll: @@ -583,6 +615,7 @@ walk: // Outer loop for walking the tree value.handlers = n.handlers value.fullPath = n.fullPath + value.tsr = false return value default: @@ -592,9 +625,9 @@ walk: // Outer loop for walking the tree } if path == prefix { - // If the current path does not equal '/' and the node does not have a registered handle and the most recently matched node has a child node + // If the current node does not have a registered handle, // the current node needs to roll back to last valid skippedNode - if n.handlers == nil && path != "/" { + if n.handlers == nil { for length := len(*skippedNodes); length > 0; length-- { skippedNode := (*skippedNodes)[length-1] *skippedNodes = (*skippedNodes)[:length-1] @@ -608,50 +641,32 @@ walk: // Outer loop for walking the tree continue walk } } - // n = latestNode.children[len(latestNode.children)-1] } // We should have reached the node containing the handle. // Check if this node has a handle registered. if value.handlers = n.handlers; value.handlers != nil { value.fullPath = n.fullPath + value.tsr = false return value } // If there is no handle for this route, but this route has a // wildcard child, there must be a handle for this path with an // additional trailing slash - if path == "/" && n.wildChild && n.nType != root { - value.tsr = true - return value - } - - if path == "/" && n.nType == static { - value.tsr = true - return value - } - - // No handle found. Check if a handle for this path + a - // trailing slash exists for trailing slash recommendation - for i, c := range []byte(n.indices) { - if c == '/' { - n = n.children[i] - value.tsr = (len(n.path) == 1 && n.handlers != nil) || - (n.nType == catchAll && n.children[0].handlers != nil) - return value + cTsr := (path == "/" && n.wildChild && n.nType != root) || (path == "/" && n.nType == static) + if !cTsr { + // No handle found. Check if a handle for this path + a + // trailing slash exists for trailing slash recommendation + for i, c := range []byte(n.indices) { + if c == '/' { + n = n.children[i] + cTsr = (len(n.path) == 1 && n.handlers != nil) || + (n.nType == catchAll && n.children[0].handlers != nil) + break + } } } - return value - } - - // Nothing found. We can recommend to redirect to the same URL with an - // extra trailing slash if a leaf exists for that path - value.tsr = path == "/" || - (len(prefix) == len(path)+1 && prefix[len(path)] == '/' && - path == prefix[:len(prefix)-1] && n.handlers != nil) - - // roll back to last valid skippedNode - if !value.tsr && path != "/" { for length := len(*skippedNodes); length > 0; length-- { skippedNode := (*skippedNodes)[length-1] *skippedNodes = (*skippedNodes)[:length-1] @@ -662,11 +677,38 @@ walk: // Outer loop for walking the tree *value.params = (*value.params)[:skippedNode.paramsCount] } globalParamsCount = skippedNode.paramsCount + value.tsr = value.tsr || cTsr continue walk } } + + value.tsr = value.tsr || cTsr + return value } + // Nothing found. We can recommend to redirect to the same URL with an + // extra trailing slash if a leaf exists for that path + cTsr := path == "/" || + (len(prefix) == len(path)+1 && prefix[len(path)] == '/' && + path == prefix[:len(prefix)-1] && n.handlers != nil) + + // roll back to last valid skippedNode + for length := len(*skippedNodes); length > 0; length-- { + skippedNode := (*skippedNodes)[length-1] + *skippedNodes = (*skippedNodes)[:length-1] + if strings.HasSuffix(skippedNode.path, path) { + path = skippedNode.path + n = skippedNode.node + if value.params != nil { + *value.params = (*value.params)[:skippedNode.paramsCount] + } + globalParamsCount = skippedNode.paramsCount + value.tsr = value.tsr || cTsr + continue walk + } + } + + value.tsr = value.tsr || cTsr return value } } diff --git a/tree_test.go b/tree_test.go index 23339af4..2dab6604 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1111,3 +1111,78 @@ func TestTreeFindCaseInsensitivePathWildcardParamAndStaticChild(t *testing.T) { t.Errorf("Wrong result for '/prefix/something': %s", string(out)) } } + +func TestTreeTrailingSlashAndParamRoute(t *testing.T) { + // Case 1: Route /b/ and /:name, request /b + // GET /b should match /:name with param name="b", rather than redirecting to /b/ + tree := &node{} + routes := []string{ + "/b/", + "/:name", + } + for _, route := range routes { + tree.addRoute(route, fakeHandler(route)) + } + + checkRequests(t, tree, testRequests{ + {"/b", false, "/:name", Params{Param{Key: "name", Value: "b"}}}, + {"/c", false, "/:name", Params{Param{Key: "name", Value: "c"}}}, + {"/b/", false, "/b/", nil}, + }) + checkPriorities(t, tree) + + // Case 2: Mirror case: /a/b and /:p/b/, request /a/b/ + // GET /a/b/ should match /:p/b/ with param p="a", rather than redirecting to /a/b + tree2 := &node{} + routes2 := []string{ + "/a/b", + "/:p/b/", + } + for _, route := range routes2 { + tree2.addRoute(route, fakeHandler(route)) + } + + checkRequests(t, tree2, testRequests{ + {"/a/b/", false, "/:p/b/", Params{Param{Key: "p", Value: "a"}}}, + {"/a/b", false, "/a/b", nil}, + }) + checkPriorities(t, tree2) + + // Case 3: When no wildcard route matches the exact request, + // TSR should still be recommended for candidate static route + tree3 := &node{} + routes3 := []string{ + "/b/", + "/:name/x", + } + for _, route := range routes3 { + tree3.addRoute(route, fakeHandler(route)) + } + + val := tree3.getValue("/b", nil, getSkippedNodes(), false) + if val.handlers != nil { + t.Errorf("Expected nil handler for '/b', got: %v", val.handlers) + } + if !val.tsr { + t.Errorf("Expected TSR=true for '/b' when '/b/' exists and '/:name/x' does not match") + } + + // Case 4: Mirror TSR fallback when no wildcard route matches + tree4 := &node{} + routes4 := []string{ + "/a/b", + "/:p/b/x", + } + for _, route := range routes4 { + tree4.addRoute(route, fakeHandler(route)) + } + + val4 := tree4.getValue("/a/b/", nil, getSkippedNodes(), false) + if val4.handlers != nil { + t.Errorf("Expected nil handler for '/a/b/', got: %v", val4.handlers) + } + if !val4.tsr { + t.Errorf("Expected TSR=true for '/a/b/' when '/a/b' exists and '/:p/b/x' does not match") + } +} + From bd7d7359e030391ff94b61f9b54727f662e46ccb Mon Sep 17 00:00:00 2001 From: Adam Magued Date: Sat, 17 Oct 2026 16:54:12 +0300 Subject: [PATCH 2/4] style: format gin_test and tree_test with gofmt --- gin_test.go | 1 - tree_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/gin_test.go b/gin_test.go index 7c0f934e..2be35a6e 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1264,4 +1264,3 @@ func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { assert.Equal(t, "/a/b", matchedRoute) } } - diff --git a/tree_test.go b/tree_test.go index 2dab6604..50e71bca 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1185,4 +1185,3 @@ func TestTreeTrailingSlashAndParamRoute(t *testing.T) { t.Errorf("Expected TSR=true for '/a/b/' when '/a/b' exists and '/:p/b/x' does not match") } } - From 95bcf500eb063cf86114f8cfc41a0a7f287d3a72 Mon Sep 17 00:00:00 2001 From: Adam Magued Date: Sat, 17 Oct 2026 18:27:38 +0300 Subject: [PATCH 3/4] test: separate router instances in TestRouterParamRouteWithTrailingSlashSibling --- gin_test.go | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/gin_test.go b/gin_test.go index 2be35a6e..03119554 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1198,37 +1198,28 @@ func TestRouterQueryMethod(t *testing.T) { func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { for _, redirectTrailingSlash := range []bool{false, true} { - router := New() - router.RedirectTrailingSlash = redirectTrailingSlash + // Case 1: Route /b/ and /:name, request /b + router1 := New() + router1.RedirectTrailingSlash = redirectTrailingSlash var matchedRoute string var matchedParams Params - router.GET("/b/", func(c *Context) { + router1.GET("/b/", func(c *Context) { matchedRoute = "/b/" matchedParams = c.Params c.String(http.StatusOK, "b_slash") }) - router.GET("/:name", func(c *Context) { + router1.GET("/:name", func(c *Context) { matchedRoute = "/:name" matchedParams = c.Params c.String(http.StatusOK, "name") }) - router.GET("/a/b", func(c *Context) { - matchedRoute = "/a/b" - matchedParams = c.Params - c.String(http.StatusOK, "ab") - }) - router.GET("/:p/b/", func(c *Context) { - matchedRoute = "/:p/b/" - matchedParams = c.Params - c.String(http.StatusOK, "pb_slash") - }) // GET /b -> should match /:name with name=b w := httptest.NewRecorder() req, _ := http.NewRequest(http.MethodGet, "/b", nil) - router.ServeHTTP(w, req) + router1.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/:name", matchedRoute) assert.Equal(t, Params{Param{Key: "name", Value: "b"}}, matchedParams) @@ -1236,22 +1227,37 @@ func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { // GET /b/ -> should match /b/ w = httptest.NewRecorder() req, _ = http.NewRequest(http.MethodGet, "/b/", nil) - router.ServeHTTP(w, req) + router1.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/b/", matchedRoute) // GET /c -> should match /:name with name=c w = httptest.NewRecorder() req, _ = http.NewRequest(http.MethodGet, "/c", nil) - router.ServeHTTP(w, req) + router1.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/:name", matchedRoute) assert.Equal(t, Params{Param{Key: "name", Value: "c"}}, matchedParams) + // Case 2: Mirror case: /a/b and /:p/b/, request /a/b/ + router2 := New() + router2.RedirectTrailingSlash = redirectTrailingSlash + + router2.GET("/a/b", func(c *Context) { + matchedRoute = "/a/b" + matchedParams = c.Params + c.String(http.StatusOK, "ab") + }) + router2.GET("/:p/b/", func(c *Context) { + matchedRoute = "/:p/b/" + matchedParams = c.Params + c.String(http.StatusOK, "pb_slash") + }) + // GET /a/b/ -> should match /:p/b/ with p=a w = httptest.NewRecorder() req, _ = http.NewRequest(http.MethodGet, "/a/b/", nil) - router.ServeHTTP(w, req) + router2.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/:p/b/", matchedRoute) assert.Equal(t, Params{Param{Key: "p", Value: "a"}}, matchedParams) @@ -1259,7 +1265,7 @@ func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { // GET /a/b -> should match /a/b w = httptest.NewRecorder() req, _ = http.NewRequest(http.MethodGet, "/a/b", nil) - router.ServeHTTP(w, req) + router2.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/a/b", matchedRoute) } From a010002b100a0856ea3f06f7085dae608fb1cb06 Mon Sep 17 00:00:00 2001 From: AdamMagued Date: Sun, 1 Nov 2026 17:57:55 +0200 Subject: [PATCH 4/4] fix(tree): address review feedback on trailing slash param routing --- gin_test.go | 31 +++++++++++++++++ go.mod | 2 +- go.sum | 2 -- tree.go | 96 +++++++++++++++++----------------------------------- tree_test.go | 33 ++++++++++++++++++ 5 files changed, 96 insertions(+), 68 deletions(-) diff --git a/gin_test.go b/gin_test.go index 03119554..f238522e 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1268,5 +1268,36 @@ func TestRouterParamRouteWithTrailingSlashSibling(t *testing.T) { router2.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "/a/b", matchedRoute) + + // Case 3: Wildcard sibling interaction: /:a/b/ and /:a/b, request /foo/b + router3 := New() + router3.RedirectTrailingSlash = redirectTrailingSlash + + router3.GET("/:a/b/", func(c *Context) { + matchedRoute = "/:a/b/" + matchedParams = c.Params + c.String(http.StatusOK, "ab_slash") + }) + router3.GET("/:a/b", func(c *Context) { + matchedRoute = "/:a/b" + matchedParams = c.Params + c.String(http.StatusOK, "ab") + }) + + // GET /foo/b -> should match /:a/b with a=foo + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/foo/b", nil) + router3.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/:a/b", matchedRoute) + assert.Equal(t, Params{Param{Key: "a", Value: "foo"}}, matchedParams) + + // GET /foo/b/ -> should match /:a/b/ with a=foo + w = httptest.NewRecorder() + req, _ = http.NewRequest(http.MethodGet, "/foo/b/", nil) + router3.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "/:a/b/", matchedRoute) + assert.Equal(t, Params{Param{Key: "a", Value: "foo"}}, matchedParams) } } diff --git a/go.mod b/go.mod index 34770a69..99a8d5bb 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/mattn/go-isatty v0.0.20 github.com/modern-go/reflect2 v1.0.2 github.com/pelletier/go-toml/v2 v2.2.4 - github.com/quic-go/quic-go v0.63.0 + github.com/quic-go/quic-go v0.60.0 github.com/stretchr/testify v1.12.1 github.com/ugorji/go/codec v1.3.1 go.mongodb.org/mongo-driver/v2 v2.5.0 diff --git a/go.sum b/go.sum index 6ebe4d14..dc6ef902 100644 --- a/go.sum +++ b/go.sum @@ -50,8 +50,6 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.60.0 h1:xcQioE8OM66UQLeUMHltK1CCcOu3JbVB4JAQdDQSB+0= github.com/quic-go/quic-go v0.60.0/go.mod h1:wpKpjmPpftl30sL6pFh7REVpjbcCVy4zt2vDyK1TuJk= -github.com/quic-go/quic-go v0.63.0 h1:LIFGHI4PFUhhw2dDD1ARHdCff143ffMHwZtbnbuJ78A= -github.com/quic-go/quic-go v0.63.0/go.mod h1:RAro2j2yN9a9EiPACLHT9IB2NXCvGQmmo/alT0yYI0w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/tree.go b/tree.go index 8750ff0c..8bdf78a2 100644 --- a/tree.go +++ b/tree.go @@ -426,6 +426,7 @@ func (n *node) getValue(path string, params *Params, skippedNodes *[]skippedNode // able to record one more backtrack frame. A raw reslice of the capacity // captured at pool allocation panics with "slice bounds out of range". *skippedNodes = (*skippedNodes)[:0] + value.tsr = false walk: // Outer loop for walking the tree for { @@ -538,20 +539,6 @@ walk: // Outer loop for walking the tree // ... but we can't cTsr := len(path) == end+1 - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - value.tsr = value.tsr || cTsr - continue walk - } - } value.tsr = value.tsr || cTsr return value } @@ -568,20 +555,6 @@ walk: // Outer loop for walking the tree n = n.children[0] cTsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") } - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - value.tsr = value.tsr || cTsr - continue walk - } - } value.tsr = value.tsr || cTsr return value @@ -627,7 +600,7 @@ walk: // Outer loop for walking the tree if path == prefix { // If the current node does not have a registered handle, // the current node needs to roll back to last valid skippedNode - if n.handlers == nil { + if n.handlers == nil && path != "/" { for length := len(*skippedNodes); length > 0; length-- { skippedNode := (*skippedNodes)[length-1] *skippedNodes = (*skippedNodes)[:length-1] @@ -653,20 +626,38 @@ walk: // Outer loop for walking the tree // If there is no handle for this route, but this route has a // wildcard child, there must be a handle for this path with an // additional trailing slash - cTsr := (path == "/" && n.wildChild && n.nType != root) || (path == "/" && n.nType == static) - if !cTsr { - // No handle found. Check if a handle for this path + a - // trailing slash exists for trailing slash recommendation - for i, c := range []byte(n.indices) { - if c == '/' { - n = n.children[i] - cTsr = (len(n.path) == 1 && n.handlers != nil) || - (n.nType == catchAll && n.children[0].handlers != nil) - break - } + if path == "/" && n.wildChild && n.nType != root { + value.tsr = true + return value + } + + if path == "/" && n.nType == static { + value.tsr = true + return value + } + + // No handle found. Check if a handle for this path + a + // trailing slash exists for trailing slash recommendation + for i, c := range []byte(n.indices) { + if c == '/' { + n = n.children[i] + value.tsr = (len(n.path) == 1 && n.handlers != nil) || + (n.nType == catchAll && len(n.children) > 0 && n.children[0].handlers != nil) + return value } } + return value + } + + // Nothing found. We can recommend to redirect to the same URL with an + // extra trailing slash if a leaf exists for that path + cTsr := path == "/" || + (len(prefix) == len(path)+1 && prefix[len(path)] == '/' && + path == prefix[:len(prefix)-1] && n.handlers != nil) + + // roll back to last valid skippedNode + if path != "/" { for length := len(*skippedNodes); length > 0; length-- { skippedNode := (*skippedNodes)[length-1] *skippedNodes = (*skippedNodes)[:length-1] @@ -681,31 +672,6 @@ walk: // Outer loop for walking the tree continue walk } } - - value.tsr = value.tsr || cTsr - return value - } - - // Nothing found. We can recommend to redirect to the same URL with an - // extra trailing slash if a leaf exists for that path - cTsr := path == "/" || - (len(prefix) == len(path)+1 && prefix[len(path)] == '/' && - path == prefix[:len(prefix)-1] && n.handlers != nil) - - // roll back to last valid skippedNode - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - value.tsr = value.tsr || cTsr - continue walk - } } value.tsr = value.tsr || cTsr diff --git a/tree_test.go b/tree_test.go index 50e71bca..57c48044 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1184,4 +1184,37 @@ func TestTreeTrailingSlashAndParamRoute(t *testing.T) { if !val4.tsr { t.Errorf("Expected TSR=true for '/a/b/' when '/a/b' exists and '/:p/b/x' does not match") } + + // Case 5: Wildcard sibling interaction: /:a/b/ and /:a/b, request /foo/b + // GET /foo/b should match /:a/b with param a="foo", rather than redirecting to /:a/b/ + tree5 := &node{} + routes5 := []string{ + "/:a/b/", + "/:a/b", + } + for _, route := range routes5 { + tree5.addRoute(route, fakeHandler(route)) + } + + checkRequests(t, tree5, testRequests{ + {"/foo/b", false, "/:a/b", Params{Param{Key: "a", Value: "foo"}}}, + {"/foo/b/", false, "/:a/b/", Params{Param{Key: "a", Value: "foo"}}}, + }) + checkPriorities(t, tree5) + + // Case 6: Reverse registration order of wildcard sibling routes + tree6 := &node{} + routes6 := []string{ + "/:a/b", + "/:a/b/", + } + for _, route := range routes6 { + tree6.addRoute(route, fakeHandler(route)) + } + + checkRequests(t, tree6, testRequests{ + {"/foo/b", false, "/:a/b", Params{Param{Key: "a", Value: "foo"}}}, + {"/foo/b/", false, "/:a/b/", Params{Param{Key: "a", Value: "foo"}}}, + }) + checkPriorities(t, tree6) }