cleanup follow redirect middleware

otelq
William Perron 2 years ago
parent 6b6ecff1fd
commit 4cc9309374
No known key found for this signature in database
GPG Key ID: D1815C43C9BA3DE1

@ -1,7 +1,6 @@
package http package http
import ( import (
"fmt"
stdhttp "net/http" stdhttp "net/http"
"net/url" "net/url"
) )
@ -29,12 +28,13 @@ func (f *FollowRedirect) RoundTrip(req *stdhttp.Request) (*stdhttp.Response, err
if res.StatusCode >= 300 && res.StatusCode <= 399 { if res.StatusCode >= 300 && res.StatusCode <= 399 {
loc := res.Header.Get("location") loc := res.Header.Get("location")
fmt.Println("==> got a redirect, following...")
if loc != "" { if loc != "" {
u, err := url.Parse(loc) u, err := url.Parse(loc)
if err != nil { if err != nil {
panic("got an invalid URL from the `location` header") // If the location header can't be parsed as a URL, exit early
// and return the original response without attempting to follow
// the redirect.
return res, nil
} }
clone := req.Clone(req.Context()) clone := req.Clone(req.Context())
clone.URL = u clone.URL = u

Loading…
Cancel
Save