-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Closed
Labels
Milestone
Description
- What version of Go are you using (go version)?
go version go1.4.2 darwin/amd64 - What operating system and processor architecture are you using?
Not really relevant to this issue...
MacBook Pro (15-inch, Mid 2012)
2.6 GHz Intel Core i7
16 GB 1600 MHz DDR3 - What did you do?
Implemented a custom redirection policy handler viahttp.Client'sCheckRedirectto stop redirection.
After aDo()call on a request, the returned response'sBodyis not nil and reading from it returns an errorhttp: read on closed response body. See example code below. - What did you expect to see?
Expected either an unclosed, unread-from non-nilBody, or anilBody. - What did you see instead?
Saw a closed, already-read-fromBodythat was notnil.
This unexpected condition required me to hack around the case by explicitly setting resp.Body to nil when detecting my custom redirect policy abort error from the returned url.Error.
Also, I have no access to the intermediate redirection responses. I would want an interception function to be able to inspect the redirection response to see if I should redirect or not, but instead I get a request instance; what good is the request to determine if I should redirect? This design also prevents me from logging relevant redirection details to the end user, so as to properly trace the request-response path.
// Define our redirection policy:
const redirects_max_follow = 0 // for this test case
var redirectPolicyError = errors.New("redirect policy")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) > redirects_max_follow {
return redirectPolicyError
}
return nil
},
}
// Make the request:
resp, err := client.Do(req)
if err != nil {
if uerr, ok := err.(*url.Error); ok {
if uerr.Err == redirectPolicyError {
// Redirection responses get their bodies removed by `Do()` automatically.
// Not sure if this is a bug or not.
// Setting Body to nil prevents future "http: read on closed response body" error.
resp.Body = nil
goto ok
}
}
Error("HTTP error: %s\n", err)
return -2
ok:
}