Compare commits

...

4 Commits

Author SHA1 Message Date
Javier Fabrizio
a234b47831
Merge 6e891029bf1128e0845ce64a111bd35f0682dcce into 915e4c90d28ec4cffc6eb146e208ab5a65eac772 2026-01-01 15:55:16 +00:00
Paulo Henrique
915e4c90d2
refactor(context): replace hardcoded localhost IPs with constants (#4481) 2025-12-27 19:25:17 +08:00
Javier Fabrizio
6e891029bf add comments to functions 2023-07-11 15:56:45 -03:00
Javier Fabrizio
14247a51e5 fix Request.Context() checks 2023-07-10 16:56:00 -03:00
4 changed files with 45 additions and 18 deletions

View File

@ -1394,16 +1394,19 @@ func (c *Context) SetAccepted(formats ...string) {
/***** GOLANG.ORG/X/NET/CONTEXT *****/
/************************************/
// hasRequestContext returns whether c.Request has Context and fallback.
// hasFallback returns whether c.engine has ContextWithFallback.
func (c *Context) hasFallback() bool {
return c.engine != nil && c.engine.ContextWithFallback
}
// hasRequestContext returns whether c.Request has Context
func (c *Context) hasRequestContext() bool {
hasFallback := c.engine != nil && c.engine.ContextWithFallback
hasRequestContext := c.Request != nil && c.Request.Context() != nil
return hasFallback && hasRequestContext
return c.Request != nil && c.Request.Context() != nil
}
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
func (c *Context) Deadline() (deadline time.Time, ok bool) {
if !c.hasRequestContext() {
if !c.hasFallback() || !c.hasRequestContext() {
return
}
return c.Request.Context().Deadline()
@ -1411,7 +1414,7 @@ func (c *Context) Deadline() (deadline time.Time, ok bool) {
// Done returns nil (chan which will wait forever) when c.Request has no Context.
func (c *Context) Done() <-chan struct{} {
if !c.hasRequestContext() {
if !c.hasFallback() || !c.hasRequestContext() {
return nil
}
return c.Request.Context().Done()
@ -1419,7 +1422,7 @@ func (c *Context) Done() <-chan struct{} {
// Err returns nil when c.Request has no Context.
func (c *Context) Err() error {
if !c.hasRequestContext() {
if !c.hasFallback() || !c.hasRequestContext() {
return nil
}
return c.Request.Context().Err()
@ -1440,7 +1443,7 @@ func (c *Context) Value(key any) any {
return val
}
}
if !c.hasRequestContext() {
if !c.hasFallback() || !c.hasRequestContext() {
return nil
}
return c.Request.Context().Value(key)

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)
@ -3023,20 +3023,38 @@ func TestRemoteIPFail(t *testing.T) {
func TestHasRequestContext(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.False(t, c.hasRequestContext(), "no request, no fallback")
assert.False(t, c.hasRequestContext())
c.engine.ContextWithFallback = true
assert.False(t, c.hasRequestContext(), "no request, has fallback")
assert.False(t, c.hasRequestContext())
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
assert.True(t, c.hasRequestContext(), "has request, has fallback")
assert.True(t, c.hasRequestContext())
c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
assert.False(t, c.hasRequestContext(), "has request with nil ctx, has fallback")
assert.False(t, c.hasRequestContext())
c.engine.ContextWithFallback = false
assert.False(t, c.hasRequestContext(), "has request, no fallback")
assert.False(t, c.hasRequestContext())
c = &Context{}
assert.False(t, c.hasRequestContext(), "no request, no engine")
assert.False(t, c.hasRequestContext())
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
assert.False(t, c.hasRequestContext(), "has request, no engine")
assert.True(t, c.hasRequestContext())
}
func TestHasFallback(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.False(t, c.hasFallback())
c.engine.ContextWithFallback = true
assert.True(t, c.hasFallback())
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
assert.True(t, c.hasFallback())
c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
assert.True(t, c.hasFallback())
c.engine.ContextWithFallback = false
assert.False(t, c.hasFallback())
c = &Context{}
assert.False(t, c.hasFallback())
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
assert.False(t, c.hasFallback())
}
func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
@ -3212,7 +3230,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)
}

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)