mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
获取路由地址
This commit is contained in:
parent
5acf660117
commit
b18c5bc917
@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/gin-contrib/sse"
|
"github.com/gin-contrib/sse"
|
||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Content-Type MIME of the most common data formats.
|
// Content-Type MIME of the most common data formats.
|
||||||
@ -634,6 +635,10 @@ func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (e
|
|||||||
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
||||||
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
|
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
|
||||||
func (c *Context) ClientIP() string {
|
func (c *Context) ClientIP() string {
|
||||||
|
root:=c.engine.trees[0].root
|
||||||
|
fmt.Println("wsh2:",(*root.children[0]).allPath,root.indices)
|
||||||
|
fmt.Println("wsh002:",(*(root.children[1])),root.indices)
|
||||||
|
fmt.Println("wsh02:",len(root.children))
|
||||||
if c.engine.ForwardedByClientIP {
|
if c.engine.ForwardedByClientIP {
|
||||||
clientIP := c.requestHeader("X-Forwarded-For")
|
clientIP := c.requestHeader("X-Forwarded-For")
|
||||||
clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
|
clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
|
||||||
|
1
debug.go
1
debug.go
@ -30,6 +30,7 @@ func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
|||||||
nuHandlers := len(handlers)
|
nuHandlers := len(handlers)
|
||||||
handlerName := nameOfFunction(handlers.Last())
|
handlerName := nameOfFunction(handlers.Last())
|
||||||
if DebugPrintRouteFunc == nil {
|
if DebugPrintRouteFunc == nil {
|
||||||
|
fmt.Println("wsh1:",absolutePath)
|
||||||
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
||||||
} else {
|
} else {
|
||||||
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
|
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
|
||||||
|
5
gin.go
5
gin.go
@ -376,10 +376,11 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
}
|
}
|
||||||
root := t[i].root
|
root := t[i].root
|
||||||
// Find route in tree
|
// Find route in tree
|
||||||
handlers, params, tsr := root.getValue(path, c.Params, unescape)
|
handlers, params, tsr,_ := root.getValue(path, c.Params, unescape)
|
||||||
if handlers != nil {
|
if handlers != nil {
|
||||||
c.handlers = handlers
|
c.handlers = handlers
|
||||||
c.Params = params
|
c.Params = params
|
||||||
|
fmt.Println("params:",params)
|
||||||
c.Next()
|
c.Next()
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
return
|
return
|
||||||
@ -401,7 +402,7 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
if tree.method == httpMethod {
|
if tree.method == httpMethod {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
|
if handlers, _, _ ,_:= tree.root.getValue(path, nil, unescape); handlers != nil {
|
||||||
c.handlers = engine.allNoMethod
|
c.handlers = engine.allNoMethod
|
||||||
serveError(c, http.StatusMethodNotAllowed, default405Body)
|
serveError(c, http.StatusMethodNotAllowed, default405Body)
|
||||||
return
|
return
|
||||||
|
27
tree.go
27
tree.go
@ -8,6 +8,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Param is a single URL parameter, consisting of a key and a value.
|
// Param is a single URL parameter, consisting of a key and a value.
|
||||||
@ -94,6 +95,7 @@ type node struct {
|
|||||||
nType nodeType
|
nType nodeType
|
||||||
maxParams uint8
|
maxParams uint8
|
||||||
wildChild bool
|
wildChild bool
|
||||||
|
allPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// increments priority of the given child and reorders if necessary.
|
// increments priority of the given child and reorders if necessary.
|
||||||
@ -123,6 +125,7 @@ func (n *node) incrementChildPrio(pos int) int {
|
|||||||
// addRoute adds a node with the given handle to the path.
|
// addRoute adds a node with the given handle to the path.
|
||||||
// Not concurrency-safe!
|
// Not concurrency-safe!
|
||||||
func (n *node) addRoute(path string, handlers HandlersChain) {
|
func (n *node) addRoute(path string, handlers HandlersChain) {
|
||||||
|
fmt.Println("wsh66:",path)
|
||||||
fullPath := path
|
fullPath := path
|
||||||
n.priority++
|
n.priority++
|
||||||
numParams := countParams(path)
|
numParams := countParams(path)
|
||||||
@ -153,6 +156,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
|||||||
indices: n.indices,
|
indices: n.indices,
|
||||||
children: n.children,
|
children: n.children,
|
||||||
handlers: n.handlers,
|
handlers: n.handlers,
|
||||||
|
allPath:path,
|
||||||
priority: n.priority - 1,
|
priority: n.priority - 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +172,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
|||||||
n.indices = string([]byte{n.path[i]})
|
n.indices = string([]byte{n.path[i]})
|
||||||
n.path = path[:i]
|
n.path = path[:i]
|
||||||
n.handlers = nil
|
n.handlers = nil
|
||||||
|
n.allPath=path
|
||||||
n.wildChild = false
|
n.wildChild = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,12 +240,14 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
|||||||
n = child
|
n = child
|
||||||
}
|
}
|
||||||
n.insertChild(numParams, path, fullPath, handlers)
|
n.insertChild(numParams, path, fullPath, handlers)
|
||||||
|
n.allPath=path
|
||||||
return
|
return
|
||||||
|
|
||||||
} else if i == len(path) { // Make node a (in-path) leaf
|
} else if i == len(path) { // Make node a (in-path) leaf
|
||||||
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.allPath=path
|
||||||
n.handlers = handlers
|
n.handlers = handlers
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -248,6 +255,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
|||||||
} else { // Empty tree
|
} else { // Empty tree
|
||||||
n.insertChild(numParams, path, fullPath, handlers)
|
n.insertChild(numParams, path, fullPath, handlers)
|
||||||
n.nType = root
|
n.nType = root
|
||||||
|
n.allPath=path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,6 +368,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert remaining path part and handle to the leaf
|
// insert remaining path part and handle to the leaf
|
||||||
|
fmt.Println("wsh21:",path[offset:])
|
||||||
n.path = path[offset:]
|
n.path = path[offset:]
|
||||||
n.handlers = handlers
|
n.handlers = handlers
|
||||||
}
|
}
|
||||||
@ -369,7 +378,11 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
|
|||||||
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
|
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
|
||||||
// made if a handle exists with an extra (without the) trailing slash for the
|
// made if a handle exists with an extra (without the) trailing slash for the
|
||||||
// given path.
|
// given path.
|
||||||
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool) {
|
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool,regPath string) {
|
||||||
|
regPath=""
|
||||||
|
defer func() {
|
||||||
|
fmt.Println("tsr:",tsr,"regpath:",regPath)
|
||||||
|
}()
|
||||||
p = po
|
p = po
|
||||||
walk: // Outer loop for walking the tree
|
walk: // Outer loop for walking the tree
|
||||||
for {
|
for {
|
||||||
@ -384,6 +397,8 @@ walk: // Outer loop for walking the tree
|
|||||||
for i := 0; i < len(n.indices); i++ {
|
for i := 0; i < len(n.indices); i++ {
|
||||||
if c == n.indices[i] {
|
if c == n.indices[i] {
|
||||||
n = n.children[i]
|
n = n.children[i]
|
||||||
|
regPath+=n.path
|
||||||
|
fmt.Println("path4:",n.path,regPath)
|
||||||
continue walk
|
continue walk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -412,7 +427,11 @@ walk: // Outer loop for walking the tree
|
|||||||
i := len(p)
|
i := len(p)
|
||||||
p = p[:i+1] // expand slice within preallocated capacity
|
p = p[:i+1] // expand slice within preallocated capacity
|
||||||
p[i].Key = n.path[1:]
|
p[i].Key = n.path[1:]
|
||||||
|
|
||||||
val := path[:end]
|
val := path[:end]
|
||||||
|
|
||||||
|
regPath+=n.path
|
||||||
|
fmt.Println("path6:",n.path,regPath)
|
||||||
if unescape {
|
if unescape {
|
||||||
var err error
|
var err error
|
||||||
if p[i].Value, err = url.QueryUnescape(val); err != nil {
|
if p[i].Value, err = url.QueryUnescape(val); err != nil {
|
||||||
@ -442,6 +461,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]
|
||||||
|
|
||||||
tsr = n.path == "/" && n.handlers != nil
|
tsr = n.path == "/" && n.handlers != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -455,6 +475,7 @@ walk: // Outer loop for walking the tree
|
|||||||
i := len(p)
|
i := len(p)
|
||||||
p = p[:i+1] // expand slice within preallocated capacity
|
p = p[:i+1] // expand slice within preallocated capacity
|
||||||
p[i].Key = n.path[2:]
|
p[i].Key = n.path[2:]
|
||||||
|
fmt.Println("path5:",n.path,n.allPath)
|
||||||
if unescape {
|
if unescape {
|
||||||
var err error
|
var err error
|
||||||
if p[i].Value, err = url.QueryUnescape(path); err != nil {
|
if p[i].Value, err = url.QueryUnescape(path); err != nil {
|
||||||
@ -472,6 +493,8 @@ walk: // Outer loop for walking the tree
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if path == n.path {
|
} else if path == n.path {
|
||||||
|
fmt.Println("path2:",n.path)
|
||||||
|
regPath+=n.path
|
||||||
// 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 handlers = n.handlers; handlers != nil {
|
if handlers = n.handlers; handlers != nil {
|
||||||
@ -487,6 +510,7 @@ walk: // Outer loop for walking the tree
|
|||||||
// trailing slash exists for trailing slash recommendation
|
// trailing slash exists for trailing slash recommendation
|
||||||
for i := 0; i < len(n.indices); i++ {
|
for i := 0; i < len(n.indices); i++ {
|
||||||
if n.indices[i] == '/' {
|
if n.indices[i] == '/' {
|
||||||
|
fmt.Println("path3:",n.children[i].path)
|
||||||
n = n.children[i]
|
n = n.children[i]
|
||||||
tsr = (len(n.path) == 1 && n.handlers != nil) ||
|
tsr = (len(n.path) == 1 && n.handlers != nil) ||
|
||||||
(n.nType == catchAll && n.children[0].handlers != nil)
|
(n.nType == catchAll && n.children[0].handlers != nil)
|
||||||
@ -502,6 +526,7 @@ walk: // Outer loop for walking the tree
|
|||||||
tsr = (path == "/") ||
|
tsr = (path == "/") ||
|
||||||
(len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
|
(len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
|
||||||
path == n.path[:len(n.path)-1] && n.handlers != nil)
|
path == n.path[:len(n.path)-1] && n.handlers != nil)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user