Skip to content

Commit

Permalink
Pull out the original checkRedirectCode so we deal with consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
Seanstoppable committed Aug 19, 2024
1 parent ff0bf06 commit 5d0e6c9
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions lib/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,9 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
for {
// For all but the first request, create the next
// request hop and replace req.
loc := req.URL.String()
if len(reqs) > 0 {
loc := resp.Header.Get("Location")
loc = resp.Header.Get("Location")
if loc == "" {
return nil, uerr(fmt.Errorf("%d response missing Location header", resp.StatusCode))
}
Expand Down Expand Up @@ -571,14 +572,6 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
if ref := refererForURL(reqs[len(reqs)-1].URL, req.URL); ref != "" {
req.Header.Set("Referer", ref)
}
err = c.checkRedirect(req, resp, reqs)

// Sentinel error to let users select the
// previous response, without closing its
// body. See Issue 10069.
if err == ErrUseLastResponse {
return resp, nil
}

// Close the previous response's body. But
// read at least some of the body so if it's
Expand All @@ -590,16 +583,6 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
}
resp.Body.Close()

if err != nil {
// Special case for Go 1 compatibility: return both the response
// and an error if the CheckRedirect function failed.
// See https://golang.org/issue/3795
// The resp.Body has already been closed.
ue := uerr(err)
ue.(*url.Error).URL = loc
return resp, ue
}
}

reqs = append(reqs, req)
Expand All @@ -623,6 +606,11 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
return resp, nil
}
if err != nil {
// Special case for Go 1 compatibility: return both the response
// and an error if the CheckRedirect function failed.
// See https://golang.org/issue/3795
ue := uerr(err)
ue.(*url.Error).URL = loc
return resp, err
}

Expand Down

0 comments on commit 5d0e6c9

Please sign in to comment.