mirror of
https://github.com/gin-gonic/gin.git
synced 2026-04-29 23:23:18 +08:00
feat: add warning for multiple response writes (Issue #4477)
Add debug warning when multiple response writes are detected in Context.Render(). This helps developers identify potential issues where multiple calls to response writing methods (JSON, XML, String, etc.) would concatenate outputs. Changes: - Add wroteResponse bool field to Context struct - Reset wroteResponse in Context.reset() method - Check and warn in Context.Render() before writing - Set wroteResponse=true after successful write The warning is only printed in debug mode using debugPrint(), which is controlled by gin.SetMode(). In release mode, no warning is printed.
This commit is contained in:
parent
d3ffc99852
commit
094ea8276a
11
context.go
11
context.go
@ -94,6 +94,11 @@ type Context struct {
|
||||
// SameSite allows a server to define a cookie attribute making it impossible for
|
||||
// the browser to send this cookie along with cross-site requests.
|
||||
sameSite http.SameSite
|
||||
|
||||
// wroteResponse tracks whether a response has been written to the client.
|
||||
// This is used to warn about multiple response writes, which can lead to
|
||||
// unexpected behavior.
|
||||
wroteResponse bool
|
||||
}
|
||||
|
||||
/************************************/
|
||||
@ -113,6 +118,7 @@ func (c *Context) reset() {
|
||||
c.queryCache = nil
|
||||
c.formCache = nil
|
||||
c.sameSite = 0
|
||||
c.wroteResponse = false
|
||||
*c.params = (*c.params)[:0]
|
||||
*c.skippedNodes = (*c.skippedNodes)[:0]
|
||||
}
|
||||
@ -1150,11 +1156,15 @@ func (c *Context) Cookie(name string) (string, error) {
|
||||
|
||||
// Render writes the response headers and calls render.Render to render data.
|
||||
func (c *Context) Render(code int, r render.Render) {
|
||||
if c.wroteResponse {
|
||||
debugPrint("[WARNING] Multiple response writes detected. This may cause unexpected behavior.")
|
||||
}
|
||||
c.Status(code)
|
||||
|
||||
if !bodyAllowedForStatus(code) {
|
||||
r.WriteContentType(c.Writer)
|
||||
c.Writer.WriteHeaderNow()
|
||||
c.wroteResponse = true
|
||||
return
|
||||
}
|
||||
|
||||
@ -1163,6 +1173,7 @@ func (c *Context) Render(code int, r render.Render) {
|
||||
_ = c.Error(err)
|
||||
c.Abort()
|
||||
}
|
||||
c.wroteResponse = true
|
||||
}
|
||||
|
||||
// HTML renders the HTTP template specified by its file name.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user