From 235898e64285eb5c2cfdd485f21220e4deab3006 Mon Sep 17 00:00:00 2001 From: bn4t <17193640+bn4t@users.noreply.github.com> Date: Tue, 5 May 2020 15:20:00 +0000 Subject: [PATCH 01/32] Fix typo (#2358) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d75843a7..5c145d5a 100644 --- a/README.md +++ b/README.md @@ -1763,7 +1763,7 @@ func main() { // kill -9 is syscall.SIGKILL but can't be catch, so don't need add it signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) <-quit - log.Println("Shuting down server...") + log.Println("Shutting down server...") // The context is used to inform the server it has 5 seconds to finish // the request it is currently handling From 82b284fd774d6bf68c934a3f335b816c61edf66f Mon Sep 17 00:00:00 2001 From: Anup Kumar Panwar <1anuppanwar@gmail.com> Date: Fri, 8 May 2020 06:20:26 +0530 Subject: [PATCH 02/32] uncomment the code (#2362) --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c145d5a..9cc23fc2 100644 --- a/README.md +++ b/README.md @@ -357,14 +357,14 @@ References issue [#774](https://github.com/gin-gonic/gin/issues/774) and detail func main() { router := gin.Default() // Set a lower memory limit for multipart forms (default is 32 MiB) - // router.MaxMultipartMemory = 8 << 20 // 8 MiB + router.MaxMultipartMemory = 8 << 20 // 8 MiB router.POST("/upload", func(c *gin.Context) { // single file file, _ := c.FormFile("file") log.Println(file.Filename) // Upload the file to specific dst. - // c.SaveUploadedFile(file, dst) + c.SaveUploadedFile(file, dst) c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) }) @@ -388,7 +388,7 @@ See the detail [example code](https://github.com/gin-gonic/examples/tree/master/ func main() { router := gin.Default() // Set a lower memory limit for multipart forms (default is 32 MiB) - // router.MaxMultipartMemory = 8 << 20 // 8 MiB + router.MaxMultipartMemory = 8 << 20 // 8 MiB router.POST("/upload", func(c *gin.Context) { // Multipart form form, _ := c.MultipartForm() @@ -398,7 +398,7 @@ func main() { log.Println(file.Filename) // Upload the file to specific dst. - // c.SaveUploadedFile(file, dst) + c.SaveUploadedFile(file, dst) } c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files))) }) From 6f3d96ccff56290fd506dff68af21a56a22d0873 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 9 May 2020 17:41:00 +0800 Subject: [PATCH 03/32] chore: improve render string performance (#2365) --- render/text.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/render/text.go b/render/text.go index 4e52d4c5..30f5f532 100644 --- a/render/text.go +++ b/render/text.go @@ -6,7 +6,6 @@ package render import ( "fmt" - "io" "net/http" ) @@ -35,6 +34,6 @@ func WriteString(w http.ResponseWriter, format string, data []interface{}) (err _, err = fmt.Fprintf(w, format, data...) return } - _, err = io.WriteString(w, format) + _, err = w.Write([]byte(format)) return } From d17270dd90c488308de3102d6951946ca0a5911f Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 10 May 2020 13:22:25 +0800 Subject: [PATCH 04/32] Sync route tree to httprouter latest code (#2368) * update tree Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update countParams Signed-off-by: Bo-Yi Wu * fix testing Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * update Signed-off-by: Bo-Yi Wu * udpate Signed-off-by: Bo-Yi Wu * fix testing Signed-off-by: Bo-Yi Wu * refactor gin context Signed-off-by: Bo-Yi Wu * add fullPath Signed-off-by: Bo-Yi Wu * chore: refactor * remove unused code Signed-off-by: Bo-Yi Wu * remove varsCount Signed-off-by: Bo-Yi Wu * refactor Signed-off-by: Bo-Yi Wu --- context.go | 2 + gin.go | 16 +- tree.go | 609 +++++++++++++++++++++++++++++---------------------- tree_test.go | 84 ++++--- 4 files changed, 411 insertions(+), 300 deletions(-) diff --git a/context.go b/context.go index 4ebcc294..2ea1a028 100644 --- a/context.go +++ b/context.go @@ -54,6 +54,7 @@ type Context struct { fullPath string engine *Engine + params *Params // This mutex protect Keys map mu sync.RWMutex @@ -95,6 +96,7 @@ func (c *Context) reset() { c.Accepted = nil c.queryCache = nil c.formCache = nil + *c.params = (*c.params)[0:0] } // Copy returns a copy of the current context that can be safely used outside the request's scope. diff --git a/gin.go b/gin.go index 4e72f81d..1e126179 100644 --- a/gin.go +++ b/gin.go @@ -113,6 +113,7 @@ type Engine struct { noMethod HandlersChain pool sync.Pool trees methodTrees + maxParams uint16 } var _ IRouter = &Engine{} @@ -163,7 +164,8 @@ func Default() *Engine { } func (engine *Engine) allocateContext() *Context { - return &Context{engine: engine} + v := make(Params, 0, engine.maxParams) + return &Context{engine: engine, params: &v} } // Delims sets template left and right delims and returns a Engine instance. @@ -256,6 +258,7 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { assert1(len(handlers) > 0, "there must be at least one handler") debugPrintRoute(method, path, handlers) + root := engine.trees.get(method) if root == nil { root = new(node) @@ -263,6 +266,11 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { engine.trees = append(engine.trees, methodTree{method: method, root: root}) } root.addRoute(path, handlers) + + // Update maxParams + if paramsCount := countParams(path); paramsCount > engine.maxParams { + engine.maxParams = paramsCount + } } // Routes returns a slice of registered routes, including some useful information, such as: @@ -402,10 +410,12 @@ func (engine *Engine) handleHTTPRequest(c *Context) { } root := t[i].root // Find route in tree - value := root.getValue(rPath, c.Params, unescape) + value := root.getValue(rPath, c.params, unescape) + if value.params != nil { + c.Params = *value.params + } if value.handlers != nil { c.handlers = value.handlers - c.Params = value.params c.fullPath = value.fullPath c.Next() c.writermem.WriteHeaderNow() diff --git a/tree.go b/tree.go index b687ec43..e3aa9190 100644 --- a/tree.go +++ b/tree.go @@ -8,6 +8,7 @@ import ( "net/url" "strings" "unicode" + "unicode/utf8" ) // Param is a single URL parameter, consisting of a key and a value. @@ -71,17 +72,15 @@ func longestCommonPrefix(a, b string) int { return i } -func countParams(path string) uint8 { +func countParams(path string) uint16 { var n uint - for i := 0; i < len(path); i++ { - if path[i] == ':' || path[i] == '*' { + for i := range []byte(path) { + switch path[i] { + case ':', '*': n++ } } - if n >= 255 { - return 255 - } - return uint8(n) + return uint16(n) } type nodeType uint8 @@ -96,16 +95,15 @@ const ( type node struct { path string indices string + wildChild bool + nType nodeType + priority uint32 children []*node handlers HandlersChain - priority uint32 - nType nodeType - maxParams uint8 - wildChild bool fullPath string } -// increments priority of the given child and reorders if necessary. +// Increments priority of the given child and reorders if necessary func (n *node) incrementChildPrio(pos int) int { cs := n.children cs[pos].priority++ @@ -116,13 +114,14 @@ func (n *node) incrementChildPrio(pos int) int { for ; newPos > 0 && cs[newPos-1].priority < prio; newPos-- { // Swap node positions cs[newPos-1], cs[newPos] = cs[newPos], cs[newPos-1] + } - // build new index char string + // Build new index char string if newPos != pos { - n.indices = n.indices[:newPos] + // unchanged prefix, might be empty - n.indices[pos:pos+1] + // the index char we move - n.indices[newPos:pos] + n.indices[pos+1:] // rest without char at 'pos' + n.indices = n.indices[:newPos] + // Unchanged prefix, might be empty + n.indices[pos:pos+1] + // The index char we move + n.indices[newPos:pos] + n.indices[pos+1:] // Rest without char at 'pos' } return newPos @@ -133,11 +132,10 @@ func (n *node) incrementChildPrio(pos int) int { func (n *node) addRoute(path string, handlers HandlersChain) { fullPath := path n.priority++ - numParams := countParams(path) // Empty tree if len(n.path) == 0 && len(n.children) == 0 { - n.insertChild(numParams, path, fullPath, handlers) + n.insertChild(path, fullPath, handlers) n.nType = root return } @@ -146,11 +144,6 @@ func (n *node) addRoute(path string, handlers HandlersChain) { walk: for { - // Update maxParams of the current node - if numParams > n.maxParams { - n.maxParams = numParams - } - // Find the longest common prefix. // This also implies that the common prefix contains no ':' or '*' // since the existing key can't contain those chars. @@ -168,13 +161,6 @@ walk: fullPath: n.fullPath, } - // Update maxParams (max of all children) - for _, v := range child.children { - if v.maxParams > child.maxParams { - child.maxParams = v.maxParams - } - } - n.children = []*node{&child} // []byte for proper unicode char conversion, see #65 n.indices = string([]byte{n.path[i]}) @@ -193,18 +179,13 @@ walk: n = n.children[0] n.priority++ - // Update maxParams of the child node - if numParams > n.maxParams { - n.maxParams = numParams - } - numParams-- - // Check if the wildcard matches - if len(path) >= len(n.path) && n.path == path[:len(n.path)] { - // check for longer wildcard, e.g. :name and :names - if len(n.path) >= len(path) || path[len(n.path)] == '/' { - continue walk - } + if len(path) >= len(n.path) && n.path == path[:len(n.path)] && + // Adding a child to a catchAll is not possible + n.nType != catchAll && + // Check for longer wildcard, e.g. :name and :names + (len(n.path) >= len(path) || path[len(n.path)] == '/') { + continue walk } pathSeg := path @@ -244,14 +225,13 @@ walk: // []byte for proper unicode char conversion, see #65 n.indices += string([]byte{c}) child := &node{ - maxParams: numParams, - fullPath: fullPath, + fullPath: fullPath, } n.children = append(n.children, child) n.incrementChildPrio(len(n.indices) - 1) n = child } - n.insertChild(numParams, path, fullPath, handlers) + n.insertChild(path, fullPath, handlers) return } @@ -265,7 +245,7 @@ walk: } // Search for a wildcard segment and check the name for invalid characters. -// Returns -1 as index, if no wildcard war found. +// Returns -1 as index, if no wildcard was found. func findWildcard(path string) (wildcard string, i int, valid bool) { // Find start for start, c := range []byte(path) { @@ -289,8 +269,8 @@ func findWildcard(path string) (wildcard string, i int, valid bool) { return "", -1, false } -func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) { - for numParams > 0 { +func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) { + for { // Find prefix until first wildcard wildcard, i, valid := findWildcard(path) if i < 0 { // No wildcard found @@ -324,15 +304,13 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle n.wildChild = true child := &node{ - nType: param, - path: wildcard, - maxParams: numParams, - fullPath: fullPath, + nType: param, + path: wildcard, + fullPath: fullPath, } n.children = []*node{child} n = child n.priority++ - numParams-- // if the path doesn't end with the wildcard, then there // will be another non-wildcard subpath starting with '/' @@ -340,9 +318,8 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle path = path[len(wildcard):] child := &node{ - maxParams: numParams, - priority: 1, - fullPath: fullPath, + priority: 1, + fullPath: fullPath, } n.children = []*node{child} n = child @@ -355,7 +332,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle } // catchAll - if i+len(wildcard) != len(path) || numParams > 1 { + if i+len(wildcard) != len(path) { panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'") } @@ -375,13 +352,9 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle child := &node{ wildChild: true, nType: catchAll, - maxParams: 1, fullPath: fullPath, } - // update maxParams of the parent node - if n.maxParams < 1 { - n.maxParams = 1 - } + n.children = []*node{child} n.indices = string('/') n = child @@ -389,12 +362,11 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle // second node: node holding the variable child = &node{ - path: path[i:], - nType: catchAll, - maxParams: 1, - handlers: handlers, - priority: 1, - fullPath: fullPath, + path: path[i:], + nType: catchAll, + handlers: handlers, + priority: 1, + fullPath: fullPath, } n.children = []*node{child} @@ -410,21 +382,128 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle // nodeValue holds return values of (*Node).getValue method type nodeValue struct { handlers HandlersChain - params Params + params *Params tsr bool fullPath string } -// getValue returns the handle registered with the given path (key). The values of +// Returns the handle registered with the given path (key). The values of // wildcards are saved to a map. // 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) (value nodeValue) { - value.params = po +func (n *node) getValue(path string, params *Params, unescape bool) (value nodeValue) { walk: // Outer loop for walking the tree for { prefix := n.path + if len(path) > len(prefix) { + if path[:len(prefix)] == prefix { + path = path[len(prefix):] + // 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 + if !n.wildChild { + idxc := path[0] + for i, c := range []byte(n.indices) { + if c == idxc { + n = n.children[i] + 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 + } + + // Handle wildcard child + 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 params != nil { + if value.params == nil { + 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: val, + } + } + + // we need to go deeper! + if end < len(path) { + if len(n.children) > 0 { + 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] + value.tsr = (n.path == "/" && n.handlers != nil) + } + return + + case catchAll: + // 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] + val := path + if unescape { + if v, err := url.QueryUnescape(path); err == nil { + val = v + } + } + (*value.params)[i] = Param{ + Key: n.path[2:], + Value: val, + } + } + + value.handlers = n.handlers + value.fullPath = n.fullPath + return + + default: + panic("invalid node type") + } + } + } + if path == prefix { // We should have reached the node containing the handle. // Check if this node has a handle registered. @@ -433,6 +512,9 @@ walk: // Outer loop for walking the tree return } + // If there is no handle for this route, but this route has a + // wildcard child, there must be a handle for this path with an + // additional trailing slash if path == "/" && n.wildChild && n.nType != root { value.tsr = true return @@ -440,9 +522,8 @@ walk: // Outer loop for walking the tree // No handle found. Check if a handle for this path + a // trailing slash exists for trailing slash recommendation - indices := n.indices - for i, max := 0, len(indices); i < max; i++ { - if indices[i] == '/' { + for i, c := range []byte(n.indices) { + if c == '/' { n = n.children[i] value.tsr = (len(n.path) == 1 && n.handlers != nil) || (n.nType == catchAll && n.children[0].handlers != nil) @@ -453,106 +534,6 @@ walk: // Outer loop for walking the tree return } - if len(path) > len(prefix) && path[:len(prefix)] == prefix { - path = path[len(prefix):] - // 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 - if !n.wildChild { - c := path[0] - indices := n.indices - for i, max := 0, len(indices); i < max; i++ { - if c == indices[i] { - n = n.children[i] - 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 - } - - // handle wildcard child - 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 - } - - // we need to go deeper! - if end < len(path) { - if len(n.children) > 0 { - 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] - 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 - } - - value.handlers = n.handlers - value.fullPath = n.fullPath - return - - default: - panic("invalid node type") - } - } - // Nothing found. We can recommend to redirect to the same URL with an // extra trailing slash if a leaf exists for that path value.tsr = (path == "/") || @@ -562,109 +543,214 @@ walk: // Outer loop for walking the tree } } -// findCaseInsensitivePath makes a case-insensitive lookup of the given path and tries to find a handler. +// Makes a case-insensitive lookup of the given path and tries to find a handler. // It can optionally also fix trailing slashes. // It returns the case-corrected path and a bool indicating whether the lookup // was successful. -func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPath []byte, found bool) { - ciPath = make([]byte, 0, len(path)+1) // preallocate enough memory +func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) ([]byte, bool) { + const stackBufSize = 128 - // Outer loop for walking the tree - for len(path) >= len(n.path) && strings.EqualFold(path[:len(n.path)], n.path) { - path = path[len(n.path):] + // Use a static sized buffer on the stack in the common case. + // If the path is too long, allocate a buffer on the heap instead. + buf := make([]byte, 0, stackBufSize) + if l := len(path) + 1; l > stackBufSize { + buf = make([]byte, 0, l) + } + + ciPath := n.findCaseInsensitivePathRec( + path, + buf, // Preallocate enough memory for new path + [4]byte{}, // Empty rune buffer + fixTrailingSlash, + ) + + return ciPath, ciPath != nil +} + +// Shift bytes in array by n bytes left +func shiftNRuneBytes(rb [4]byte, n int) [4]byte { + switch n { + case 0: + return rb + case 1: + return [4]byte{rb[1], rb[2], rb[3], 0} + case 2: + return [4]byte{rb[2], rb[3]} + case 3: + return [4]byte{rb[3]} + default: + return [4]byte{} + } +} + +// Recursive case-insensitive lookup function used by n.findCaseInsensitivePath +func (n *node) findCaseInsensitivePathRec(path string, ciPath []byte, rb [4]byte, fixTrailingSlash bool) []byte { + npLen := len(n.path) + +walk: // Outer loop for walking the tree + for len(path) >= npLen && (npLen == 0 || strings.EqualFold(path[1:npLen], n.path[1:])) { + // Add common prefix to result + oldPath := path + path = path[npLen:] ciPath = append(ciPath, n.path...) - if len(path) == 0 { + if len(path) > 0 { + // 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 + if !n.wildChild { + // Skip rune bytes already processed + rb = shiftNRuneBytes(rb, npLen) + + if rb[0] != 0 { + // Old rune not finished + idxc := rb[0] + for i, c := range []byte(n.indices) { + if c == idxc { + // continue with child node + n = n.children[i] + npLen = len(n.path) + continue walk + } + } + } else { + // Process a new rune + var rv rune + + // Find rune start. + // Runes are up to 4 byte long, + // -4 would definitely be another rune. + var off int + for max := min(npLen, 3); off < max; off++ { + if i := npLen - off; utf8.RuneStart(oldPath[i]) { + // read rune from cached path + rv, _ = utf8.DecodeRuneInString(oldPath[i:]) + break + } + } + + // Calculate lowercase bytes of current rune + lo := unicode.ToLower(rv) + utf8.EncodeRune(rb[:], lo) + + // Skip already processed bytes + rb = shiftNRuneBytes(rb, off) + + idxc := rb[0] + for i, c := range []byte(n.indices) { + // Lowercase matches + if c == idxc { + // must use a recursive approach since both the + // uppercase byte and the lowercase byte might exist + // as an index + if out := n.children[i].findCaseInsensitivePathRec( + path, ciPath, rb, fixTrailingSlash, + ); out != nil { + return out + } + break + } + } + + // If we found no match, the same for the uppercase rune, + // if it differs + if up := unicode.ToUpper(rv); up != lo { + utf8.EncodeRune(rb[:], up) + rb = shiftNRuneBytes(rb, off) + + idxc := rb[0] + for i, c := range []byte(n.indices) { + // Uppercase matches + if c == idxc { + // Continue with child node + n = n.children[i] + npLen = len(n.path) + continue walk + } + } + } + } + + // Nothing found. We can recommend to redirect to the same URL + // without a trailing slash if a leaf exists for that path + if fixTrailingSlash && path == "/" && n.handlers != nil { + return ciPath + } + return nil + } + + 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++ + } + + // Add param value to case insensitive path + ciPath = append(ciPath, path[:end]...) + + // We need to go deeper! + if end < len(path) { + if len(n.children) > 0 { + // Continue with child node + n = n.children[0] + npLen = len(n.path) + path = path[end:] + continue + } + + // ... but we can't + if fixTrailingSlash && len(path) == end+1 { + return ciPath + } + return nil + } + + if n.handlers != nil { + return ciPath + } + + if fixTrailingSlash && len(n.children) == 1 { + // No handle found. Check if a handle for this path + a + // trailing slash exists + n = n.children[0] + if n.path == "/" && n.handlers != nil { + return append(ciPath, '/') + } + } + + return nil + + case catchAll: + return append(ciPath, path...) + + default: + panic("invalid node type") + } + } else { // We should have reached the node containing the handle. // Check if this node has a handle registered. if n.handlers != nil { - return ciPath, true + return ciPath } // No handle found. // Try to fix the path by adding a trailing slash if fixTrailingSlash { - for i := 0; i < len(n.indices); i++ { - if n.indices[i] == '/' { + for i, c := range []byte(n.indices) { + if c == '/' { n = n.children[i] if (len(n.path) == 1 && n.handlers != nil) || (n.nType == catchAll && n.children[0].handlers != nil) { - return append(ciPath, '/'), true + return append(ciPath, '/') } - return + return nil } } } - return - } - - // 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 - if !n.wildChild { - r := unicode.ToLower(rune(path[0])) - for i, index := range n.indices { - // must use recursive approach since both index and - // ToLower(index) could exist. We must check both. - if r == unicode.ToLower(index) { - out, found := n.children[i].findCaseInsensitivePath(path, fixTrailingSlash) - if found { - return append(ciPath, out...), true - } - } - } - - // Nothing found. We can recommend to redirect to the same URL - // without a trailing slash if a leaf exists for that path - found = fixTrailingSlash && path == "/" && n.handlers != nil - return - } - - 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++ - } - - // add param value to case insensitive path - ciPath = append(ciPath, path[:end]...) - - // we need to go deeper! - if end < len(path) { - if len(n.children) > 0 { - path = path[end:] - n = n.children[0] - continue - } - - // ... but we can't - if fixTrailingSlash && len(path) == end+1 { - return ciPath, true - } - return - } - - if n.handlers != nil { - return ciPath, true - } - if fixTrailingSlash && len(n.children) == 1 { - // No handle found. Check if a handle for this path + a - // trailing slash exists - n = n.children[0] - if n.path == "/" && n.handlers != nil { - return append(ciPath, '/'), true - } - } - return - - case catchAll: - return append(ciPath, path...), true - - default: - panic("invalid node type") + return nil } } @@ -672,13 +758,12 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPa // Try to fix the path by adding / removing a trailing slash if fixTrailingSlash { if path == "/" { - return ciPath, true + return ciPath } - if len(path)+1 == len(n.path) && n.path[len(path)] == '/' && - strings.EqualFold(path, n.path[:len(path)]) && - n.handlers != nil { - return append(ciPath, n.path...), true + if len(path)+1 == npLen && n.path[len(path)] == '/' && + strings.EqualFold(path[1:], n.path[1:len(path)]) && n.handlers != nil { + return append(ciPath, n.path...) } } - return + return nil } diff --git a/tree_test.go b/tree_test.go index 0fe2fe00..1cb4f559 100644 --- a/tree_test.go +++ b/tree_test.go @@ -28,6 +28,11 @@ type testRequests []struct { ps Params } +func getParams() *Params { + ps := make(Params, 0, 20) + return &ps +} + func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes ...bool) { unescape := false if len(unescapes) >= 1 { @@ -35,7 +40,7 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes .. } for _, request := range requests { - value := tree.getValue(request.path, nil, unescape) + value := tree.getValue(request.path, getParams(), unescape) if value.handlers == nil { if !request.nilHandler { @@ -50,9 +55,12 @@ func checkRequests(t *testing.T, tree *node, requests testRequests, unescapes .. } } - if !reflect.DeepEqual(value.params, request.ps) { - t.Errorf("Params mismatch for route '%s'", request.path) + if value.params != nil { + if !reflect.DeepEqual(*value.params, request.ps) { + t.Errorf("Params mismatch for route '%s'", request.path) + } } + } } @@ -76,33 +84,11 @@ func checkPriorities(t *testing.T, n *node) uint32 { return prio } -func checkMaxParams(t *testing.T, n *node) uint8 { - var maxParams uint8 - for i := range n.children { - params := checkMaxParams(t, n.children[i]) - if params > maxParams { - maxParams = params - } - } - if n.nType > root && !n.wildChild { - maxParams++ - } - - if n.maxParams != maxParams { - t.Errorf( - "maxParams mismatch for node '%s': is %d, should be %d", - n.path, n.maxParams, maxParams, - ) - } - - return maxParams -} - func TestCountParams(t *testing.T) { if countParams("/path/:param1/static/*catch-all") != 2 { t.Fail() } - if countParams(strings.Repeat("/:param", 256)) != 255 { + if countParams(strings.Repeat("/:param", 256)) != 256 { t.Fail() } } @@ -142,7 +128,6 @@ func TestTreeAddAndGet(t *testing.T) { }) checkPriorities(t, tree) - checkMaxParams(t, tree) } func TestTreeWildcard(t *testing.T) { @@ -186,7 +171,6 @@ func TestTreeWildcard(t *testing.T) { }) checkPriorities(t, tree) - checkMaxParams(t, tree) } func TestUnescapeParameters(t *testing.T) { @@ -224,7 +208,6 @@ func TestUnescapeParameters(t *testing.T) { }, unescape) checkPriorities(t, tree) - checkMaxParams(t, tree) } func catchPanic(testFunc func()) (recv interface{}) { @@ -323,12 +306,14 @@ func TestTreeDupliatePath(t *testing.T) { } } + //printChildren(tree, "") + checkRequests(t, tree, testRequests{ {"/", false, "/", nil}, {"/doc/", false, "/doc/", nil}, - {"/src/some/file.png", false, "/src/*filepath", Params{Param{Key: "filepath", Value: "/some/file.png"}}}, - {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{Key: "query", Value: "someth!ng+in+ünìcodé"}}}, - {"/user_gopher", false, "/user_:name", Params{Param{Key: "name", Value: "gopher"}}}, + {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}}, + {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}}, + {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}}, }) } @@ -356,6 +341,8 @@ func TestTreeCatchAllConflict(t *testing.T) { {"/src/*filepath/x", true}, {"/src2/", false}, {"/src2/*filepath/x", true}, + {"/src3/*filepath", false}, + {"/src3/*filepath/x", true}, } testRoutes(t, routes) } @@ -372,7 +359,6 @@ func TestTreeCatchMaxParams(t *testing.T) { tree := &node{} var route = "/cmd/*filepath" tree.addRoute(route, fakeHandler(route)) - checkMaxParams(t, tree) } func TestTreeDoubleWildcard(t *testing.T) { @@ -508,6 +494,9 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) { func TestTreeFindCaseInsensitivePath(t *testing.T) { tree := &node{} + longPath := "/l" + strings.Repeat("o", 128) + "ng" + lOngPath := "/l" + strings.Repeat("O", 128) + "ng/" + routes := [...]string{ "/hi", "/b/", @@ -531,6 +520,17 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) { "/doc/go/away", "/no/a", "/no/b", + "/Π", + "/u/apfêl/", + "/u/äpfêl/", + "/u/öpfêl", + "/v/Äpfêl/", + "/v/Öpfêl", + "/w/♬", // 3 byte + "/w/♭/", // 3 byte, last byte differs + "/w/𠜎", // 4 byte + "/w/𠜏/", // 4 byte + longPath, } for _, route := range routes { @@ -609,6 +609,21 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) { {"/DOC/", "/doc", true, true}, {"/NO", "", false, true}, {"/DOC/GO", "", false, true}, + {"/π", "/Π", true, false}, + {"/π/", "/Π", true, true}, + {"/u/ÄPFÊL/", "/u/äpfêl/", true, false}, + {"/u/ÄPFÊL", "/u/äpfêl/", true, true}, + {"/u/ÖPFÊL/", "/u/öpfêl", true, true}, + {"/u/ÖPFÊL", "/u/öpfêl", true, false}, + {"/v/äpfêL/", "/v/Äpfêl/", true, false}, + {"/v/äpfêL", "/v/Äpfêl/", true, true}, + {"/v/öpfêL/", "/v/Öpfêl", true, true}, + {"/v/öpfêL", "/v/Öpfêl", true, false}, + {"/w/♬/", "/w/♬", true, true}, + {"/w/♭", "/w/♭/", true, true}, + {"/w/𠜎/", "/w/𠜎", true, true}, + {"/w/𠜏", "/w/𠜏/", true, true}, + {lOngPath, longPath, true, true}, } // With fixTrailingSlash = true for _, test := range tests { @@ -696,8 +711,7 @@ func TestTreeWildcardConflictEx(t *testing.T) { tree.addRoute(conflict.route, fakeHandler(conflict.route)) }) - if !regexp.MustCompile(fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'", - conflict.segPath, conflict.existSegPath, conflict.existPath)).MatchString(fmt.Sprint(recv)) { + if !regexp.MustCompile(fmt.Sprintf("'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'", conflict.segPath, conflict.existSegPath, conflict.existPath)).MatchString(fmt.Sprint(recv)) { t.Fatalf("invalid wildcard conflict error (%v)", recv) } } From a6e8665e42dad83ee275d4adc34a6e507bdd11ed Mon Sep 17 00:00:00 2001 From: vinhha96 Date: Mon, 11 May 2020 12:25:49 +0700 Subject: [PATCH 05/32] fix(tree): reassign fullpath when register new node which the same current node (#2366) * fix(tree): assign fullpath to current node by fullpath of new node if current node the same new node * test(router-test): reverse the order when register router when test func GetFullPath * chg(tree-test): update test case with register new route in TestRouteContextHoldsFullPath Co-authored-by: vinhha Co-authored-by: Bo-Yi Wu --- routes_test.go | 3 +++ tree.go | 1 + 2 files changed, 4 insertions(+) diff --git a/routes_test.go b/routes_test.go index 360ade62..11ff71a6 100644 --- a/routes_test.go +++ b/routes_test.go @@ -593,6 +593,9 @@ func TestRouteContextHoldsFullPath(t *testing.T) { "/simple-two/one-two", "/project/:name/build/*params", "/project/:name/bui", + "/user/:id/status", + "/user/:id", + "/user/:id/profile", } for _, route := range routes { diff --git a/tree.go b/tree.go index e3aa9190..f528fa3b 100644 --- a/tree.go +++ b/tree.go @@ -240,6 +240,7 @@ walk: panic("handlers are already registered for path '" + fullPath + "'") } n.handlers = handlers + n.fullPath = fullPath return } } From 1d5b9badd97ba578f00a0d45ad46834cca5479ca Mon Sep 17 00:00:00 2001 From: thinkerou Date: Thu, 14 May 2020 11:35:14 +0800 Subject: [PATCH 06/32] chore: rename getQueryCache/getFormCache to initQueryCache/initFormCache (#2375) --- context.go | 12 ++++++------ utils.go | 13 ++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/context.go b/context.go index 2ea1a028..a9458833 100644 --- a/context.go +++ b/context.go @@ -414,7 +414,7 @@ func (c *Context) QueryArray(key string) []string { return values } -func (c *Context) getQueryCache() { +func (c *Context) initQueryCache() { if c.queryCache == nil { c.queryCache = c.Request.URL.Query() } @@ -423,7 +423,7 @@ func (c *Context) getQueryCache() { // GetQueryArray returns a slice of strings for a given query key, plus // a boolean value whether at least one value exists for the given key. func (c *Context) GetQueryArray(key string) ([]string, bool) { - c.getQueryCache() + c.initQueryCache() if values, ok := c.queryCache[key]; ok && len(values) > 0 { return values, true } @@ -439,7 +439,7 @@ func (c *Context) QueryMap(key string) map[string]string { // GetQueryMap returns a map for a given query key, plus a boolean value // whether at least one value exists for the given key. func (c *Context) GetQueryMap(key string) (map[string]string, bool) { - c.getQueryCache() + c.initQueryCache() return c.get(c.queryCache, key) } @@ -481,7 +481,7 @@ func (c *Context) PostFormArray(key string) []string { return values } -func (c *Context) getFormCache() { +func (c *Context) initFormCache() { if c.formCache == nil { c.formCache = make(url.Values) req := c.Request @@ -497,7 +497,7 @@ func (c *Context) getFormCache() { // GetPostFormArray returns a slice of strings for a given form key, plus // a boolean value whether at least one value exists for the given key. func (c *Context) GetPostFormArray(key string) ([]string, bool) { - c.getFormCache() + c.initFormCache() if values := c.formCache[key]; len(values) > 0 { return values, true } @@ -513,7 +513,7 @@ func (c *Context) PostFormMap(key string) map[string]string { // GetPostFormMap returns a map for a given form key, plus a boolean value // whether at least one value exists for the given key. func (c *Context) GetPostFormMap(key string) (map[string]string, bool) { - c.getFormCache() + c.initFormCache() return c.get(c.formCache, key) } diff --git a/utils.go b/utils.go index 71b80de7..fab3aee3 100644 --- a/utils.go +++ b/utils.go @@ -90,13 +90,13 @@ func filterFlags(content string) string { } func chooseData(custom, wildcard interface{}) interface{} { - if custom == nil { - if wildcard == nil { - panic("negotiation config is invalid") - } + if custom != nil { + return custom + } + if wildcard != nil { return wildcard } - return custom + panic("negotiation config is invalid") } func parseAccept(acceptHeader string) []string { @@ -127,8 +127,7 @@ func joinPaths(absolutePath, relativePath string) string { } finalPath := path.Join(absolutePath, relativePath) - appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/' - if appendSlash { + if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' { return finalPath + "/" } return finalPath From b4c8bf1bbe1bf7109c663d34839357341b0d9392 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 17 May 2020 18:11:22 +0800 Subject: [PATCH 07/32] chore(performance): improve countParams (#2378) * update Signed-off-by: Bo-Yi Wu * chore: update * chore: improve countParams performance * update Signed-off-by: Bo-Yi Wu * chore: add comment --- tree.go | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/tree.go b/tree.go index f528fa3b..18ada811 100644 --- a/tree.go +++ b/tree.go @@ -5,10 +5,18 @@ package gin import ( + "bytes" "net/url" + "reflect" "strings" "unicode" "unicode/utf8" + "unsafe" +) + +var ( + strColon = []byte(":") + strStar = []byte("*") ) // Param is a single URL parameter, consisting of a key and a value. @@ -72,14 +80,35 @@ func longestCommonPrefix(a, b string) int { return i } +// bytesToStr converts byte slice to a string without memory allocation. +// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . +// +// Note it may break if string and/or slice header will change +// in the future go versions. +func bytesToStr(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} + +// strToBytes converts string to a byte slice without memory allocation. +// +// Note it may break if string and/or slice header will change +// in the future go versions. +func strToBytes(s string) (b []byte) { + /* #nosec G103 */ + bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + /* #nosec G103 */ + sh := *(*reflect.StringHeader)(unsafe.Pointer(&s)) + bh.Data = sh.Data + bh.Len = sh.Len + bh.Cap = sh.Len + return b +} + func countParams(path string) uint16 { var n uint - for i := range []byte(path) { - switch path[i] { - case ':', '*': - n++ - } - } + s := strToBytes(path) + n += uint(bytes.Count(s, strColon)) + n += uint(bytes.Count(s, strStar)) return uint16(n) } @@ -163,7 +192,7 @@ walk: n.children = []*node{&child} // []byte for proper unicode char conversion, see #65 - n.indices = string([]byte{n.path[i]}) + n.indices = bytesToStr([]byte{n.path[i]}) n.path = path[:i] n.handlers = nil n.wildChild = false @@ -223,7 +252,7 @@ walk: // Otherwise insert it if c != ':' && c != '*' { // []byte for proper unicode char conversion, see #65 - n.indices += string([]byte{c}) + n.indices += bytesToStr([]byte{c}) child := &node{ fullPath: fullPath, } From acbb3d4f42cb0c3b25e69a37c7080e681b56d74c Mon Sep 17 00:00:00 2001 From: Sudhir Mishra Date: Mon, 18 May 2020 13:20:35 +0530 Subject: [PATCH 08/32] Broken link repo typo fix (#2381) --- BENCHMARKS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BENCHMARKS.md b/BENCHMARKS.md index b164ae06..0f59b509 100644 --- a/BENCHMARKS.md +++ b/BENCHMARKS.md @@ -6,7 +6,7 @@ **Date:** May 04th, 2020 **Version:** Gin v1.6.3 **Go Version:** 1.14.2 linux/amd64 -**Source:** [Go HTTP Router Benchmark](https://github/gin-gonic/go-http-routing-benchmark) +**Source:** [Go HTTP Router Benchmark](https://github.com/gin-gonic/go-http-routing-benchmark) **Result:** [See the gist](https://gist.github.com/appleboy/b5f2ecfaf50824ae9c64dcfb9165ae5e) or [Travis result](https://travis-ci.org/github/gin-gonic/go-http-routing-benchmark/jobs/682947061) ## Static Routes: 157 From 7bffae1d3dbc2d8256bcf5bb4026a48fc69e4b10 Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Sat, 23 May 2020 22:19:37 +0800 Subject: [PATCH 09/32] Remove some functions that have the same effect as the bytes package (#2387) --- tree.go | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/tree.go b/tree.go index 18ada811..7a80af9e 100644 --- a/tree.go +++ b/tree.go @@ -7,11 +7,11 @@ package gin import ( "bytes" "net/url" - "reflect" "strings" "unicode" "unicode/utf8" - "unsafe" + + "github.com/gin-gonic/gin/internal/bytesconv" ) var ( @@ -80,36 +80,12 @@ func longestCommonPrefix(a, b string) int { return i } -// bytesToStr converts byte slice to a string without memory allocation. -// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . -// -// Note it may break if string and/or slice header will change -// in the future go versions. -func bytesToStr(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} - -// strToBytes converts string to a byte slice without memory allocation. -// -// Note it may break if string and/or slice header will change -// in the future go versions. -func strToBytes(s string) (b []byte) { - /* #nosec G103 */ - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - /* #nosec G103 */ - sh := *(*reflect.StringHeader)(unsafe.Pointer(&s)) - bh.Data = sh.Data - bh.Len = sh.Len - bh.Cap = sh.Len - return b -} - func countParams(path string) uint16 { - var n uint - s := strToBytes(path) - n += uint(bytes.Count(s, strColon)) - n += uint(bytes.Count(s, strStar)) - return uint16(n) + var n uint16 + s := bytesconv.StringToBytes(path) + n += uint16(bytes.Count(s, strColon)) + n += uint16(bytes.Count(s, strStar)) + return n } type nodeType uint8 @@ -192,7 +168,7 @@ walk: n.children = []*node{&child} // []byte for proper unicode char conversion, see #65 - n.indices = bytesToStr([]byte{n.path[i]}) + n.indices = bytesconv.BytesToString([]byte{n.path[i]}) n.path = path[:i] n.handlers = nil n.wildChild = false @@ -252,7 +228,7 @@ walk: // Otherwise insert it if c != ':' && c != '*' { // []byte for proper unicode char conversion, see #65 - n.indices += bytesToStr([]byte{c}) + n.indices += bytesconv.BytesToString([]byte{c}) child := &node{ fullPath: fullPath, } From 2773ce6e60866643388e6b5089b0a8ed64ea030a Mon Sep 17 00:00:00 2001 From: thinkerou Date: Sat, 23 May 2020 22:52:01 +0800 Subject: [PATCH 10/32] add copyright (#2388) Co-authored-by: Bo-Yi Wu --- internal/bytesconv/bytesconv.go | 4 ++++ internal/bytesconv/bytesconv_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/internal/bytesconv/bytesconv.go b/internal/bytesconv/bytesconv.go index 32c2b59e..7b80e335 100644 --- a/internal/bytesconv/bytesconv.go +++ b/internal/bytesconv/bytesconv.go @@ -1,3 +1,7 @@ +// Copyright 2020 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + package bytesconv import ( diff --git a/internal/bytesconv/bytesconv_test.go b/internal/bytesconv/bytesconv_test.go index ee2c8ab2..eeaad5ee 100644 --- a/internal/bytesconv/bytesconv_test.go +++ b/internal/bytesconv/bytesconv_test.go @@ -1,3 +1,7 @@ +// Copyright 2020 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + package bytesconv import ( From 922138144359752ff017df26c925bae6525962bc Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Sun, 24 May 2020 10:58:28 +0800 Subject: [PATCH 11/32] remove a unused type SecureJSONPrefix (#2391) --- render/json.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/render/json.go b/render/json.go index 015c0dbb..41863093 100644 --- a/render/json.go +++ b/render/json.go @@ -41,9 +41,6 @@ type AsciiJSON struct { Data interface{} } -// SecureJSONPrefix is a string which represents SecureJSON prefix. -type SecureJSONPrefix string - // PureJSON contains the given interface object. type PureJSON struct { Data interface{} From 5f261fa7529e62f574831ec7e869a5680ed23b52 Mon Sep 17 00:00:00 2001 From: Miles Date: Sun, 24 May 2020 11:37:32 +0800 Subject: [PATCH 12/32] Add a redirect sample for POST method (#2389) * Add a redirect sample for POST method Refer to issue https://github.com/gin-gonic/gin/issues/444 * put an empty line before 1396 Co-authored-by: Bo-Yi Wu --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 9cc23fc2..771b577f 100644 --- a/README.md +++ b/README.md @@ -1394,6 +1394,12 @@ r.GET("/test", func(c *gin.Context) { }) ``` +Issuing a HTTP redirect from POST. Refer to issue: [#444](https://github.com/gin-gonic/gin/issues/444) +```go +r.POST("/test", func(c *gin.Context) { + c.Redirect(http.StatusFound, "/foo") +}) +``` Issuing a Router redirect, use `HandleContext` like below. From c9b853581700d6834cc6e658d6966ac35b52f969 Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Mon, 25 May 2020 20:13:09 +0800 Subject: [PATCH 13/32] Rename some variables (#2393) --- errors.go | 20 ++++++++++---------- fs.go | 6 +++--- routergroup.go | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/errors.go b/errors.go index 25e8ff60..9a317992 100644 --- a/errors.go +++ b/errors.go @@ -55,7 +55,7 @@ func (msg *Error) SetMeta(data interface{}) *Error { // JSON creates a properly formatted JSON func (msg *Error) JSON() interface{} { - json := H{} + jsonData := H{} if msg.Meta != nil { value := reflect.ValueOf(msg.Meta) switch value.Kind() { @@ -63,16 +63,16 @@ func (msg *Error) JSON() interface{} { return msg.Meta case reflect.Map: for _, key := range value.MapKeys() { - json[key.String()] = value.MapIndex(key).Interface() + jsonData[key.String()] = value.MapIndex(key).Interface() } default: - json["meta"] = msg.Meta + jsonData["meta"] = msg.Meta } } - if _, ok := json["error"]; !ok { - json["error"] = msg.Error() + if _, ok := jsonData["error"]; !ok { + jsonData["error"] = msg.Error() } - return json + return jsonData } // MarshalJSON implements the json.Marshaller interface. @@ -135,17 +135,17 @@ func (a errorMsgs) Errors() []string { } func (a errorMsgs) JSON() interface{} { - switch len(a) { + switch length := len(a); length { case 0: return nil case 1: return a.Last().JSON() default: - json := make([]interface{}, len(a)) + jsonData := make([]interface{}, length) for i, err := range a { - json[i] = err.JSON() + jsonData[i] = err.JSON() } - return json + return jsonData } } diff --git a/fs.go b/fs.go index 7a6738a6..007d9b75 100644 --- a/fs.go +++ b/fs.go @@ -9,7 +9,7 @@ import ( "os" ) -type onlyfilesFS struct { +type onlyFilesFS struct { fs http.FileSystem } @@ -26,11 +26,11 @@ func Dir(root string, listDirectory bool) http.FileSystem { if listDirectory { return fs } - return &onlyfilesFS{fs} + return &onlyFilesFS{fs} } // Open conforms to http.Filesystem. -func (fs onlyfilesFS) Open(name string) (http.File, error) { +func (fs onlyFilesFS) Open(name string) (http.File, error) { f, err := fs.fs.Open(name) if err != nil { return nil, err diff --git a/routergroup.go b/routergroup.go index 9ff7c038..15d9930d 100644 --- a/routergroup.go +++ b/routergroup.go @@ -187,7 +187,7 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS fileServer := http.StripPrefix(absolutePath, http.FileServer(fs)) return func(c *Context) { - if _, nolisting := fs.(*onlyfilesFS); nolisting { + if _, noListing := fs.(*onlyFilesFS); noListing { c.Writer.WriteHeader(http.StatusNotFound) } From 5e40c1d49c21bf989e8d54dbd555086f06d4fb8a Mon Sep 17 00:00:00 2001 From: Vas N Date: Mon, 25 May 2020 14:47:06 +0100 Subject: [PATCH 14/32] DebugPrintRouteFunc() unit test (#2395) Co-authored-by: thinkerou --- debug_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/debug_test.go b/debug_test.go index d707b4bf..d8cd5d1a 100644 --- a/debug_test.go +++ b/debug_test.go @@ -7,6 +7,7 @@ package gin import ( "bytes" "errors" + "fmt" "html/template" "io" "log" @@ -64,6 +65,18 @@ func TestDebugPrintRoutes(t *testing.T) { assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re) } +func TestDebugPrintRouteFunc(t *testing.T) { + DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) { + fmt.Fprintf(DefaultWriter, "[GIN-debug] %-6s %-40s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) + } + re := captureOutput(t, func() { + SetMode(DebugMode) + debugPrintRoute("GET", "/path/to/route/:param1/:param2", HandlersChain{func(c *Context) {}, handlerNameTest}) + SetMode(TestMode) + }) + assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param1/:param2 --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re) +} + func TestDebugPrintLoadTemplate(t *testing.T) { re := captureOutput(t, func() { SetMode(DebugMode) From 4cabdd303fe38b6b53e83a6aa04d0468a71c0139 Mon Sep 17 00:00:00 2001 From: Johnny Dallas Date: Wed, 8 Jul 2020 18:40:00 -0700 Subject: [PATCH 15/32] Add CustomRecovery builtin middleware (#2322) * Add CustomRecovery and CustomRecoveryWithWriter methods * add CustomRecovery example to README * add test for CustomRecovery * support RecoveryWithWriter(io.Writer, ...RecoveryFunc) --- README.md | 33 +++++++++++++++ recovery.go | 27 ++++++++++-- recovery_test.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 771b577f..7ff59997 100644 --- a/README.md +++ b/README.md @@ -496,6 +496,39 @@ func main() { } ``` +### Custom Recovery behavior +```go +func main() { + // Creates a router without any middleware by default + r := gin.New() + + // Global middleware + // Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release. + // By default gin.DefaultWriter = os.Stdout + r.Use(gin.Logger()) + + // Recovery middleware recovers from any panics and writes a 500 if there was one. + r.Use(gin.CustomRecovery(func(c *gin.Context, recovered interface{}) { + if err, ok := recovered.(string); ok { + c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err)) + } + c.AbortWithStatus(http.StatusInternalServerError) + })) + + r.GET("/panic", func(c *gin.Context) { + // panic with a string -- the custom middleware could save this to a database or report it to the user + panic("foo") + }) + + r.GET("/", func(c *gin.Context) { + c.String(http.StatusOK, "ohai") + }) + + // Listen and serve on 0.0.0.0:8080 + r.Run(":8080") +} +``` + ### How to write log file ```go func main() { diff --git a/recovery.go b/recovery.go index 8cf0932a..d02b829b 100644 --- a/recovery.go +++ b/recovery.go @@ -26,13 +26,29 @@ var ( slash = []byte("/") ) +// RecoveryFunc defines the function passable to CustomRecovery. +type RecoveryFunc func(c *Context, err interface{}) + // Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. func Recovery() HandlerFunc { return RecoveryWithWriter(DefaultErrorWriter) } +//CustomRecovery returns a middleware that recovers from any panics and calls the provided handle func to handle it. +func CustomRecovery(handle RecoveryFunc) HandlerFunc { + return RecoveryWithWriter(DefaultErrorWriter, handle) +} + // RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one. -func RecoveryWithWriter(out io.Writer) HandlerFunc { +func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc { + if len(recovery) > 0 { + return CustomRecoveryWithWriter(out, recovery[0]) + } + return CustomRecoveryWithWriter(out, defaultHandleRecovery) +} + +// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it. +func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc { var logger *log.Logger if out != nil { logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags) @@ -70,13 +86,12 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc { timeFormat(time.Now()), err, stack, reset) } } - - // If the connection is dead, we can't write a status to it. if brokenPipe { + // If the connection is dead, we can't write a status to it. c.Error(err.(error)) // nolint: errcheck c.Abort() } else { - c.AbortWithStatus(http.StatusInternalServerError) + handle(c, err) } } }() @@ -84,6 +99,10 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc { } } +func defaultHandleRecovery(c *Context, err interface{}) { + c.AbortWithStatus(http.StatusInternalServerError) +} + // stack returns a nicely formatted stack frame, skipping skip frames. func stack(skip int) []byte { buf := new(bytes.Buffer) // the returned data diff --git a/recovery_test.go b/recovery_test.go index 21a0a480..6cc2a47a 100644 --- a/recovery_test.go +++ b/recovery_test.go @@ -62,7 +62,7 @@ func TestPanicInHandler(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Contains(t, buffer.String(), "panic recovered") assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem") - assert.Contains(t, buffer.String(), "TestPanicInHandler") + assert.Contains(t, buffer.String(), t.Name()) assert.NotContains(t, buffer.String(), "GET /recovery") // Debug mode prints the request @@ -144,3 +144,107 @@ func TestPanicWithBrokenPipe(t *testing.T) { }) } } + +func TestCustomRecoveryWithWriter(t *testing.T) { + errBuffer := new(bytes.Buffer) + buffer := new(bytes.Buffer) + router := New() + handleRecovery := func(c *Context, err interface{}) { + errBuffer.WriteString(err.(string)) + c.AbortWithStatus(http.StatusBadRequest) + } + router.Use(CustomRecoveryWithWriter(buffer, handleRecovery)) + router.GET("/recovery", func(_ *Context) { + panic("Oupps, Houston, we have a problem") + }) + // RUN + w := performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "panic recovered") + assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem") + assert.Contains(t, buffer.String(), t.Name()) + assert.NotContains(t, buffer.String(), "GET /recovery") + + // Debug mode prints the request + SetMode(DebugMode) + // RUN + w = performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "GET /recovery") + + assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String()) + + SetMode(TestMode) +} + +func TestCustomRecovery(t *testing.T) { + errBuffer := new(bytes.Buffer) + buffer := new(bytes.Buffer) + router := New() + DefaultErrorWriter = buffer + handleRecovery := func(c *Context, err interface{}) { + errBuffer.WriteString(err.(string)) + c.AbortWithStatus(http.StatusBadRequest) + } + router.Use(CustomRecovery(handleRecovery)) + router.GET("/recovery", func(_ *Context) { + panic("Oupps, Houston, we have a problem") + }) + // RUN + w := performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "panic recovered") + assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem") + assert.Contains(t, buffer.String(), t.Name()) + assert.NotContains(t, buffer.String(), "GET /recovery") + + // Debug mode prints the request + SetMode(DebugMode) + // RUN + w = performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "GET /recovery") + + assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String()) + + SetMode(TestMode) +} + +func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) { + errBuffer := new(bytes.Buffer) + buffer := new(bytes.Buffer) + router := New() + DefaultErrorWriter = buffer + handleRecovery := func(c *Context, err interface{}) { + errBuffer.WriteString(err.(string)) + c.AbortWithStatus(http.StatusBadRequest) + } + router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery)) + router.GET("/recovery", func(_ *Context) { + panic("Oupps, Houston, we have a problem") + }) + // RUN + w := performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "panic recovered") + assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem") + assert.Contains(t, buffer.String(), t.Name()) + assert.NotContains(t, buffer.String(), "GET /recovery") + + // Debug mode prints the request + SetMode(DebugMode) + // RUN + w = performRequest(router, "GET", "/recovery") + // TEST + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, buffer.String(), "GET /recovery") + + assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String()) + + SetMode(TestMode) +} From 44e78a78d434aa3462e0578f1483873214a7fe81 Mon Sep 17 00:00:00 2001 From: Hiroyuki Tanaka Date: Sat, 1 Aug 2020 16:03:33 +0900 Subject: [PATCH 16/32] README: Change badge to pkg.go.dev (#2449) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ff59997..5ce9d8ef 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin) [![codecov](https://codecov.io/gh/gin-gonic/gin/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-gonic/gin) [![Go Report Card](https://goreportcard.com/badge/github.com/gin-gonic/gin)](https://goreportcard.com/report/github.com/gin-gonic/gin) -[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc) +[![GoDoc](https://pkg.go.dev/badge/github.com/gin-gonic/gin?status.svg)](https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc) [![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Sourcegraph](https://sourcegraph.com/github.com/gin-gonic/gin/-/badge.svg)](https://sourcegraph.com/github.com/gin-gonic/gin?badge) [![Open Source Helpers](https://www.codetriage.com/gin-gonic/gin/badges/users.svg)](https://www.codetriage.com/gin-gonic/gin) From c6d6df6d5ada990c902c51a54b9c4c6f21f87840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A3=AE=20=E5=84=AA=E5=A4=AA?= <59682979+uta-mori@users.noreply.github.com> Date: Sat, 1 Aug 2020 16:26:29 +0900 Subject: [PATCH 17/32] Docs: Update README.md Custom Validator sample code (#2448) Co-authored-by: Bo-Yi Wu --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5ce9d8ef..fc1d01cf 100644 --- a/README.md +++ b/README.md @@ -758,12 +758,12 @@ import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" - "gopkg.in/go-playground/validator.v10" + "github.com/go-playground/validator/v10" ) // Booking contains binded and validated data. type Booking struct { - CheckIn time.Time `form:"check_in" binding:"required" time_format:"2006-01-02"` + CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"` CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"` } @@ -800,11 +800,14 @@ func getBookable(c *gin.Context) { ``` ```console -$ curl "localhost:8085/bookable?check_in=2018-04-16&check_out=2018-04-17" +$ curl "localhost:8085/bookable?check_in=2030-04-16&check_out=2030-04-17" {"message":"Booking dates are valid!"} -$ curl "localhost:8085/bookable?check_in=2018-03-10&check_out=2018-03-09" +$ curl "localhost:8085/bookable?check_in=2030-03-10&check_out=2030-03-09" {"error":"Key: 'Booking.CheckOut' Error:Field validation for 'CheckOut' failed on the 'gtfield' tag"} + +$ curl "localhost:8085/bookable?check_in=2000-03-09&check_out=2000-03-10" +{"error":"Key: 'Booking.CheckIn' Error:Field validation for 'CheckIn' failed on the 'bookabledate' tag"}% ``` [Struct level validations](https://github.com/go-playground/validator/releases/tag/v8.7) can also be registered this way. From cf8b583db420257f5ed2c751073e18028ba47551 Mon Sep 17 00:00:00 2001 From: John Bampton Date: Tue, 4 Aug 2020 09:04:06 +1000 Subject: [PATCH 18/32] Fix spelling (#2451) --- CHANGELOG.md | 6 +++--- binding/form_mapping_test.go | 2 +- context_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 592c2abc..3ac51ad3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,14 +8,14 @@ ## Gin v1.6.2 -### BUFIXES +### BUGFIXES * fix missing initial sync.RWMutex [#2305](https://github.com/gin-gonic/gin/pull/2305) ### ENHANCEMENTS * Add set samesite in cookie. [#2306](https://github.com/gin-gonic/gin/pull/2306) ## Gin v1.6.1 -### BUFIXES +### BUGFIXES * Revert "fix accept incoming network connections" [#2294](https://github.com/gin-gonic/gin/pull/2294) ## Gin v1.6.0 @@ -25,7 +25,7 @@ * drop support govendor [#2148](https://github.com/gin-gonic/gin/pull/2148) * Added support for SameSite cookie flag [#1615](https://github.com/gin-gonic/gin/pull/1615) ### FEATURES - * add yaml negotitation [#2220](https://github.com/gin-gonic/gin/pull/2220) + * add yaml negotiation [#2220](https://github.com/gin-gonic/gin/pull/2220) * FileFromFS [#2112](https://github.com/gin-gonic/gin/pull/2112) ### BUGFIXES * Unix Socket Handling [#2280](https://github.com/gin-gonic/gin/pull/2280) diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index 2a560371..2675d46b 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -190,7 +190,7 @@ func TestMappingTime(t *testing.T) { assert.Error(t, err) } -func TestMapiingTimeDuration(t *testing.T) { +func TestMappingTimeDuration(t *testing.T) { var s struct { D time.Duration } diff --git a/context_test.go b/context_test.go index ce077bc6..b53bf92b 100644 --- a/context_test.go +++ b/context_test.go @@ -940,7 +940,7 @@ func TestContextRenderNoContentHTMLString(t *testing.T) { assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) } -// TestContextData tests that the response can be written from `bytesting` +// TestContextData tests that the response can be written from `bytestring` // with specified MIME type func TestContextRenderData(t *testing.T) { w := httptest.NewRecorder() From 30b5f7e2d7b32a498ba12f3fe1ead105e63f7f7c Mon Sep 17 00:00:00 2001 From: lantw44 Date: Sat, 8 Aug 2020 17:31:08 +0800 Subject: [PATCH 19/32] binding: avoid 2038 problem on 32-bit architectures (#2450) Function setTimeField calls strconv.ParseInt with bit size 0 when parsing Unix time, which means it is equivalent to specifying 32 on 32-bit architectures. This causes the function to suffer from the year 2038 problem. To fix it and keep the behavior the same on both 32-bit and 64-bit architectures, explicitly specify bit size 64. Co-authored-by: thinkerou --- binding/form_mapping.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index b81ad195..f0913ea5 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -270,7 +270,7 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val switch tf := strings.ToLower(timeFormat); tf { case "unix", "unixnano": - tv, err := strconv.ParseInt(val, 10, 0) + tv, err := strconv.ParseInt(val, 10, 64) if err != nil { return err } From 815e1ce281f873c62cdbe05d60b5b2ef9d20ed6b Mon Sep 17 00:00:00 2001 From: Florian Polster Date: Sat, 8 Aug 2020 14:32:19 +0200 Subject: [PATCH 20/32] Prevent panic in Context.GetQuery() when there is no Request (#2412) Co-authored-by: thinkerou --- context.go | 6 +++++- context_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index a9458833..95b1807d 100644 --- a/context.go +++ b/context.go @@ -416,7 +416,11 @@ func (c *Context) QueryArray(key string) []string { func (c *Context) initQueryCache() { if c.queryCache == nil { - c.queryCache = c.Request.URL.Query() + if c.Request != nil { + c.queryCache = c.Request.URL.Query() + } else { + c.queryCache = url.Values{} + } } } diff --git a/context_test.go b/context_test.go index b53bf92b..1a5a3c5f 100644 --- a/context_test.go +++ b/context_test.go @@ -410,6 +410,21 @@ func TestContextQuery(t *testing.T) { assert.Empty(t, c.PostForm("foo")) } +func TestContextDefaultQueryOnEmptyRequest(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil + assert.NotPanics(t, func() { + value, ok := c.GetQuery("NoKey") + assert.False(t, ok) + assert.Empty(t, value) + }) + assert.NotPanics(t, func() { + assert.Equal(t, "nada", c.DefaultQuery("NoKey", "nada")) + }) + assert.NotPanics(t, func() { + assert.Empty(t, c.Query("NoKey")) + }) +} + func TestContextQueryAndPostForm(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second") From b94d23d1b48d7b5078035ddbab0c3b5df138b827 Mon Sep 17 00:00:00 2001 From: AllinGo Date: Wed, 12 Aug 2020 09:28:51 +0800 Subject: [PATCH 21/32] support go 1.15 (#2463) --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 6680a5b3..d7086b38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,10 @@ matrix: - go: 1.14.x env: - TESTTAGS=nomsgpack + - go: 1.15.x + - go: 1.15.x + env: + - TESTTAGS=nomsgpack - go: master git: From 0304ee96edb2f7f863d5e8337073cade0327af4a Mon Sep 17 00:00:00 2001 From: kaiiak Date: Tue, 1 Sep 2020 09:33:54 +0800 Subject: [PATCH 22/32] Add GetUint and GetUint64 method on gin.context (#2487) --- context.go | 16 ++++++++++++++++ context_test.go | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/context.go b/context.go index 95b1807d..216cecae 100644 --- a/context.go +++ b/context.go @@ -295,6 +295,22 @@ func (c *Context) GetInt64(key string) (i64 int64) { return } +// GetUint returns the value associated with the key as an unsigned integer. +func (c *Context) GetUint(key string) (ui uint) { + if val, ok := c.Get(key); ok && val != nil { + ui, _ = val.(uint) + } + return +} + +// GetUint64 returns the value associated with the key as an unsigned integer. +func (c *Context) GetUint64(key string) (ui64 uint64) { + if val, ok := c.Get(key); ok && val != nil { + ui64, _ = val.(uint64) + } + return +} + // GetFloat64 returns the value associated with the key as a float64. func (c *Context) GetFloat64(key string) (f64 float64) { if val, ok := c.Get(key); ok && val != nil { diff --git a/context_test.go b/context_test.go index 1a5a3c5f..e2f8de06 100644 --- a/context_test.go +++ b/context_test.go @@ -261,6 +261,18 @@ func TestContextGetInt64(t *testing.T) { assert.Equal(t, int64(42424242424242), c.GetInt64("int64")) } +func TestContextGetUint(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Set("uint", uint(1)) + assert.Equal(t, uint(1), c.GetUint("uint")) +} + +func TestContextGetUint64(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Set("uint64", uint64(18446744073709551615)) + assert.Equal(t, uint64(18446744073709551615), c.GetUint64("uint64")) +} + func TestContextGetFloat64(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Set("float64", 4.2) From b860d8672daea919f988ed83df94f28ac4c9cc5f Mon Sep 17 00:00:00 2001 From: Dennis Cho <47404603+forest747@users.noreply.github.com> Date: Thu, 3 Sep 2020 00:15:25 +0900 Subject: [PATCH 23/32] Fix typo (#2489) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc1d01cf..a40d6373 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ func main() { ``` ``` -ids: map[b:hello a:1234], names: map[second:tianou first:thinkerou] +ids: map[b:hello a:1234]; names: map[second:tianou first:thinkerou] ``` ### Upload files From 1f232c7f47fb0675bcf0b334c8c4ab252c0dba86 Mon Sep 17 00:00:00 2001 From: yugu Date: Wed, 9 Sep 2020 20:00:44 +0800 Subject: [PATCH 24/32] docs:close the body of the response (#2494) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a40d6373..0515e99d 100644 --- a/README.md +++ b/README.md @@ -1255,6 +1255,7 @@ func main() { } reader := response.Body + defer reader.Close() contentLength := response.ContentLength contentType := response.Header.Get("Content-Type") From 3100b7cb05a8072b76d31686d8a7b4f9b12df4be Mon Sep 17 00:00:00 2001 From: John Bampton Date: Mon, 14 Sep 2020 12:40:20 +1000 Subject: [PATCH 25/32] Fix spelling (#2498) --- context.go | 2 +- context_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 216cecae..5b60d8b0 100644 --- a/context.go +++ b/context.go @@ -973,7 +973,7 @@ func (c *Context) File(filepath string) { http.ServeFile(c.Writer, c.Request, filepath) } -// FileFromFS writes the specified file from http.FileSytem into the body stream in an efficient way. +// FileFromFS writes the specified file from http.FileSystem into the body stream in an efficient way. func (c *Context) FileFromFS(filepath string, fs http.FileSystem) { defer func(old string) { c.Request.URL.Path = old diff --git a/context_test.go b/context_test.go index e2f8de06..8e1e3b57 100644 --- a/context_test.go +++ b/context_test.go @@ -1282,7 +1282,7 @@ func TestContextIsAborted(t *testing.T) { assert.True(t, c.IsAborted()) } -// TestContextData tests that the response can be written from `bytesting` +// TestContextData tests that the response can be written from `bytestring` // with specified MIME type func TestContextAbortWithStatus(t *testing.T) { w := httptest.NewRecorder() From 540b1eff7069128df5d95e09968569f2266d9a6a Mon Sep 17 00:00:00 2001 From: eudore <30709860+eudore@users.noreply.github.com> Date: Fri, 25 Sep 2020 09:45:17 +0800 Subject: [PATCH 26/32] update content-disposition header to MIME-style (#2512) --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context.go b/context.go index 5b60d8b0..71fb5937 100644 --- a/context.go +++ b/context.go @@ -987,7 +987,7 @@ func (c *Context) FileFromFS(filepath string, fs http.FileSystem) { // FileAttachment writes the specified file into the body stream in an efficient way // On the client side, the file will typically be downloaded with the given filename func (c *Context) FileAttachment(filepath, filename string) { - c.Writer.Header().Set("content-disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename)) + c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename)) http.ServeFile(c.Writer, c.Request, filepath) } From 129796610085cd37bb1f48973363e0cb381664b6 Mon Sep 17 00:00:00 2001 From: xyb Date: Thu, 15 Oct 2020 13:55:57 +0800 Subject: [PATCH 27/32] use IndexByte replace Split to improve performance (#2500) Co-authored-by: yonbiaoxiao Co-authored-by: Bo-Yi Wu --- utils.go | 5 ++++- utils_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/utils.go b/utils.go index fab3aee3..c32f0eeb 100644 --- a/utils.go +++ b/utils.go @@ -103,7 +103,10 @@ func parseAccept(acceptHeader string) []string { parts := strings.Split(acceptHeader, ",") out := make([]string, 0, len(parts)) for _, part := range parts { - if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" { + if i := strings.IndexByte(part, ';'); i > 0 { + part = part[:i] + } + if part = strings.TrimSpace(part); part != "" { out = append(out, part) } } diff --git a/utils_test.go b/utils_test.go index 9b57c57b..cc486c35 100644 --- a/utils_test.go +++ b/utils_test.go @@ -18,6 +18,12 @@ func init() { SetMode(TestMode) } +func BenchmarkParseAccept(b *testing.B) { + for i := 0; i < b.N; i++ { + parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8") + } +} + type testStruct struct { T *testing.T } From a7a6986d73f69f8f51d69db9a95aaddf6382e9ea Mon Sep 17 00:00:00 2001 From: Zach Newburgh Date: Thu, 15 Oct 2020 10:41:35 -0400 Subject: [PATCH 28/32] fix: print headers without Authorization header on broken pipe (#2528) Co-authored-by: thinkerou --- recovery.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/recovery.go b/recovery.go index d02b829b..563f5aaa 100644 --- a/recovery.go +++ b/recovery.go @@ -76,11 +76,12 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc { headers[idx] = current[0] + ": *" } } + headersToStr := strings.Join(headers, "\r\n") if brokenPipe { - logger.Printf("%s\n%s%s", err, string(httpRequest), reset) + logger.Printf("%s\n%s%s", err, headersToStr, reset) } else if IsDebugging() { logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", - timeFormat(time.Now()), strings.Join(headers, "\r\n"), err, stack, reset) + timeFormat(time.Now()), headersToStr, err, stack, reset) } else { logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", timeFormat(time.Now()), err, stack, reset) From d541085b59975edc285e98b6c19cea6bfed7d0a9 Mon Sep 17 00:00:00 2001 From: Zasda Yusuf Mikail Date: Fri, 16 Oct 2020 08:32:10 +0700 Subject: [PATCH 29/32] Add some missing dots on README (#2519) Signed-off-by: Zasda Yusuf Mikail Co-authored-by: Bo-Yi Wu --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0515e99d..7e8359ee 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,8 @@ Gin uses a custom version of [HttpRouter](https://github.com/julienschmidt/httpr - [x] Zero allocation router. - [x] Still the fastest http router and framework. From routing to writing. -- [x] Complete suite of unit tests -- [x] Battle tested +- [x] Complete suite of unit tests. +- [x] Battle tested. - [x] API frozen, new releases will not break your code. ## Build with [jsoniter](https://github.com/json-iterator/go) From c83a1cca0a804261cfce5222b2bb35e3d9a46e48 Mon Sep 17 00:00:00 2001 From: xyb Date: Fri, 16 Oct 2020 18:32:33 +0800 Subject: [PATCH 30/32] reduce allocs and improve the render `WriteString` (#2508) Co-authored-by: yonbiaoxiao Co-authored-by: Bo-Yi Wu Co-authored-by: thinkerou --- render/text.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/render/text.go b/render/text.go index 30f5f532..461b720a 100644 --- a/render/text.go +++ b/render/text.go @@ -7,6 +7,8 @@ package render import ( "fmt" "net/http" + + "github.com/gin-gonic/gin/internal/bytesconv" ) // String contains the given interface object slice and its format. @@ -34,6 +36,6 @@ func WriteString(w http.ResponseWriter, format string, data []interface{}) (err _, err = fmt.Fprintf(w, format, data...) return } - _, err = w.Write([]byte(format)) + _, err = w.Write(bytesconv.StringToBytes(format)) return } From f969bfaf50d095713e17a10edd0e3534ba2fa9f6 Mon Sep 17 00:00:00 2001 From: Georges Varouchas Date: Sat, 17 Oct 2020 15:22:37 +0200 Subject: [PATCH 31/32] implement ".Unwrap() error" on Error type (#2525) (#2526) Co-authored-by: Bo-Yi Wu Co-authored-by: thinkerou --- errors.go | 5 +++++ errors_1.13_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 errors_1.13_test.go diff --git a/errors.go b/errors.go index 9a317992..0f276c13 100644 --- a/errors.go +++ b/errors.go @@ -90,6 +90,11 @@ func (msg *Error) IsType(flags ErrorType) bool { return (msg.Type & flags) > 0 } +// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap() +func (msg *Error) Unwrap() error { + return msg.Err +} + // ByType returns a readonly copy filtered the byte. // ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic. func (a errorMsgs) ByType(typ ErrorType) errorMsgs { diff --git a/errors_1.13_test.go b/errors_1.13_test.go new file mode 100644 index 00000000..a8f9a94e --- /dev/null +++ b/errors_1.13_test.go @@ -0,0 +1,33 @@ +// +build go1.13 + +package gin + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +type TestErr string + +func (e TestErr) Error() string { return string(e) } + +// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()". +// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13, +// hence the "// +build go1.13" directive at the beginning of this file. +func TestErrorUnwrap(t *testing.T) { + innerErr := TestErr("somme error") + + // 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr + err := fmt.Errorf("wrapped: %w", &Error{ + Err: innerErr, + Type: ErrorTypeAny, + }) + + // check that 'errors.Is()' and 'errors.As()' behave as expected : + assert.True(t, errors.Is(err, innerErr)) + var testErr TestErr + assert.True(t, errors.As(err, &testErr)) +} From 7e444c6f59340cb7b8d940e91a8dfcc7ce1e47c2 Mon Sep 17 00:00:00 2001 From: Peperoncino <2wua4nlyi@gmail.com> Date: Wed, 21 Oct 2020 10:36:01 +0900 Subject: [PATCH 32/32] upgrade go-validator to v10.4.1 (#2536) --- go.mod | 2 +- go.sum | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index cfaee746..884ff851 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/gin-contrib/sse v0.1.0 - github.com/go-playground/validator/v10 v10.2.0 + github.com/go-playground/validator/v10 v10.4.1 github.com/golang/protobuf v1.3.3 github.com/json-iterator/go v1.1.9 github.com/mattn/go-isatty v0.0.12 diff --git a/go.sum b/go.sum index 4c14fb83..a64b3319 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,8 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= -github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -34,8 +34,15 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=