Skip to content

Commit

Permalink
Ensure responses are context.ResponseWriters (go-gitea#19843)
Browse files Browse the repository at this point in the history
In order for web.Wrap to be able to detect if a response has been written
we need to wrap any non-context.ResponseWriters as a such. Otherwise
responses will be incorrectly detected as non-written to and handlers can
double run.

In the case of GZip this handler will change the response to a non-context.RW
and this failure to correctly detect response writing causes fallthrough and
a NPE.

Fix go-gitea#19839

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath authored and AbdulrhmnGhanem committed Aug 23, 2022
1 parent b7ddd52 commit 91d2844
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/web/wrap_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func convertHandler(handler interface{}) wrappedHandlerFunc {
case http.HandlerFunc:
return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
routing.UpdateFuncInfo(req.Context(), funcInfo)
if _, ok := resp.(context.ResponseWriter); !ok {
resp = context.NewResponse(resp)
}
t(resp, req)
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
done = true
Expand Down Expand Up @@ -92,6 +95,9 @@ func convertHandler(handler interface{}) wrappedHandlerFunc {
next = wrapInternal(others)
}
routing.UpdateFuncInfo(req.Context(), funcInfo)
if _, ok := resp.(context.ResponseWriter); !ok {
resp = context.NewResponse(resp)
}
t(next).ServeHTTP(resp, req)
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
done = true
Expand Down

0 comments on commit 91d2844

Please sign in to comment.