Skip to content

Commit

Permalink
Preserve HTTP headers on redirect
Browse files Browse the repository at this point in the history
This ensures things like our custom UserAgent get preserved if the
server does a redirect (my mirror enforces SSL, for example).

See golang/go#4800

Code cribbed from gyuho/gon
  • Loading branch information
pdf committed Jan 5, 2015
1 parent 359a5cd commit 9aeb23b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gomusicbrainz.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ package gomusicbrainz
import (
"encoding/xml"
"errors"
"fmt"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -111,6 +112,25 @@ func (c *WS2Client) getReqeust(data interface{}, params url.Values, endpoint str

client := &http.Client{}

defaultRedirectLimit := 30

// Preserve headers on redirect
// See: https://github.com/golang/go/issues/4800
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > defaultRedirectLimit {
return fmt.Errorf("%d consecutive requests(redirects)", len(via))
}
if len(via) == 0 {
// No redirects
return nil
}
// mutate the subsequent redirect requests with the first Header
for key, val := range via[0].Header {
req.Header[key] = val
}
return nil
}

reqUrl := *c.WS2RootURL
reqUrl.Path = path.Join(reqUrl.Path, endpoint)
reqUrl.RawQuery = params.Encode()
Expand Down

0 comments on commit 9aeb23b

Please sign in to comment.