Compare commits

...

3 Commits

4 changed files with 26 additions and 13 deletions

View File

@ -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

View File

@ -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
View File

@ -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

View File

@ -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)