fix Request.Context() checks (#3512)
Co-authored-by: Bence Vidosits <bence.vidosits1@ibm.com>
This commit is contained in:
		
							
								
								
									
										15
									
								
								context.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								context.go
									
									
									
									
									
								
							@ -1180,9 +1180,16 @@ func (c *Context) SetAccepted(formats ...string) {
 | 
				
			|||||||
/***** GOLANG.ORG/X/NET/CONTEXT *****/
 | 
					/***** GOLANG.ORG/X/NET/CONTEXT *****/
 | 
				
			||||||
/************************************/
 | 
					/************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// hasRequestContext returns whether c.Request has Context and fallback.
 | 
				
			||||||
 | 
					func (c *Context) hasRequestContext() bool {
 | 
				
			||||||
 | 
						hasFallback := c.engine != nil && c.engine.ContextWithFallback
 | 
				
			||||||
 | 
						hasRequestContext := c.Request != nil && c.Request.Context() != nil
 | 
				
			||||||
 | 
						return hasFallback && hasRequestContext
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
 | 
					// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
 | 
				
			||||||
func (c *Context) Deadline() (deadline time.Time, ok bool) {
 | 
					func (c *Context) Deadline() (deadline time.Time, ok bool) {
 | 
				
			||||||
	if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
 | 
						if !c.hasRequestContext() {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return c.Request.Context().Deadline()
 | 
						return c.Request.Context().Deadline()
 | 
				
			||||||
@ -1190,7 +1197,7 @@ func (c *Context) Deadline() (deadline time.Time, ok bool) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Done returns nil (chan which will wait forever) when c.Request has no Context.
 | 
					// Done returns nil (chan which will wait forever) when c.Request has no Context.
 | 
				
			||||||
func (c *Context) Done() <-chan struct{} {
 | 
					func (c *Context) Done() <-chan struct{} {
 | 
				
			||||||
	if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
 | 
						if !c.hasRequestContext() {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return c.Request.Context().Done()
 | 
						return c.Request.Context().Done()
 | 
				
			||||||
@ -1198,7 +1205,7 @@ func (c *Context) Done() <-chan struct{} {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Err returns nil when c.Request has no Context.
 | 
					// Err returns nil when c.Request has no Context.
 | 
				
			||||||
func (c *Context) Err() error {
 | 
					func (c *Context) Err() error {
 | 
				
			||||||
	if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
 | 
						if !c.hasRequestContext() {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return c.Request.Context().Err()
 | 
						return c.Request.Context().Err()
 | 
				
			||||||
@ -1219,7 +1226,7 @@ func (c *Context) Value(key any) any {
 | 
				
			|||||||
			return val
 | 
								return val
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
 | 
						if !c.hasRequestContext() {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return c.Request.Context().Value(key)
 | 
						return c.Request.Context().Value(key)
 | 
				
			||||||
 | 
				
			|||||||
@ -2176,6 +2176,24 @@ func TestRemoteIPFail(t *testing.T) {
 | 
				
			|||||||
	assert.False(t, trust)
 | 
						assert.False(t, trust)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHasRequestContext(t *testing.T) {
 | 
				
			||||||
 | 
						c, _ := CreateTestContext(httptest.NewRecorder())
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "no request, no fallback")
 | 
				
			||||||
 | 
						c.engine.ContextWithFallback = true
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "no request, has fallback")
 | 
				
			||||||
 | 
						c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
 | 
				
			||||||
 | 
						assert.True(t, c.hasRequestContext(), "has request, has fallback")
 | 
				
			||||||
 | 
						c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "has request with nil ctx, has fallback")
 | 
				
			||||||
 | 
						c.engine.ContextWithFallback = false
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "has request, no fallback")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						c = &Context{}
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "no request, no engine")
 | 
				
			||||||
 | 
						c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
 | 
				
			||||||
 | 
						assert.False(t, c.hasRequestContext(), "has request, no engine")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
 | 
					func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
 | 
				
			||||||
	c, _ := CreateTestContext(httptest.NewRecorder())
 | 
						c, _ := CreateTestContext(httptest.NewRecorder())
 | 
				
			||||||
	// enable ContextWithFallback feature flag
 | 
						// enable ContextWithFallback feature flag
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user