mirror of
https://github.com/golang/go
synced 2025-06-07 16:40:46 +00:00
net/http: close resp.Body when error occurred during redirection
Fixes #19976 Change-Id: I48486467066784a9dcc24357ec94a1be85265a6f Reviewed-on: https://go-review.googlesource.com/40940 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
2b6c58f6d5
commit
e51e0f9cdd
src/net/http
@ -524,10 +524,12 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
||||
if len(reqs) > 0 {
|
||||
loc := resp.Header.Get("Location")
|
||||
if loc == "" {
|
||||
resp.closeBody()
|
||||
return nil, uerr(fmt.Errorf("%d response missing Location header", resp.StatusCode))
|
||||
}
|
||||
u, err := req.URL.Parse(loc)
|
||||
if err != nil {
|
||||
resp.closeBody()
|
||||
return nil, uerr(fmt.Errorf("failed to parse Location header %q: %v", loc, err))
|
||||
}
|
||||
ireq := reqs[0]
|
||||
@ -542,6 +544,7 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
||||
if includeBody && ireq.GetBody != nil {
|
||||
req.Body, err = ireq.GetBody()
|
||||
if err != nil {
|
||||
resp.closeBody()
|
||||
return nil, uerr(err)
|
||||
}
|
||||
req.ContentLength = ireq.ContentLength
|
||||
|
@ -321,3 +321,9 @@ func (r *Response) Write(w io.Writer) error {
|
||||
// Success
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Response) closeBody() {
|
||||
if r.Body != nil {
|
||||
r.Body.Close()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user