(ctx context.Context)
| 614 | } |
| 615 | |
| 616 | func (r *request) do(ctx context.Context) (*http.Response, error) { |
| 617 | req, err := http.NewRequestWithContext(ctx, r.method, r.String(), nil) |
| 618 | if err != nil { |
| 619 | return nil, err |
| 620 | } |
| 621 | if r.header == nil { |
| 622 | req.Header = http.Header{} |
| 623 | } else { |
| 624 | req.Header = r.header.Clone() // headers need to be copied to avoid concurrent map access |
| 625 | } |
| 626 | if r.body != nil { |
| 627 | body, err := r.body() |
| 628 | if err != nil { |
| 629 | return nil, err |
| 630 | } |
| 631 | req.Body = body |
| 632 | req.GetBody = r.body |
| 633 | if r.size > 0 { |
| 634 | req.ContentLength = r.size |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | ctx = log.WithLogger(ctx, log.G(ctx).WithField("url", r.sanitizedURL())) |
| 639 | log.G(ctx).WithFields(requestFields(req)).Debug("do request") |
| 640 | if err := r.authorize(ctx, req); err != nil { |
| 641 | return nil, fmt.Errorf("failed to authorize: %w", err) |
| 642 | } |
| 643 | |
| 644 | client := &http.Client{} |
| 645 | if r.host.Client != nil { |
| 646 | *client = *r.host.Client |
| 647 | } |
| 648 | if client.CheckRedirect == nil { |
| 649 | client.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 650 | if len(via) >= 10 { |
| 651 | return errors.New("stopped after 10 redirects") |
| 652 | } |
| 653 | if err := r.authorize(ctx, req); err != nil { |
| 654 | return fmt.Errorf("failed to authorize redirect: %w", err) |
| 655 | } |
| 656 | return nil |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | tracing.UpdateHTTPClient(client, tracing.Name("remotes.docker.resolver", "HTTPRequest")) |
| 661 | |
| 662 | resp, err := client.Do(req) |
| 663 | if err != nil { |
| 664 | return nil, fmt.Errorf("failed to do request: %w", err) |
| 665 | } |
| 666 | log.G(ctx).WithFields(responseFields(resp)).Debug("fetch response received") |
| 667 | return resp, nil |
| 668 | } |
| 669 | |
| 670 | type doChecks func(r *request, resp *http.Response) error |
| 671 |
no test coverage detected