Commit e51e0f9c authored by Weichao Tang's avatar Weichao Tang Committed by Brad Fitzpatrick

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2b6c58f6
......@@ -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()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment