Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,35 @@ func (d *Downloader) unmarshalRepoPackages(ctx context.Context, p, cacheDir stri
return d.unmarshalRepoPackagesHTTP(ctx, p, cf)
}

// NewRequest returns an http.Request, adding auth headers if the URL is
// prefixed with "oauth-".
func (d *Downloader) NewRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
u, useOauth := strings.CutPrefix(url, "oauth-")
req, err := http.NewRequestWithContext(ctx, method, u, body)
if err != nil {
return nil, err
}
if useOauth {
creds, err := google.FindDefaultCredentials(ctx)
if err != nil {
return nil, fmt.Errorf("failed to obtain creds: %v", err)
}
token, err := creds.TokenSource.Token()
if err != nil {
return nil, fmt.Errorf("failed to obtain access token: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
}
return req, nil
}

// CanResume returns true if we can resume downloading the specified url.
func (d *Downloader) CanResume(url string) (bool, int64, error) {
resp, err := d.HTTPClient.Head(url)
func (d *Downloader) CanResume(ctx context.Context, url string) (bool, int64, error) {
req, err := d.NewRequest(ctx, http.MethodHead, url, nil)
if err != nil {
return false, 0, err
}
resp, err := d.HTTPClient.Do(req)
if err != nil {
return false, 0, err
}
Expand All @@ -220,23 +246,10 @@ func (d *Downloader) CanResume(url string) (bool, int64, error) {

// Get gets a url using an optional proxy server, retrying once on any error.
func (d *Downloader) Get(ctx context.Context, path string) (*http.Response, error) {
useOauth := strings.HasPrefix(path, "oauth-")
path = strings.TrimPrefix(path, "oauth-")
req, err := http.NewRequest(http.MethodGet, path, nil)
req, err := d.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
if useOauth {
creds, err := google.FindDefaultCredentials(ctx)
if err != nil {
return nil, fmt.Errorf("failed to obtain creds: %v", err)
}
token, err := creds.TokenSource.Token()
if err != nil {
return nil, fmt.Errorf("failed to obtain access token: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
}
resp, err := d.HTTPClient.Do(req)
// We retry on any error once as this mitigates some
// connection issues in certain situations.
Expand Down
6 changes: 3 additions & 3 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"strings"

"cloud.google.com/go/storage"
humanize "github.com/dustin/go-humanize"
"github.com/dustin/go-humanize"
"github.com/google/googet/v2/client"
"github.com/google/googet/v2/goolib"
"github.com/google/googet/v2/oswrap"
Expand Down Expand Up @@ -76,11 +76,11 @@ func packageHTTP(ctx context.Context, url, dst, chksum string, downloader *clien
// Check that the server supports ranged requests and that the
// existing file is smaller than what we want to download.
logger.Infof("existing file size: %d", size)
ok, length, err := downloader.CanResume(url)
ok, length, err := downloader.CanResume(ctx, url)
if err != nil {
logger.Errorf("CanResume: %v", err)
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := downloader.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
Expand Down
Loading