mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 23:12:17 +08:00
udpate
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
71f841a1f5
commit
fe436bf2c0
5
gin.go
5
gin.go
@ -253,6 +253,7 @@ func (engine *Engine) rebuild405Handlers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||||
|
// log.Println(method, path)
|
||||||
assert1(path[0] == '/', "path must begin with '/'")
|
assert1(path[0] == '/', "path must begin with '/'")
|
||||||
assert1(method != "", "HTTP method can not be empty")
|
assert1(method != "", "HTTP method can not be empty")
|
||||||
assert1(len(handlers) > 0, "there must be at least one handler")
|
assert1(len(handlers) > 0, "there must be at least one handler")
|
||||||
@ -269,13 +270,15 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
|||||||
root.addRoute(path, handlers)
|
root.addRoute(path, handlers)
|
||||||
|
|
||||||
// Update maxParams
|
// Update maxParams
|
||||||
if paramsCount := countParams(path); paramsCount+varsCount > root.maxParams {
|
// log.Println("countParams(path):", countParams(path))
|
||||||
|
if paramsCount := countParams(path); paramsCount+varsCount > engine.maxParams {
|
||||||
engine.maxParams = paramsCount + varsCount
|
engine.maxParams = paramsCount + varsCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lazy-init paramsPool alloc func
|
// Lazy-init paramsPool alloc func
|
||||||
if engine.paramsPool.New == nil && engine.maxParams > 0 {
|
if engine.paramsPool.New == nil && engine.maxParams > 0 {
|
||||||
engine.paramsPool.New = func() interface{} {
|
engine.paramsPool.New = func() interface{} {
|
||||||
|
// log.Println("engine.maxParams:", engine.maxParams)
|
||||||
ps := make(Params, 0, engine.maxParams)
|
ps := make(Params, 0, engine.maxParams)
|
||||||
return &ps
|
return &ps
|
||||||
}
|
}
|
||||||
|
244
tree.go
244
tree.go
@ -5,7 +5,6 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@ -424,146 +423,143 @@ func (n *node) getValue(path string, params func() *Params, unescape bool) (valu
|
|||||||
walk: // Outer loop for walking the tree
|
walk: // Outer loop for walking the tree
|
||||||
for {
|
for {
|
||||||
prefix := n.path
|
prefix := n.path
|
||||||
|
if len(path) > len(prefix) {
|
||||||
if len(path) > len(prefix) && path[:len(prefix)] == prefix {
|
if path[:len(prefix)] == prefix {
|
||||||
path = path[len(prefix):]
|
path = path[len(prefix):]
|
||||||
// If this node does not have a wildcard (param or catchAll)
|
// If this node does not have a wildcard (param or catchAll)
|
||||||
// child, we can just look up the next child node and continue
|
// child, we can just look up the next child node and continue
|
||||||
// to walk down the tree
|
// to walk down the tree
|
||||||
if !n.wildChild {
|
if !n.wildChild {
|
||||||
idxc := path[0]
|
idxc := path[0]
|
||||||
for i, c := range []byte(n.indices) {
|
for i, c := range []byte(n.indices) {
|
||||||
if c == idxc {
|
if c == idxc {
|
||||||
n = n.children[i]
|
n = n.children[i]
|
||||||
continue walk
|
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)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing found.
|
// handle wildcard child
|
||||||
// We can recommend to redirect to the same URL without a
|
n = n.children[0]
|
||||||
// trailing slash if a leaf exists for that path.
|
switch n.nType {
|
||||||
value.tsr = (path == "/" && n.handlers != nil)
|
case param:
|
||||||
return
|
// find param end (either '/' or path end)
|
||||||
}
|
end := 0
|
||||||
|
for end < len(path) && path[end] != '/' {
|
||||||
// handle wildcard child
|
end++
|
||||||
n = n.children[0]
|
|
||||||
switch n.nType {
|
|
||||||
case param:
|
|
||||||
// find param end (either '/' or path end)
|
|
||||||
end := 0
|
|
||||||
for end < len(path) && path[end] != '/' {
|
|
||||||
end++
|
|
||||||
}
|
|
||||||
|
|
||||||
// save param value
|
|
||||||
// if cap(value.params) < int(n.maxParams) {
|
|
||||||
// value.params = make(Params, 0, n.maxParams)
|
|
||||||
// }
|
|
||||||
// i := len(value.params)
|
|
||||||
// value.params = value.params[:i+1] // expand slice within preallocated capacity
|
|
||||||
// value.params[i].Key = n.path[1:]
|
|
||||||
// val := path[:end]
|
|
||||||
// if unescape {
|
|
||||||
// var err error
|
|
||||||
// if value.params[i].Value, err = url.QueryUnescape(val); err != nil {
|
|
||||||
// value.params[i].Value = val // fallback, in case of error
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// value.params[i].Value = val
|
|
||||||
// }
|
|
||||||
if params != nil {
|
|
||||||
if value.params == nil {
|
|
||||||
value.params = params()
|
|
||||||
}
|
}
|
||||||
// Expand slice within preallocated capacity
|
|
||||||
i := len(*value.params)
|
// save param value
|
||||||
log.Println(i)
|
// if cap(value.params) < int(n.maxParams) {
|
||||||
log.Println(*value.params)
|
// value.params = make(Params, 0, n.maxParams)
|
||||||
*value.params = (*value.params)[:i+1]
|
// }
|
||||||
|
// i := len(value.params)
|
||||||
|
// value.params = value.params[:i+1] // expand slice within preallocated capacity
|
||||||
|
// value.params[i].Key = n.path[1:]
|
||||||
// val := path[:end]
|
// val := path[:end]
|
||||||
// if unescape {
|
// if unescape {
|
||||||
// if v, err := url.QueryUnescape(val); err == nil {
|
// var err error
|
||||||
// val = v
|
// if value.params[i].Value, err = url.QueryUnescape(val); err != nil {
|
||||||
|
// value.params[i].Value = val // fallback, in case of error
|
||||||
// }
|
// }
|
||||||
|
// } else {
|
||||||
|
// value.params[i].Value = val
|
||||||
// }
|
// }
|
||||||
(*value.params)[i] = Param{
|
if params != nil {
|
||||||
Key: n.path[1:],
|
if value.params == nil {
|
||||||
Value: path,
|
value.params = params()
|
||||||
|
}
|
||||||
|
// Expand slice within preallocated capacity
|
||||||
|
i := len(*value.params)
|
||||||
|
*value.params = (*value.params)[:i+1]
|
||||||
|
// val := path[:end]
|
||||||
|
// if unescape {
|
||||||
|
// if v, err := url.QueryUnescape(val); err == nil {
|
||||||
|
// val = v
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
(*value.params)[i] = Param{
|
||||||
|
Key: n.path[1:],
|
||||||
|
Value: path[:end],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// we need to go deeper!
|
// we need to go deeper!
|
||||||
if end < len(path) {
|
if end < len(path) {
|
||||||
if len(n.children) > 0 {
|
if len(n.children) > 0 {
|
||||||
path = path[end:]
|
path = path[end:]
|
||||||
|
n = n.children[0]
|
||||||
|
continue walk
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... but we can't
|
||||||
|
value.tsr = (len(path) == end+1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if value.handlers = n.handlers; value.handlers != nil {
|
||||||
|
// value.fullPath = n.fullPath
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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]
|
n = n.children[0]
|
||||||
continue walk
|
value.tsr = (n.path == "/" && n.handlers != nil)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
|
||||||
|
case catchAll:
|
||||||
|
// // save param value
|
||||||
|
// if cap(value.params) < int(n.maxParams) {
|
||||||
|
// value.params = make(Params, 0, n.maxParams)
|
||||||
|
// }
|
||||||
|
// i := len(value.params)
|
||||||
|
// value.params = value.params[:i+1] // expand slice within preallocated capacity
|
||||||
|
// value.params[i].Key = n.path[2:]
|
||||||
|
// if unescape {
|
||||||
|
// var err error
|
||||||
|
// if value.params[i].Value, err = url.QueryUnescape(path); err != nil {
|
||||||
|
// value.params[i].Value = path // fallback, in case of error
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// value.params[i].Value = path
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Save param value
|
||||||
|
if params != nil {
|
||||||
|
if value.params == nil {
|
||||||
|
value.params = params()
|
||||||
|
}
|
||||||
|
// Expand slice within preallocated capacity
|
||||||
|
i := len(*value.params)
|
||||||
|
*value.params = (*value.params)[:i+1]
|
||||||
|
// if unescape {
|
||||||
|
// if v, err := url.QueryUnescape(path); err == nil {
|
||||||
|
// path = v
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
(*value.params)[i] = Param{
|
||||||
|
Key: n.path[2:],
|
||||||
|
Value: path,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... but we can't
|
value.handlers = n.handlers
|
||||||
value.tsr = (len(path) == end+1)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if value.handlers = n.handlers; value.handlers != nil {
|
|
||||||
// value.fullPath = n.fullPath
|
// value.fullPath = n.fullPath
|
||||||
return
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic("invalid node type")
|
||||||
}
|
}
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
|
|
||||||
case catchAll:
|
|
||||||
// // save param value
|
|
||||||
// if cap(value.params) < int(n.maxParams) {
|
|
||||||
// value.params = make(Params, 0, n.maxParams)
|
|
||||||
// }
|
|
||||||
// i := len(value.params)
|
|
||||||
// value.params = value.params[:i+1] // expand slice within preallocated capacity
|
|
||||||
// value.params[i].Key = n.path[2:]
|
|
||||||
// if unescape {
|
|
||||||
// var err error
|
|
||||||
// if value.params[i].Value, err = url.QueryUnescape(path); err != nil {
|
|
||||||
// value.params[i].Value = path // fallback, in case of error
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// value.params[i].Value = path
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Save param value
|
|
||||||
if params != nil {
|
|
||||||
if value.params == nil {
|
|
||||||
value.params = params()
|
|
||||||
}
|
|
||||||
// Expand slice within preallocated capacity
|
|
||||||
i := len(*value.params)
|
|
||||||
*value.params = (*value.params)[:i+1]
|
|
||||||
// if unescape {
|
|
||||||
// if v, err := url.QueryUnescape(path); err == nil {
|
|
||||||
// path = v
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
(*value.params)[i] = Param{
|
|
||||||
Key: n.path[2:],
|
|
||||||
Value: path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
value.handlers = n.handlers
|
|
||||||
// value.fullPath = n.fullPath
|
|
||||||
return
|
|
||||||
|
|
||||||
default:
|
|
||||||
panic("invalid node type")
|
|
||||||
}
|
}
|
||||||
}
|
} else if path == prefix {
|
||||||
|
|
||||||
if path == prefix {
|
|
||||||
// We should have reached the node containing the handle.
|
// We should have reached the node containing the handle.
|
||||||
// Check if this node has a handle registered.
|
// Check if this node has a handle registered.
|
||||||
if value.handlers = n.handlers; value.handlers != nil {
|
if value.handlers = n.handlers; value.handlers != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user