// Internal http request management func doHttpRequest(cfg *config.Configuration, req *http.Request, creds auth.Creds) (*http.Response, error) { var ( res *http.Response cause string err error ) if cfg.NtlmAccess(auth.GetOperationForRequest(req)) { cause = "ntlm" res, err = doNTLMRequest(cfg, req, true) } else { cause = "http" res, err = NewHttpClient(cfg, req.Host).Do(req) } if res == nil { res = &http.Response{ StatusCode: 0, Header: make(http.Header), Request: req, Body: ioutil.NopCloser(bytes.NewBufferString("")), } } if err != nil { if errors.IsAuthError(err) { SetAuthType(cfg, req, res) doHttpRequest(cfg, req, creds) } else { err = errors.Wrap(err, cause) } } else { err = handleResponse(cfg, res, creds) } if err != nil { if res != nil { SetErrorResponseContext(cfg, err, res) } else { setErrorRequestContext(cfg, err, req) } } return res, err }
// doApiBatchRequest runs the request to the LFS batch API. If the API returns a // 401, the repo will be marked as having private access and the request will be // re-run. When the repo is marked as having private access, credentials will // be retrieved. func DoBatchRequest(req *http.Request) (*http.Response, *batchResponse, error) { res, err := DoRequest(req, config.Config.PrivateAccess(auth.GetOperationForRequest(req))) if err != nil { if res != nil && res.StatusCode == 401 { return res, nil, errutil.NewAuthError(err) } return res, nil, err } resp := &batchResponse{} err = httputil.DecodeResponse(res, resp) if err != nil { httputil.SetErrorResponseContext(err, res) } return res, resp, err }
func setErrorRequestContext(err error, req *http.Request) { errutil.ErrorSetContext(err, "Endpoint", config.Config.Endpoint(auth.GetOperationForRequest(req)).Url) errutil.ErrorSetContext(err, "URL", TraceHttpReq(req)) setErrorHeaderContext(err, "Response", req.Header) }
func SetAuthType(req *http.Request, res *http.Response) { authType := GetAuthType(res) operation := auth.GetOperationForRequest(req) config.Config.SetAccess(operation, authType) tracerx.Printf("api: http response indicates %q authentication. Resubmitting...", authType) }