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. 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 ( 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{} diff --git a/tree.go b/tree.go index a43392df..74e07e84 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 @@ -191,7 +167,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 @@ -251,7 +227,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, }