tree: sync httprouter update (#2172)

This commit is contained in:
thinkerou 2019-12-08 19:34:05 +08:00 committed by GitHub
parent c6544855d7
commit 6e16da8683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

41
tree.go
View File

@ -253,13 +253,13 @@ walk:
} }
n.insertChild(numParams, path, fullPath, handlers) n.insertChild(numParams, path, fullPath, handlers)
return return
}
} else if i == len(path) { // Make node a (in-path) leaf // Otherwise and handle to current node
if n.handlers != nil { if n.handlers != nil {
panic("handlers are already registered for path '" + fullPath + "'") panic("handlers are already registered for path '" + fullPath + "'")
} }
n.handlers = handlers n.handlers = handlers
}
return return
} }
} }
@ -267,31 +267,31 @@ walk:
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) { func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) {
var offset int // already handled bytes of the path var offset int // already handled bytes of the path
// find prefix until first wildcard (beginning with ':' or '*') // Find prefix until first wildcard (beginning with ':' or '*')
for i, max := 0, len(path); numParams > 0; i++ { for i, max := 0, len(path); numParams > 0; i++ {
c := path[i] c := path[i]
if c != ':' && c != '*' { if c != ':' && c != '*' {
continue continue
} }
// find wildcard end (either '/' or path end) // Find wildcard end (either '/' or path end) and check the name for invalid characters
end := i + 1 end := i + 1
for end < max && path[end] != '/' { invalid := false
switch path[end] { for end < max {
// the wildcard name must not contain ':' and '*' c := path[end]
case ':', '*': if c == '/' {
panic("only one wildcard per path segment is allowed, has: '" + break
path[i:] + "' in path '" + fullPath + "'") }
default: if c == ':' || c == '*' {
invalid = true
}
end++ end++
} }
}
// check if this Node existing children which would be // The wildcard name must not contain ':' and '*'
// unreachable if we insert the wildcard here if invalid {
if len(n.children) > 0 { panic("only one wildcard per path segment is allowed, has: '" +
panic("wildcard route '" + path[i:end] + path[i:end] + "' in path '" + fullPath + "'")
"' conflicts with existing children in path '" + fullPath + "'")
} }
// check if the wildcard has a name // check if the wildcard has a name
@ -299,6 +299,13 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
panic("wildcards must be named with a non-empty name in path '" + fullPath + "'") panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
} }
// Check if this node has existing children which would be
// unreachable if we insert the wildcard here
if len(n.children) > 0 {
panic("wildcard route '" + path[i:end] +
"' conflicts with existing children in path '" + fullPath + "'")
}
if c == ':' { // param if c == ':' { // param
// split path at the beginning of the wildcard // split path at the beginning of the wildcard
if i > 0 { if i > 0 {