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 1/4] 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 2/4] 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 3/4] 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 4/4] 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.