mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
sync routing tree with latest changes in Gin (#51541)
This commit is contained in:
parent
2473dc68f6
commit
0caecfe298
@ -34,7 +34,7 @@ func longestCommonPrefix(a, b string) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// addChild will add a child Node, keeping wildcards at the end
|
// addChild will add a child Node, keeping wildcardChild at the end
|
||||||
func (n *Node) addChild(child *Node) {
|
func (n *Node) addChild(child *Node) {
|
||||||
if n.wildChild && len(n.children) > 0 {
|
if n.wildChild && len(n.children) > 0 {
|
||||||
wildcardChild := n.children[len(n.children)-1]
|
wildcardChild := n.children[len(n.children)-1]
|
||||||
@ -273,7 +273,7 @@ func (n *Node) insertChild(path string, fullPath string, handlers Handler) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// The wildcard name must not contain ':' and '*'
|
// The wildcard name must only contain one ':' or '*' character
|
||||||
if !valid {
|
if !valid {
|
||||||
panic("only one wildcard per path segment is allowed, has: '" +
|
panic("only one wildcard per path segment is allowed, has: '" +
|
||||||
wildcard + "' in path '" + fullPath + "'")
|
wildcard + "' in path '" + fullPath + "'")
|
||||||
@ -302,7 +302,7 @@ func (n *Node) insertChild(path string, fullPath string, handlers Handler) {
|
|||||||
n.priority++
|
n.priority++
|
||||||
|
|
||||||
// if the path doesn't end with the wildcard, then there
|
// if the path doesn't end with the wildcard, then there
|
||||||
// will be another non-wildcard subpath starting with '/'
|
// will be another subpath starting with '/'
|
||||||
if len(wildcard) < len(path) {
|
if len(wildcard) < len(path) {
|
||||||
path = path[len(wildcard):]
|
path = path[len(wildcard):]
|
||||||
|
|
||||||
@ -326,7 +326,12 @@ func (n *Node) insertChild(path string, fullPath string, handlers Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
|
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
|
||||||
panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'")
|
pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]
|
||||||
|
panic("catch-all wildcard '" + path +
|
||||||
|
"' in new path '" + fullPath +
|
||||||
|
"' conflicts with existing path segment '" + pathSeg +
|
||||||
|
"' in existing prefix '" + n.path + pathSeg +
|
||||||
|
"'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently fixed width 1 for '/'
|
// currently fixed width 1 for '/'
|
||||||
@ -428,7 +433,7 @@ walk: // Outer loop for walking the tree
|
|||||||
|
|
||||||
if !n.wildChild {
|
if !n.wildChild {
|
||||||
// If the path at the end of the loop is not equal to '/' and the current node has no child nodes
|
// 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 vaild skippedNode
|
// the current node needs to roll back to last valid skippedNode
|
||||||
if path != "/" {
|
if path != "/" {
|
||||||
for l := len(*skippedNodes); l > 0; {
|
for l := len(*skippedNodes); l > 0; {
|
||||||
skippedNode := (*skippedNodes)[l-1]
|
skippedNode := (*skippedNodes)[l-1]
|
||||||
@ -508,7 +513,7 @@ walk: // Outer loop for walking the tree
|
|||||||
// No handle found. Check if a handle for this path + a
|
// No handle found. Check if a handle for this path + a
|
||||||
// trailing slash exists for TSR recommendation
|
// trailing slash exists for TSR recommendation
|
||||||
n = n.children[0]
|
n = n.children[0]
|
||||||
value.Tsr = n.path == "/" && n.handler != nil
|
value.Tsr = (n.path == "/" && n.handler != nil) || (n.path == "" && n.indices == "/")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -545,7 +550,7 @@ walk: // Outer loop for walking the tree
|
|||||||
|
|
||||||
if path == prefix {
|
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 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 vaild skippedNode
|
// the current node needs to roll back to last valid skippedNode
|
||||||
if n.handler == nil && path != "/" {
|
if n.handler == nil && path != "/" {
|
||||||
for l := len(*skippedNodes); l > 0; {
|
for l := len(*skippedNodes); l > 0; {
|
||||||
skippedNode := (*skippedNodes)[l-1]
|
skippedNode := (*skippedNodes)[l-1]
|
||||||
|
@ -588,6 +588,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
|
|||||||
"/blog/:p",
|
"/blog/:p",
|
||||||
"/posts/:b/:c",
|
"/posts/:b/:c",
|
||||||
"/posts/b/:c/d/",
|
"/posts/b/:c/d/",
|
||||||
|
"/vendor/:x/*y",
|
||||||
}
|
}
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
recv := catchPanic(func() {
|
recv := catchPanic(func() {
|
||||||
@ -624,6 +625,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
|
|||||||
"/api/world/abc/",
|
"/api/world/abc/",
|
||||||
"/blog/pp/",
|
"/blog/pp/",
|
||||||
"/posts/b/c/d",
|
"/posts/b/c/d",
|
||||||
|
"/vendor/x",
|
||||||
}
|
}
|
||||||
for _, route := range tsrRoutes {
|
for _, route := range tsrRoutes {
|
||||||
value := tree.GetValue(route, false)
|
value := tree.GetValue(route, false)
|
||||||
|
Loading…
Reference in New Issue
Block a user