From f364636816c6ed9e85e20ca402e3fd9808784d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=88=E8=99=8E?= Date: Fri, 15 Feb 2019 15:02:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E8=B0=83=E8=AF=95fmt.Pri?= =?UTF-8?q?ntln?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context.go | 7 ++----- debug.go | 3 +-- gin.go | 6 +++--- tree.go | 20 ++++---------------- 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/context.go b/context.go index a3819239..1462c6e0 100644 --- a/context.go +++ b/context.go @@ -20,7 +20,6 @@ import ( "github.com/gin-contrib/sse" "github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/render" - "fmt" ) // Content-Type MIME of the most common data formats. @@ -54,6 +53,8 @@ type Context struct { // Keys is a key/value pair exclusively for the context of each request. Keys map[string]interface{} + RelativePath string + // Errors is a list of errors attached to all the handlers/middlewares who used this context. Errors errorMsgs @@ -635,10 +636,6 @@ 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. // Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP. 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 { clientIP := c.requestHeader("X-Forwarded-For") clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0]) diff --git a/debug.go b/debug.go index b078fd74..586bade6 100644 --- a/debug.go +++ b/debug.go @@ -6,12 +6,12 @@ package gin import ( "bytes" - "fmt" "html/template" "os" "runtime" "strconv" "strings" + "fmt" ) const ginSupportMinGoVer = 6 @@ -30,7 +30,6 @@ func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) { nuHandlers := len(handlers) handlerName := nameOfFunction(handlers.Last()) if DebugPrintRouteFunc == nil { - fmt.Println("wsh1:",absolutePath) debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) } else { DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers) diff --git a/gin.go b/gin.go index a946af46..4b9264f9 100644 --- a/gin.go +++ b/gin.go @@ -5,12 +5,12 @@ package gin import ( - "fmt" "html/template" "net" "net/http" "os" "sync" + "fmt" "github.com/gin-gonic/gin/render" ) @@ -376,11 +376,11 @@ func (engine *Engine) handleHTTPRequest(c *Context) { } root := t[i].root // Find route in tree - handlers, params, tsr,_ := root.getValue(path, c.Params, unescape) + handlers, params, tsr,relativePath := root.getValue(path, c.Params, unescape) if handlers != nil { c.handlers = handlers c.Params = params - fmt.Println("params:",params) + c.RelativePath=relativePath c.Next() c.writermem.WriteHeaderNow() return diff --git a/tree.go b/tree.go index 3bb0341b..a4cc92d6 100644 --- a/tree.go +++ b/tree.go @@ -8,7 +8,6 @@ import ( "net/url" "strings" "unicode" - "fmt" ) // Param is a single URL parameter, consisting of a key and a value. @@ -125,7 +124,6 @@ func (n *node) incrementChildPrio(pos int) int { // addRoute adds a node with the given handle to the path. // Not concurrency-safe! func (n *node) addRoute(path string, handlers HandlersChain) { - fmt.Println("wsh66:",path) fullPath := path n.priority++ numParams := countParams(path) @@ -368,7 +366,6 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle } // insert remaining path part and handle to the leaf - fmt.Println("wsh21:",path[offset:]) n.path = path[offset:] n.handlers = handlers } @@ -378,11 +375,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle // 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 // given path. -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) - }() +func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool,relativePath string) { p = po walk: // Outer loop for walking the tree for { @@ -397,8 +390,7 @@ walk: // Outer loop for walking the tree for i := 0; i < len(n.indices); i++ { if c == n.indices[i] { n = n.children[i] - regPath+=n.path - fmt.Println("path4:",n.path,regPath) + relativePath+=n.path continue walk } } @@ -430,8 +422,7 @@ walk: // Outer loop for walking the tree val := path[:end] - regPath+=n.path - fmt.Println("path6:",n.path,regPath) + relativePath+=n.path if unescape { var err error if p[i].Value, err = url.QueryUnescape(val); err != nil { @@ -475,7 +466,6 @@ walk: // Outer loop for walking the tree i := len(p) p = p[:i+1] // expand slice within preallocated capacity p[i].Key = n.path[2:] - fmt.Println("path5:",n.path,n.allPath) if unescape { var err error if p[i].Value, err = url.QueryUnescape(path); err != nil { @@ -493,8 +483,7 @@ walk: // Outer loop for walking the tree } } } else if path == n.path { - fmt.Println("path2:",n.path) - regPath+=n.path + relativePath+=n.path // We should have reached the node containing the handle. // Check if this node has a handle registered. if handlers = n.handlers; handlers != nil { @@ -510,7 +499,6 @@ walk: // Outer loop for walking the tree // trailing slash exists for trailing slash recommendation for i := 0; i < len(n.indices); i++ { if n.indices[i] == '/' { - fmt.Println("path3:",n.children[i].path) n = n.children[i] tsr = (len(n.path) == 1 && n.handlers != nil) || (n.nType == catchAll && n.children[0].handlers != nil) From edc547beaa14417aaaf336bdb09ed9d163deaf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=88=E8=99=8E?= Date: Fri, 15 Feb 2019 15:15:06 +0800 Subject: [PATCH 2/4] format code,Remove useless code --- debug.go | 2 +- gin.go | 2 +- tree.go | 6 ------ 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/debug.go b/debug.go index 586bade6..98c67cf7 100644 --- a/debug.go +++ b/debug.go @@ -6,12 +6,12 @@ package gin import ( "bytes" + "fmt" "html/template" "os" "runtime" "strconv" "strings" - "fmt" ) const ginSupportMinGoVer = 6 diff --git a/gin.go b/gin.go index 4b9264f9..24bf7d78 100644 --- a/gin.go +++ b/gin.go @@ -5,12 +5,12 @@ package gin import ( + "fmt" "html/template" "net" "net/http" "os" "sync" - "fmt" "github.com/gin-gonic/gin/render" ) diff --git a/tree.go b/tree.go index a4cc92d6..b961e3a9 100644 --- a/tree.go +++ b/tree.go @@ -94,7 +94,6 @@ type node struct { nType nodeType maxParams uint8 wildChild bool - allPath string } // increments priority of the given child and reorders if necessary. @@ -154,7 +153,6 @@ func (n *node) addRoute(path string, handlers HandlersChain) { indices: n.indices, children: n.children, handlers: n.handlers, - allPath:path, priority: n.priority - 1, } @@ -170,7 +168,6 @@ func (n *node) addRoute(path string, handlers HandlersChain) { n.indices = string([]byte{n.path[i]}) n.path = path[:i] n.handlers = nil - n.allPath=path n.wildChild = false } @@ -238,14 +235,12 @@ func (n *node) addRoute(path string, handlers HandlersChain) { n = child } n.insertChild(numParams, path, fullPath, handlers) - n.allPath=path return } else if i == len(path) { // Make node a (in-path) leaf if n.handlers != nil { panic("handlers are already registered for path '" + fullPath + "'") } - n.allPath=path n.handlers = handlers } return @@ -253,7 +248,6 @@ func (n *node) addRoute(path string, handlers HandlersChain) { } else { // Empty tree n.insertChild(numParams, path, fullPath, handlers) n.nType = root - n.allPath=path } } From fa7c11124a68e160a776144e83a0742639ac9a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=88=E8=99=8E?= Date: Fri, 15 Feb 2019 16:16:32 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96relativ?= =?UTF-8?q?ePath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tree.go b/tree.go index b961e3a9..85bd7d1f 100644 --- a/tree.go +++ b/tree.go @@ -8,6 +8,7 @@ import ( "net/url" "strings" "unicode" + "fmt" ) // Param is a single URL parameter, consisting of a key and a value. @@ -379,12 +380,12 @@ walk: // Outer loop for walking the tree // If this node does not have a wildcard (param or catchAll) // child, we can just look up the next child node and continue // to walk down the tree + relativePath+=n.path if !n.wildChild { c := path[0] for i := 0; i < len(n.indices); i++ { if c == n.indices[i] { n = n.children[i] - relativePath+=n.path continue walk } } From d826e86f2d6a68b1a581f7af7c802de9a440d40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=88=E8=99=8E?= Date: Fri, 15 Feb 2019 16:21:36 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96relativ?= =?UTF-8?q?ePath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tree.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tree.go b/tree.go index 85bd7d1f..d387eb85 100644 --- a/tree.go +++ b/tree.go @@ -8,7 +8,6 @@ import ( "net/url" "strings" "unicode" - "fmt" ) // Param is a single URL parameter, consisting of a key and a value.