func (c *Client) Do(req *http.Request) (*http.Response, error) {
req.Header = c.extraHeadersFor(req)
- return c.do(req)
+ return c.do(req, "", nil)
}
// do performs an *http.Request respecting redirects, and handles the response
// as defined in c.handleResponse. Notably, it does not alter the headers for
// the request argument in any way.
-func (c *Client) do(req *http.Request) (*http.Response, error) {
+func (c *Client) do(req *http.Request, remote string, via []*http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", UserAgent)
- res, err := c.doWithRedirects(c.httpClient(req.Host), req, nil)
+ res, err := c.doWithRedirects(c.httpClient(req.Host), req, remote, via)
if err != nil {
return res, err
}
return m
}
-func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, via []*http.Request) (*http.Response, error) {
+func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, remote string, via []*http.Request) (*http.Response, error) {
tracedReq, err := c.traceRequest(req)
if err != nil {
return nil, err
return res, err
}
- return c.doWithRedirects(cli, redirectedReq, via)
+ if len(req.Header.Get("Authorization")) > 0 {
+ // If the original request was authenticated (noted by the
+ // presence of the Authorization header), then recur through
+ // doWithAuth, retaining the requests via but only after
+ // authenticating the redirected request.
+ return c.doWithAuth(remote, redirectedReq, via)
+ }
+ return c.doWithRedirects(cli, redirectedReq, remote, via)
}
func (c *Client) httpClient(host string) *http.Client {