mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-11 09:06:56 +08:00
Compare commits
4 Commits
cedcbd52f4
...
a234b47831
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a234b47831 | ||
|
|
915e4c90d2 | ||
|
|
6e891029bf | ||
|
|
14247a51e5 |
19
context.go
19
context.go
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
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