| 1 | package lfshttp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | goerrors "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net" |
| 10 | "net/http" |
| 11 | "net/textproto" |
| 12 | "net/url" |
| 13 | "os" |
| 14 | "regexp" |
| 15 | "slices" |
| 16 | "strconv" |
| 17 | "strings" |
| 18 | "sync" |
| 19 | "time" |
| 20 | |
| 21 | spnego "github.com/dpotapov/go-spnego" |
| 22 | "github.com/git-lfs/git-lfs/v3/config" |
| 23 | "github.com/git-lfs/git-lfs/v3/creds" |
| 24 | "github.com/git-lfs/git-lfs/v3/errors" |
| 25 | "github.com/git-lfs/git-lfs/v3/tr" |
| 26 | "github.com/rubyist/tracerx" |
| 27 | "golang.org/x/net/http2" |
| 28 | ) |
| 29 | |
| 30 | const MediaType = "application/vnd.git-lfs+json" |
| 31 | const RequestContentType = MediaType + "; charset=utf-8" |
| 32 | |
| 33 | const ( |
| 34 | defaultConcurrentTransfers = 8 |
| 35 | ) |
| 36 | |
| 37 | // At present, we return a fixed default value of 8 concurrent transfers, |
| 38 | // which is less than ideal for HTTP-based transfers when many objects |
| 39 | // are transferred by a single "git lfs filter-process" command. |
| 40 | // |
| 41 | // However, the "lfs.concurrentTransfers" setting also controls the number |
| 42 | // of processes we spawn for SSH-based or custom transfers, and we want |
| 43 | // to avoid spawning large numbers of separate processes. |
| 44 | func DefaultConcurrentTransfers() int { |
| 45 | return defaultConcurrentTransfers |
| 46 | } |
| 47 | |
| 48 | var ( |
| 49 | UserAgent = "git-lfs" |
| 50 | httpRE = regexp.MustCompile(`\Ahttps?://`) |
| 51 | ) |
| 52 | |
| 53 | type hostData struct { |
| 54 | host string |
| 55 | mode creds.AccessMode |
| 56 | } |
| 57 | |
| 58 | type Client struct { |
| 59 | SSH SSHResolver |
| 60 | |
| 61 | DialTimeout int |
| 62 | KeepaliveTimeout int |
| 63 | TLSTimeout int |
| 64 | ConcurrentTransfers int |
| 65 | SkipSSLVerify bool |
| 66 | |
| 67 | Verbose bool |
| 68 | DebuggingVerbose bool |
| 69 | VerboseOut io.Writer |
| 70 | |
| 71 | hostClients map[hostData]*http.Client |
| 72 | clientMu sync.Mutex |
| 73 | |
| 74 | httpLogger *syncLogger |
| 75 | |
| 76 | gitEnv config.Environment |
| 77 | osEnv config.Environment |
| 78 | uc *config.URLConfig |
| 79 | |
| 80 | credHelperContext *creds.CredentialHelperContext |
| 81 | |
| 82 | sshTries int |
| 83 | } |
| 84 | |
| 85 | func NewClient(ctx Context) *Client { |
| 86 | if ctx == nil { |
| 87 | ctx = NewContext(nil, nil, nil) |
| 88 | } |
| 89 | |
| 90 | gitEnv := ctx.GitEnv() |
| 91 | osEnv := ctx.OSEnv() |
| 92 | |
| 93 | cacheCreds := gitEnv.Bool("lfs.cachecredentials", true) |
| 94 | |
| 95 | // SSHResolver resolves LFS endpoint authentication by running |
| 96 | // git-lfs-authenticate over SSH. The returned credentials (URL |
| 97 | // and headers) are then used for subsequent HTTPS API requests. |
| 98 | var sshResolver SSHResolver = &sshAuthClient{os: osEnv, git: gitEnv} |
| 99 | if cacheCreds { |
| 100 | sshResolver = withSSHCache(sshResolver) |
| 101 | } |
| 102 | |
| 103 | return &Client{ |
| 104 | SSH: sshResolver, |
| 105 | DialTimeout: gitEnv.Int("lfs.dialtimeout", 0), |
| 106 | KeepaliveTimeout: gitEnv.Int("lfs.keepalive", 0), |
| 107 | TLSTimeout: gitEnv.Int("lfs.tlstimeout", 0), |
| 108 | ConcurrentTransfers: gitEnv.Int("lfs.concurrenttransfers", DefaultConcurrentTransfers()), |
| 109 | SkipSSLVerify: !gitEnv.Bool("http.sslverify", true) || osEnv.Bool("GIT_SSL_NO_VERIFY", false), |
| 110 | Verbose: osEnv.Bool("GIT_CURL_VERBOSE", false), |
| 111 | DebuggingVerbose: osEnv.Bool("LFS_DEBUG_HTTP", false), |
| 112 | gitEnv: gitEnv, |
| 113 | osEnv: osEnv, |
| 114 | uc: config.NewURLConfig(gitEnv), |
| 115 | sshTries: gitEnv.Int("lfs.ssh.retries", 5), |
| 116 | credHelperContext: creds.NewCredentialHelperContext(gitEnv, osEnv), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (c *Client) GitEnv() config.Environment { |
| 121 | return c.gitEnv |
| 122 | } |
| 123 | |
| 124 | func (c *Client) OSEnv() config.Environment { |
| 125 | return c.osEnv |
| 126 | } |
| 127 | |
| 128 | func (c *Client) URLConfig() *config.URLConfig { |
| 129 | return c.uc |
| 130 | } |
| 131 | |
| 132 | func (c *Client) NewRequest(method string, e Endpoint, suffix string, body interface{}) (*http.Request, error) { |
| 133 | if strings.HasPrefix(e.Url, "file://") { |
| 134 | // Initial `\n` to avoid overprinting `Downloading LFS...`. |
| 135 | fmt.Fprint(os.Stderr, "\n", tr.Tr.Get(`hint: The remote resolves to a file:// URL, which can only work with a |
| 136 | hint: standalone transfer agent. See section "Using a Custom Transfer Type |
| 137 | hint: without the API server" in custom-transfers.md for details.`), "\n") |
| 138 | } |
| 139 | |
| 140 | sshRes, err := c.sshResolveWithRetries(e, method) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | |
| 145 | prefix := e.Url |
| 146 | if len(sshRes.Href) > 0 { |
| 147 | prefix = sshRes.Href |
| 148 | } |
| 149 | |
| 150 | if !httpRE.MatchString(prefix) { |
| 151 | urlfragment := strings.SplitN(prefix, "?", 2)[0] |
| 152 | return nil, errors.New(tr.Tr.Get("missing protocol: %q", urlfragment)) |
| 153 | } |
| 154 | |
| 155 | req, err := http.NewRequest(method, joinURL(prefix, suffix), nil) |
| 156 | if err != nil { |
| 157 | return req, err |
| 158 | } |
| 159 | |
| 160 | for key, value := range sshRes.Header { |
| 161 | req.Header.Set(key, value) |
| 162 | } |
| 163 | req.Header.Set("Accept", MediaType) |
| 164 | |
| 165 | if body != nil { |
| 166 | if merr := MarshalToRequest(req, body); merr != nil { |
| 167 | return req, merr |
| 168 | } |
| 169 | req.Header.Set("Content-Type", RequestContentType) |
| 170 | } |
| 171 | |
| 172 | return req, err |
| 173 | } |
| 174 | |
| 175 | const slash = "/" |
| 176 | |
| 177 | func joinURL(prefix, suffix string) string { |
| 178 | if strings.HasSuffix(prefix, slash) { |
| 179 | return prefix + suffix |
| 180 | } |
| 181 | return prefix + slash + suffix |
| 182 | } |
| 183 | |
| 184 | // Do sends an HTTP request to get an HTTP response. It wraps net/http, adding |
| 185 | // extra headers, redirection handling, and error reporting. |
| 186 | func (c *Client) Do(req *http.Request) (*http.Response, error) { |
| 187 | req.Header = c.ExtraHeadersFor(req) |
| 188 | |
| 189 | return c.do(req, "", nil, creds.NoneAccess) |
| 190 | } |
| 191 | |
| 192 | // DoWithAccess sends an HTTP request to get an HTTP response using the |
| 193 | // specified access mode. It wraps net/http, adding extra headers, redirection |
| 194 | // handling, and error reporting. |
| 195 | func (c *Client) DoWithAccess(req *http.Request, mode creds.AccessMode) (*http.Response, error) { |
| 196 | req.Header = c.ExtraHeadersFor(req) |
| 197 | |
| 198 | return c.do(req, "", nil, mode) |
| 199 | } |
| 200 | |
| 201 | // do performs an *http.Request respecting redirects, and handles the response |
| 202 | // as defined in c.handleResponse. Notably, it does not alter the headers for |
| 203 | // the request argument in any way. |
| 204 | func (c *Client) do(req *http.Request, remote string, via []*http.Request, mode creds.AccessMode) (*http.Response, error) { |
| 205 | req.Header.Set("User-Agent", UserAgent) |
| 206 | |
| 207 | client, err := c.HttpClient(req.URL, mode) |
| 208 | if err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | |
| 212 | res, err := c.doWithRedirects(client, req, remote, via) |
| 213 | if err != nil { |
| 214 | return res, err |
| 215 | } |
| 216 | |
| 217 | return res, c.handleResponse(res) |
| 218 | } |
| 219 | |
| 220 | // Close closes any resources that this client opened. |
| 221 | func (c *Client) Close() error { |
| 222 | return c.httpLogger.Close() |
| 223 | } |
| 224 | |
| 225 | func (c *Client) sshResolveWithRetries(e Endpoint, method string) (*sshAuthResponse, error) { |
| 226 | var sshRes sshAuthResponse |
| 227 | var err error |
| 228 | |
| 229 | uc := config.NewURLConfig(c.gitEnv) |
| 230 | if val, ok := uc.Get("lfs", e.OriginalUrl, "sshtransfer"); ok && val != "negotiate" && val != "never" { |
| 231 | tracerx.Printf("skipping SSH-HTTPS hybrid protocol connection by request") |
| 232 | return nil, errors.New("git-lfs-authenticate has been disabled by request") |
| 233 | } |
| 234 | |
| 235 | requests := max(0, c.sshTries) + 1 |
| 236 | for i := 0; i < requests; i++ { |
| 237 | sshRes, err = c.SSH.Resolve(e, method) |
| 238 | if err == nil { |
| 239 | return &sshRes, nil |
| 240 | } |
| 241 | |
| 242 | // If the git-lfs-authenticate command is unavailable on the |
| 243 | // server, fall back to the guessed LFS endpoint (per the server |
| 244 | // discovery spec) by returning an empty response with no error. |
| 245 | // Retrying would not help, so stop immediately. |
| 246 | var unavailable *sshAuthenticateUnavailableError |
| 247 | if goerrors.As(err, &unavailable) { |
| 248 | tracerx.Printf( |
| 249 | "ssh: %s does not provide git-lfs-authenticate, falling back to guessed LFS endpoint", |
| 250 | e.SSHMetadata.UserAndHost, |
| 251 | ) |
| 252 | return &sshAuthResponse{}, nil |
| 253 | } |
| 254 | |
| 255 | tracerx.Printf( |
| 256 | "ssh: %s failed, error: %s, message: %s (try: %d/%d)", |
| 257 | e.SSHMetadata.UserAndHost, err.Error(), sshRes.Message, i, |
| 258 | requests, |
| 259 | ) |
| 260 | } |
| 261 | |
| 262 | if len(sshRes.Message) > 0 { |
| 263 | return nil, errors.Wrap(err, sshRes.Message) |
| 264 | } |
| 265 | return nil, err |
| 266 | } |
| 267 | |
| 268 | func (c *Client) ExtraHeadersFor(req *http.Request) http.Header { |
| 269 | extraHeaders := c.extraHeaders(req.URL) |
| 270 | if len(extraHeaders) == 0 { |
| 271 | return req.Header |
| 272 | } |
| 273 | |
| 274 | copy := make(http.Header, len(req.Header)) |
| 275 | for k, vs := range req.Header { |
| 276 | copy[k] = vs |
| 277 | } |
| 278 | |
| 279 | for k, vs := range extraHeaders { |
| 280 | for _, v := range vs { |
| 281 | if !slices.Contains(copy[k], v) { |
| 282 | copy[k] = append(copy[k], v) |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | return copy |
| 287 | } |
| 288 | |
| 289 | func (c *Client) extraHeaders(u *url.URL) map[string][]string { |
| 290 | hdrs := c.uc.GetAll("http", u.String(), "extraHeader") |
| 291 | m := make(map[string][]string, len(hdrs)) |
| 292 | |
| 293 | for _, hdr := range hdrs { |
| 294 | parts := strings.SplitN(hdr, ":", 2) |
| 295 | if len(parts) < 2 { |
| 296 | continue |
| 297 | } |
| 298 | |
| 299 | k, v := parts[0], strings.TrimSpace(parts[1]) |
| 300 | // If header keys are given in non-canonicalized form (e.g., |
| 301 | // "AUTHORIZATION" as opposed to "Authorization") they will not |
| 302 | // be returned in calls to net/http.Header.Get(). |
| 303 | // |
| 304 | // So, we avoid this problem by first canonicalizing header keys |
| 305 | // for extra headers. |
| 306 | k = textproto.CanonicalMIMEHeaderKey(k) |
| 307 | |
| 308 | m[k] = append(m[k], v) |
| 309 | } |
| 310 | return m |
| 311 | } |
| 312 | |
| 313 | func (c *Client) DoWithRedirect(cli *http.Client, req *http.Request, remote string, via []*http.Request) (*http.Request, *http.Response, error) { |
| 314 | tracedReq, err := c.traceRequest(req) |
| 315 | if err != nil { |
| 316 | return nil, nil, err |
| 317 | } |
| 318 | |
| 319 | var retries int |
| 320 | if n, ok := Retries(req); ok { |
| 321 | retries = n |
| 322 | } else { |
| 323 | retries = defaultRequestRetries |
| 324 | } |
| 325 | |
| 326 | var res *http.Response |
| 327 | |
| 328 | requests := max(0, retries) + 1 |
| 329 | for i := 0; i < requests; i++ { |
| 330 | res, err = cli.Do(req) |
| 331 | if err == nil { |
| 332 | break |
| 333 | } |
| 334 | |
| 335 | if seek, ok := req.Body.(io.Seeker); ok { |
| 336 | seek.Seek(0, io.SeekStart) |
| 337 | } |
| 338 | |
| 339 | c.traceResponse(req, tracedReq, nil) |
| 340 | } |
| 341 | |
| 342 | if err != nil { |
| 343 | c.traceResponse(req, tracedReq, nil) |
| 344 | // SPNEGO (Negotiate) errors are authentication errors. |
| 345 | var spnegoErr *spnego.Error |
| 346 | if goerrors.As(err, &spnegoErr) { |
| 347 | tracerx.Printf("http: got Negotiate error: %s", spnegoErr.Error()) |
| 348 | return nil, nil, errors.NewAuthError(err) |
| 349 | } |
| 350 | return nil, nil, err |
| 351 | } |
| 352 | |
| 353 | if res == nil { |
| 354 | return nil, nil, nil |
| 355 | } |
| 356 | |
| 357 | if res.Uncompressed { |
| 358 | tracerx.Printf("http: decompressed gzipped response") |
| 359 | } |
| 360 | |
| 361 | c.traceResponse(req, tracedReq, res) |
| 362 | |
| 363 | if res.StatusCode != 301 && |
| 364 | res.StatusCode != 302 && |
| 365 | res.StatusCode != 303 && |
| 366 | res.StatusCode != 307 && |
| 367 | res.StatusCode != 308 { |
| 368 | |
| 369 | // Above are the list of 3xx status codes that we know |
| 370 | // how to handle below. If the status code contained in |
| 371 | // the HTTP response was none of them, return the (res, |
| 372 | // err) tuple as-is, otherwise handle the redirect. |
| 373 | return nil, res, c.handleResponse(res) |
| 374 | } |
| 375 | |
| 376 | redirectTo := res.Header.Get("Location") |
| 377 | locurl, err := url.Parse(redirectTo) |
| 378 | if err == nil && !locurl.IsAbs() { |
| 379 | locurl = req.URL.ResolveReference(locurl) |
| 380 | redirectTo = locurl.String() |
| 381 | } |
| 382 | |
| 383 | via = append(via, req) |
| 384 | if len(via) >= 3 { |
| 385 | return nil, res, errors.New(tr.Tr.Get("too many redirects")) |
| 386 | } |
| 387 | |
| 388 | redirectedReq, err := newRequestForRetry(req, redirectTo) |
| 389 | if err != nil { |
| 390 | return nil, res, err |
| 391 | } |
| 392 | |
| 393 | res.Body.Close() |
| 394 | |
| 395 | return redirectedReq, nil, nil |
| 396 | } |
| 397 | |
| 398 | func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, remote string, via []*http.Request) (*http.Response, error) { |
| 399 | redirectedReq, res, err := c.DoWithRedirect(cli, req, remote, via) |
| 400 | if err != nil || res != nil { |
| 401 | return res, err |
| 402 | } |
| 403 | |
| 404 | if redirectedReq == nil { |
| 405 | return nil, errors.New(tr.Tr.Get("failed to redirect request")) |
| 406 | } |
| 407 | |
| 408 | return c.doWithRedirects(cli, redirectedReq, remote, via) |
| 409 | } |
| 410 | |
| 411 | func (c *Client) configureProtocols(u *url.URL, transport *http.Transport) error { |
| 412 | version, _ := c.uc.Get("http", u.String(), "version") |
| 413 | switch version { |
| 414 | case "HTTP/1.1": |
| 415 | // This disables HTTP/2, according to the documentation. |
| 416 | transport.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper) |
| 417 | case "HTTP/2": |
| 418 | if u.Scheme != "https" { |
| 419 | return errors.New(tr.Tr.Get("HTTP/2 cannot be used except with TLS")) |
| 420 | } |
| 421 | http2.ConfigureTransport(transport) |
| 422 | delete(transport.TLSNextProto, "http/1.1") |
| 423 | case "": |
| 424 | http2.ConfigureTransport(transport) |
| 425 | default: |
| 426 | return errors.New(tr.Tr.Get("Unknown HTTP version %q", version)) |
| 427 | } |
| 428 | return nil |
| 429 | } |
| 430 | |
| 431 | func (c *Client) Transport(u *url.URL, access creds.AccessMode) (http.RoundTripper, error) { |
| 432 | host := u.Host |
| 433 | |
| 434 | if c.gitEnv == nil { |
| 435 | c.gitEnv = make(testEnv) |
| 436 | } |
| 437 | |
| 438 | if c.osEnv == nil { |
| 439 | c.osEnv = make(testEnv) |
| 440 | } |
| 441 | |
| 442 | concurrentTransfers := c.ConcurrentTransfers |
| 443 | if concurrentTransfers < 1 { |
| 444 | concurrentTransfers = DefaultConcurrentTransfers() |
| 445 | } |
| 446 | |
| 447 | dialtime := c.DialTimeout |
| 448 | if dialtime < 1 { |
| 449 | dialtime = 30 |
| 450 | } |
| 451 | |
| 452 | keepalivetime := c.KeepaliveTimeout |
| 453 | if keepalivetime < 1 { |
| 454 | keepalivetime = 1800 |
| 455 | } |
| 456 | |
| 457 | tlstime := c.TLSTimeout |
| 458 | if tlstime < 1 { |
| 459 | tlstime = 30 |
| 460 | } |
| 461 | tr := &http.Transport{ |
| 462 | Proxy: proxyFromClient(c), |
| 463 | TLSHandshakeTimeout: time.Duration(tlstime) * time.Second, |
| 464 | MaxIdleConnsPerHost: concurrentTransfers, |
| 465 | } |
| 466 | |
| 467 | activityTimeout := 30 |
| 468 | if v, ok := c.uc.Get("lfs", u.String(), "activitytimeout"); ok { |
| 469 | if i, err := strconv.Atoi(v); err == nil { |
| 470 | activityTimeout = i |
| 471 | } else { |
| 472 | activityTimeout = 0 |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | dialer := &net.Dialer{ |
| 477 | Timeout: time.Duration(dialtime) * time.Second, |
| 478 | KeepAlive: time.Duration(keepalivetime) * time.Second, |
| 479 | DualStack: true, |
| 480 | } |
| 481 | |
| 482 | if activityTimeout > 0 { |
| 483 | activityDuration := time.Duration(activityTimeout) * time.Second |
| 484 | tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 485 | c, err := dialer.DialContext(ctx, network, addr) |
| 486 | if c == nil { |
| 487 | return c, err |
| 488 | } |
| 489 | if tc, ok := c.(*net.TCPConn); ok { |
| 490 | tc.SetKeepAlive(true) |
| 491 | tc.SetKeepAlivePeriod(dialer.KeepAlive) |
| 492 | } |
| 493 | return &deadlineConn{Timeout: activityDuration, Conn: c}, err |
| 494 | } |
| 495 | } else { |
| 496 | tr.DialContext = dialer.DialContext |
| 497 | } |
| 498 | |
| 499 | tr.TLSClientConfig = &tls.Config{ |
| 500 | Renegotiation: tls.RenegotiateFreelyAsClient, |
| 501 | } |
| 502 | |
| 503 | if isClientCertEnabledForHost(c, host) { |
| 504 | tracerx.Printf("http: client cert for %s", host) |
| 505 | cert, err := getClientCertForHost(c, host) |
| 506 | if err != nil { |
| 507 | return nil, err |
| 508 | } |
| 509 | if cert != nil { |
| 510 | tr.TLSClientConfig.Certificates = []tls.Certificate{*cert} |
| 511 | tr.TLSClientConfig.BuildNameToCertificate() |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if isCertVerificationDisabledForHost(c, host) { |
| 516 | tr.TLSClientConfig.InsecureSkipVerify = true |
| 517 | } else { |
| 518 | tr.TLSClientConfig.RootCAs = getRootCAsForHostFromGitconfig(c, host) |
| 519 | } |
| 520 | |
| 521 | if err := c.configureProtocols(u, tr); err != nil { |
| 522 | return nil, err |
| 523 | } |
| 524 | |
| 525 | if access == creds.NegotiateAccess { |
| 526 | // This technically copies a mutex, but we know since we've just created |
| 527 | // the object that this mutex is unlocked. |
| 528 | return &spnego.Transport{Transport: *tr}, nil |
| 529 | } |
| 530 | return tr, nil |
| 531 | } |
| 532 | |
| 533 | func (c *Client) HttpClient(u *url.URL, access creds.AccessMode) (*http.Client, error) { |
| 534 | c.clientMu.Lock() |
| 535 | defer c.clientMu.Unlock() |
| 536 | |
| 537 | host := u.Host |
| 538 | |
| 539 | if c.hostClients == nil { |
| 540 | c.hostClients = make(map[hostData]*http.Client) |
| 541 | } |
| 542 | |
| 543 | hd := hostData{host: host, mode: access} |
| 544 | |
| 545 | if client, ok := c.hostClients[hd]; ok { |
| 546 | return client, nil |
| 547 | } |
| 548 | |
| 549 | tr, err := c.Transport(u, access) |
| 550 | if err != nil { |
| 551 | return nil, err |
| 552 | } |
| 553 | |
| 554 | httpClient := &http.Client{ |
| 555 | Transport: tr, |
| 556 | CheckRedirect: func(*http.Request, []*http.Request) error { |
| 557 | return http.ErrUseLastResponse |
| 558 | }, |
| 559 | } |
| 560 | |
| 561 | if isCookieJarEnabledForHost(c, host) { |
| 562 | tracerx.Printf("http: cookieFile for %s", host) |
| 563 | if cookieJar, err := getCookieJarForHost(c, host); err == nil { |
| 564 | httpClient.Jar = cookieJar |
| 565 | } else { |
| 566 | tracerx.Printf("http: error while reading cookieFile: %s", err.Error()) |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | c.hostClients[hd] = httpClient |
| 571 | if c.VerboseOut == nil { |
| 572 | c.VerboseOut = os.Stderr |
| 573 | } |
| 574 | |
| 575 | return httpClient, nil |
| 576 | } |
| 577 | |
| 578 | func (c *Client) CurrentUser() (string, string) { |
| 579 | userName, _ := c.gitEnv.Get("user.name") |
| 580 | userEmail, _ := c.gitEnv.Get("user.email") |
| 581 | return userName, userEmail |
| 582 | } |
| 583 | |
| 584 | func newRequestForRetry(req *http.Request, location string) (*http.Request, error) { |
| 585 | newReq, err := http.NewRequest(req.Method, location, nil) |
| 586 | if err != nil { |
| 587 | return nil, err |
| 588 | } |
| 589 | |
| 590 | if req.URL.Scheme == "https" && newReq.URL.Scheme == "http" { |
| 591 | return nil, errors.New(tr.Tr.Get("refusing insecure redirect: HTTPS to HTTP")) |
| 592 | } |
| 593 | |
| 594 | sameHost := req.URL.Host == newReq.URL.Host |
| 595 | for key := range req.Header { |
| 596 | if key == "Authorization" { |
| 597 | if !sameHost { |
| 598 | continue |
| 599 | } |
| 600 | } |
| 601 | newReq.Header.Set(key, req.Header.Get(key)) |
| 602 | } |
| 603 | |
| 604 | oldestURL := strings.SplitN(req.URL.String(), "?", 2)[0] |
| 605 | newURL := strings.SplitN(newReq.URL.String(), "?", 2)[0] |
| 606 | tracerx.Printf("api: redirect %s %s to %s", req.Method, oldestURL, newURL) |
| 607 | |
| 608 | // This body will have already been rewound from a call to |
| 609 | // lfsapi.Client.traceRequest(). |
| 610 | newReq.Body = req.Body |
| 611 | newReq.ContentLength = req.ContentLength |
| 612 | |
| 613 | // Copy the request's context.Context, if any. |
| 614 | newReq = newReq.WithContext(req.Context()) |
| 615 | |
| 616 | return newReq, nil |
| 617 | } |
| 618 | |
| 619 | type deadlineConn struct { |
| 620 | Timeout time.Duration |
| 621 | net.Conn |
| 622 | } |
| 623 | |
| 624 | func (c *deadlineConn) Read(b []byte) (int, error) { |
| 625 | if err := c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil { |
| 626 | return 0, err |
| 627 | } |
| 628 | return c.Conn.Read(b) |
| 629 | } |
| 630 | |
| 631 | func (c *deadlineConn) Write(b []byte) (int, error) { |
| 632 | if err := c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil { |
| 633 | return 0, err |
| 634 | } |
| 635 | |
| 636 | return c.Conn.Write(b) |
| 637 | } |
| 638 | |
| 639 | func init() { |
| 640 | UserAgent = config.VersionDesc |
| 641 | } |
| 642 | |
| 643 | type testEnv map[string]string |
| 644 | |
| 645 | func (e testEnv) Get(key string) (v string, ok bool) { |
| 646 | v, ok = e[key] |
| 647 | return |
| 648 | } |
| 649 | |
| 650 | func (e testEnv) GetAll(key string) []string { |
| 651 | if v, ok := e.Get(key); ok { |
| 652 | return []string{v} |
| 653 | } |
| 654 | return make([]string, 0) |
| 655 | } |
| 656 | |
| 657 | func (e testEnv) Int(key string, def int) int { |
| 658 | s, _ := e.Get(key) |
| 659 | return config.Int(s, def) |
| 660 | } |
| 661 | |
| 662 | func (e testEnv) Int64(key string, def int64) int64 { |
| 663 | s, _ := e.Get(key) |
| 664 | return config.Int64(s, def) |
| 665 | } |
| 666 | |
| 667 | func (e testEnv) Bool(key string, def bool) bool { |
| 668 | s, _ := e.Get(key) |
| 669 | return config.Bool(s, def) |
| 670 | } |
| 671 | |
| 672 | func (e testEnv) All() map[string][]string { |
| 673 | m := make(map[string][]string) |
| 674 | for k, _ := range e { |
| 675 | m[k] = e.GetAll(k) |
| 676 | } |
| 677 | return m |
| 678 | } |