mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +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:
parent
2b6c58f6d5
commit
e51e0f9cdd
2 changed files with 9 additions and 0 deletions
|
|
@ -524,10 +524,12 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
||||||
if len(reqs) > 0 {
|
if len(reqs) > 0 {
|
||||||
loc := resp.Header.Get("Location")
|
loc := resp.Header.Get("Location")
|
||||||
if loc == "" {
|
if loc == "" {
|
||||||
|
resp.closeBody()
|
||||||
return nil, uerr(fmt.Errorf("%d response missing Location header", resp.StatusCode))
|
return nil, uerr(fmt.Errorf("%d response missing Location header", resp.StatusCode))
|
||||||
}
|
}
|
||||||
u, err := req.URL.Parse(loc)
|
u, err := req.URL.Parse(loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
resp.closeBody()
|
||||||
return nil, uerr(fmt.Errorf("failed to parse Location header %q: %v", loc, err))
|
return nil, uerr(fmt.Errorf("failed to parse Location header %q: %v", loc, err))
|
||||||
}
|
}
|
||||||
ireq := reqs[0]
|
ireq := reqs[0]
|
||||||
|
|
@ -542,6 +544,7 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
||||||
if includeBody && ireq.GetBody != nil {
|
if includeBody && ireq.GetBody != nil {
|
||||||
req.Body, err = ireq.GetBody()
|
req.Body, err = ireq.GetBody()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
resp.closeBody()
|
||||||
return nil, uerr(err)
|
return nil, uerr(err)
|
||||||
}
|
}
|
||||||
req.ContentLength = ireq.ContentLength
|
req.ContentLength = ireq.ContentLength
|
||||||
|
|
|
||||||
|
|
@ -321,3 +321,9 @@ func (r *Response) Write(w io.Writer) error {
|
||||||
// Success
|
// Success
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Response) closeBody() {
|
||||||
|
if r.Body != nil {
|
||||||
|
r.Body.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue