Skip to content

Commit

Permalink
martian(proxy): harden roundTrip() on body-less responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Nov 20, 2024
1 parent 9d6770e commit fe63c28
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/martian/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,19 @@ func (p *Proxy) roundTrip(req *http.Request) (*http.Response, error) {
return proxyutil.NewResponse(200, http.NoBody, req), nil
}

return p.rt.RoundTrip(req)
res, err := p.rt.RoundTrip(req)
if err != nil {
return nil, err
}

if isHeadOnlyRfc7230(res) && res.Body != http.NoBody {
log.Errorf(req.Context(), "response is head-only but has a body, closing body")

res.Body.Close()
res.Body = http.NoBody
}

return res, err
}

func (p *Proxy) errorResponse(req *http.Request, err error) *http.Response {
Expand Down

0 comments on commit fe63c28

Please sign in to comment.