Imported Upstream version 2.4.2
[scm/test.git] / lfsapi / client.go
index dd6c652..840c4d5 100644 (file)
@@ -85,16 +85,16 @@ func joinURL(prefix, suffix string) string {
 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
        }
@@ -150,7 +150,7 @@ func (c *Client) extraHeaders(u *url.URL) map[string][]string {
        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
@@ -220,7 +220,14 @@ func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, via []*htt
                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 {