mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-11 00:56:57 +08:00
Compare commits
3 Commits
7ad0248f2d
...
bf8bffdcd4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf8bffdcd4 | ||
|
|
915e4c90d2 | ||
|
|
38c24307a9 |
@ -1910,7 +1910,7 @@ func TestContextClientIP(t *testing.T) {
|
||||
resetContextForClientIPTests(c)
|
||||
|
||||
// IPv6 support
|
||||
c.Request.RemoteAddr = "[::1]:12345"
|
||||
c.Request.RemoteAddr = fmt.Sprintf("[%s]:12345", localhostIPv6)
|
||||
assert.Equal(t, "20.20.20.20", c.ClientIP())
|
||||
|
||||
resetContextForClientIPTests(c)
|
||||
@ -3212,7 +3212,7 @@ func TestContextCopyShouldNotCancel(t *testing.T) {
|
||||
}()
|
||||
|
||||
addr := strings.Split(l.Addr().String(), ":")
|
||||
res, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/", addr[len(addr)-1]))
|
||||
res, err := http.Get(fmt.Sprintf("http://%s:%s/", localhostIP, addr[len(addr)-1]))
|
||||
if err != nil {
|
||||
t.Error(fmt.Errorf("request error: %w", err))
|
||||
return
|
||||
|
||||
@ -83,7 +83,7 @@ func TestLoadHTMLGlobDebugMode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestH2c(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
ln, err := net.Listen("tcp", localhostIP+":0")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
27
tree.go
27
tree.go
@ -58,6 +58,8 @@ func (trees methodTrees) get(method string) *node {
|
||||
return nil
|
||||
}
|
||||
|
||||
// longestCommonPrefix returns the length in bytes of the longest common prefix
|
||||
// of the two input strings `a` and `b`.
|
||||
func longestCommonPrefix(a, b string) int {
|
||||
i := 0
|
||||
max_ := min(len(a), len(b))
|
||||
@ -410,6 +412,19 @@ type skippedNode struct {
|
||||
paramsCount int16
|
||||
}
|
||||
|
||||
// ensureParamsCapacity ensures that the params slice has capacity for at least needed elements.
|
||||
// It preserves existing length and content.
|
||||
func ensureParamsCapacity(params *Params, needed int) {
|
||||
if params == nil {
|
||||
return
|
||||
}
|
||||
if cap(*params) < needed {
|
||||
newParams := make(Params, len(*params), needed)
|
||||
copy(newParams, *params)
|
||||
*params = newParams
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -497,11 +512,7 @@ walk: // Outer loop for walking the tree
|
||||
// Save param value
|
||||
if params != nil {
|
||||
// Preallocate capacity if necessary
|
||||
if cap(*params) < int(globalParamsCount) {
|
||||
newParams := make(Params, len(*params), globalParamsCount)
|
||||
copy(newParams, *params)
|
||||
*params = newParams
|
||||
}
|
||||
ensureParamsCapacity(params, int(globalParamsCount))
|
||||
|
||||
if value.params == nil {
|
||||
value.params = params
|
||||
@ -550,11 +561,7 @@ walk: // Outer loop for walking the tree
|
||||
// Save param value
|
||||
if params != nil {
|
||||
// Preallocate capacity if necessary
|
||||
if cap(*params) < int(globalParamsCount) {
|
||||
newParams := make(Params, len(*params), globalParamsCount)
|
||||
copy(newParams, *params)
|
||||
*params = newParams
|
||||
}
|
||||
ensureParamsCapacity(params, int(globalParamsCount))
|
||||
|
||||
if value.params == nil {
|
||||
value.params = params
|
||||
|
||||
6
utils.go
6
utils.go
@ -19,6 +19,12 @@ import (
|
||||
// BindKey indicates a default bind key.
|
||||
const BindKey = "_gin-gonic/gin/bindkey"
|
||||
|
||||
// localhostIP indicates the default localhost IP address.
|
||||
const localhostIP = "127.0.0.1"
|
||||
|
||||
// localhostIPv6 indicates the default localhost IPv6 address.
|
||||
const localhostIPv6 = "::1"
|
||||
|
||||
// Bind is a helper function for given interface object and returns a Gin middleware.
|
||||
func Bind(val any) HandlerFunc {
|
||||
value := reflect.ValueOf(val)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user