Skip to content

Commit

Permalink
Fix data race in MITM'ed HTTPS request handling.[comment](golang/go#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
telanflow committed Sep 1, 2023
1 parent e7f816b commit 5a2b7e7
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ func (ctx *Context) UseFunc(fns ...MiddlewareFunc) {
// Next to exec middlewares
// Execute the next middleware as a linked list. "ctx.Next(req)"
// eg:
// func Handle(req *http.Request, ctx *Context) (*http.Response, error) {
// // You can do anything to modify the http.Request ...
// resp, err := ctx.Next(req)
// // You can do anything to modify the http.Response ...
// return resp, err
// }
//
// func Handle(req *http.Request, ctx *Context) (*http.Response, error) {
// // You can do anything to modify the http.Request ...
// resp, err := ctx.Next(req)
// // You can do anything to modify the http.Response ...
// return resp, err
// }
//
// Alternatively, you can simply return the response without executing `ctx.Next()`,
// which will interrupt subsequent middleware execution.
Expand All @@ -132,7 +133,13 @@ func (ctx *Context) Next(req *http.Request) (*http.Response, error) {
if isWebSocketRequest(req) {
return nil, RequestWebsocketUpgradeErr
}
return ctx.RoundTrip(req)

return func() (*http.Response, error) {
// explicitly discard request body to avoid data races in certain RoundTripper implementations
// see https://github.com/golang/go/issues/61596#issuecomment-1652345131
defer req.Body.Close()
return ctx.RoundTrip(req)
}()
}

middleware := ctx.middlewares[ctx.mi]
Expand Down

0 comments on commit 5a2b7e7

Please sign in to comment.