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) }