mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-25 04:23:36 +08:00
Merge 2da39e3037cd040a6cc25c79e00a6fd808471174 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
c9abdd8f4c
29
tree.go
29
tree.go
@ -595,6 +595,35 @@ walk: // Outer loop for walking the tree
|
||||
// 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
|
||||
// the current node needs to roll back to last valid skippedNode
|
||||
if n.handlers == nil && path != "/" {
|
||||
// Before rolling back, check whether this static node itself
|
||||
// recommends a trailing-slash redirect (a '/' child with a
|
||||
// handler, e.g. /aa/ registered alongside /aa_id). A wildcard
|
||||
// branch match still wins over the redirect, but when the
|
||||
// rollback finds no handler the redirect must not be lost.
|
||||
// See #4890.
|
||||
tsr := false
|
||||
for i, c := range []byte(n.indices) {
|
||||
if c == '/' {
|
||||
child := n.children[i]
|
||||
tsr = (len(child.path) == 1 && child.handlers != nil) ||
|
||||
(child.nType == catchAll && child.children[0].handlers != nil)
|
||||
break
|
||||
}
|
||||
}
|
||||
if tsr {
|
||||
for length := len(*skippedNodes); length > 0; length-- {
|
||||
skippedNode := (*skippedNodes)[length-1]
|
||||
*skippedNodes = (*skippedNodes)[:length-1]
|
||||
if strings.HasSuffix(skippedNode.path, path) {
|
||||
if v := skippedNode.node.getValue(skippedNode.path, params, skippedNodes, unescape); v.handlers != nil {
|
||||
return v
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
value.tsr = true
|
||||
return value
|
||||
}
|
||||
for length := len(*skippedNodes); length > 0; length-- {
|
||||
skippedNode := (*skippedNodes)[length-1]
|
||||
*skippedNodes = (*skippedNodes)[:length-1]
|
||||
|
||||
27
tree_test.go
27
tree_test.go
@ -711,6 +711,33 @@ func TestRedirectTrailingSlash(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirectTrailingSlashWithSkippedWildcard(t *testing.T) {
|
||||
// Regression test for #4890: when a wildcard route makes the walk roll back
|
||||
// to a skipped node, the static branch's trailing-slash recommendation must
|
||||
// not be lost (a 301 to /aa/ is expected, not a 404).
|
||||
tr := &node{}
|
||||
tr.addRoute("/:c/bb", fakeHandler("/:c/bb"))
|
||||
tr.addRoute("/aa/", fakeHandler("/aa/"))
|
||||
tr.addRoute("/aa_id", fakeHandler("/aa_id"))
|
||||
|
||||
value := tr.getValue("/aa", nil, getSkippedNodes(), false)
|
||||
if value.tsr != true {
|
||||
t.Fatalf("want true, is false")
|
||||
}
|
||||
if value.handlers != nil {
|
||||
t.Fatalf("want no handlers for /aa")
|
||||
}
|
||||
|
||||
// A wildcard branch that does match still wins over the redirect.
|
||||
tr2 := &node{}
|
||||
tr2.addRoute("/:c/bb", fakeHandler("/:c/bb"))
|
||||
tr2.addRoute("/aa/", fakeHandler("/aa/"))
|
||||
value2 := tr2.getValue("/aa/bb", nil, getSkippedNodes(), false)
|
||||
if value2.handlers == nil {
|
||||
t.Fatalf("wildcard route /:c/bb no longer matched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeFindCaseInsensitivePath(t *testing.T) {
|
||||
tree := &node{}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user