From bcac8fdc8377af57e2f29bc30b5626052ceb3981 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 3 Feb 2021 17:41:27 +0000 Subject: [PATCH] Use http.NewRequestWithContext where possible after go1.13 minimum version --- backend/drive/drive.go | 3 +-- backend/drive/upload.go | 6 ++---- backend/googlecloudstorage/googlecloudstorage.go | 3 +-- backend/http/http.go | 12 ++++-------- backend/hubic/hubic.go | 3 +-- backend/putio/fs.go | 9 +++------ backend/putio/object.go | 3 +-- backend/s3/s3.go | 3 +-- backend/webdav/odrvcookie/fetch.go | 3 +-- cmd/rc/rc.go | 3 +-- lib/rest/rest.go | 3 +-- 11 files changed, 17 insertions(+), 34 deletions(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index 43e43a986..cbd920634 100755 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -3427,11 +3427,10 @@ func (o *baseObject) httpResponse(ctx context.Context, url, method string, optio if url == "" { return nil, nil, errors.New("forbidden to download - check sharing permission") } - req, err = http.NewRequest(method, url, nil) + req, err = http.NewRequestWithContext(ctx, method, url, nil) if err != nil { return req, nil, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext fs.OpenOptionAddHTTPHeaders(req.Header, options) if o.bytes == 0 { // Don't supply range requests for 0 length objects as they always fail diff --git a/backend/drive/upload.go b/backend/drive/upload.go index 559aa155a..14c7baa64 100644 --- a/backend/drive/upload.go +++ b/backend/drive/upload.go @@ -77,11 +77,10 @@ func (f *Fs) Upload(ctx context.Context, in io.Reader, size int64, contentType, return false, err } var req *http.Request - req, err = http.NewRequest(method, urls, body) + req, err = http.NewRequestWithContext(ctx, method, urls, body) if err != nil { return false, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext googleapi.Expand(req.URL, map[string]string{ "fileId": fileID, }) @@ -114,8 +113,7 @@ func (f *Fs) Upload(ctx context.Context, in io.Reader, size int64, contentType, // Make an http.Request for the range passed in func (rx *resumableUpload) makeRequest(ctx context.Context, start int64, body io.ReadSeeker, reqSize int64) *http.Request { - req, _ := http.NewRequest("POST", rx.URI, body) - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext + req, _ := http.NewRequestWithContext(ctx, "POST", rx.URI, body) req.ContentLength = reqSize totalSize := "*" if rx.ContentLength >= 0 { diff --git a/backend/googlecloudstorage/googlecloudstorage.go b/backend/googlecloudstorage/googlecloudstorage.go index 1739e52d0..ffcde7258 100644 --- a/backend/googlecloudstorage/googlecloudstorage.go +++ b/backend/googlecloudstorage/googlecloudstorage.go @@ -1028,11 +1028,10 @@ func (o *Object) Storable() bool { // Open an object for read func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) { - req, err := http.NewRequest("GET", o.url, nil) + req, err := http.NewRequestWithContext(ctx, "GET", o.url, nil) if err != nil { return nil, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext fs.FixRangeOption(options, o.bytes) fs.OpenOptionAddHTTPHeaders(req.Header, options) var res *http.Response diff --git a/backend/http/http.go b/backend/http/http.go index 0bac2317b..a5c367402 100644 --- a/backend/http/http.go +++ b/backend/http/http.go @@ -183,9 +183,8 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e return http.ErrUseLastResponse } // check to see if points to a file - req, err := http.NewRequest("HEAD", u.String(), nil) + req, err := http.NewRequestWithContext(ctx, "HEAD", u.String(), nil) if err == nil { - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext addHeaders(req, opt) res, err := noRedir.Do(req) err = statusError(res, err) @@ -391,11 +390,10 @@ func (f *Fs) readDir(ctx context.Context, dir string) (names []string, err error return nil, errors.Errorf("internal error: readDir URL %q didn't end in /", URL) } // Do the request - req, err := http.NewRequest("GET", URL, nil) + req, err := http.NewRequestWithContext(ctx, "GET", URL, nil) if err != nil { return nil, errors.Wrap(err, "readDir failed") } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext f.addHeaders(req) res, err := f.httpClient.Do(req) if err == nil { @@ -547,11 +545,10 @@ func (o *Object) stat(ctx context.Context) error { return nil } url := o.url() - req, err := http.NewRequest("HEAD", url, nil) + req, err := http.NewRequestWithContext(ctx, "HEAD", url, nil) if err != nil { return errors.Wrap(err, "stat failed") } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext o.fs.addHeaders(req) res, err := o.fs.httpClient.Do(req) if err == nil && res.StatusCode == http.StatusNotFound { @@ -596,11 +593,10 @@ func (o *Object) Storable() bool { // Open a remote http file object for reading. Seek is supported func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) { url := o.url() - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, errors.Wrap(err, "Open failed") } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext // Add optional headers for k, v := range fs.OpenOptionHeaders(options) { diff --git a/backend/hubic/hubic.go b/backend/hubic/hubic.go index e2ae8ee63..62dd0be06 100644 --- a/backend/hubic/hubic.go +++ b/backend/hubic/hubic.go @@ -110,11 +110,10 @@ func (f *Fs) String() string { // // The credentials are read into the Fs func (f *Fs) getCredentials(ctx context.Context) (err error) { - req, err := http.NewRequest("GET", "https://api.hubic.com/1.0/account/credentials", nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://api.hubic.com/1.0/account/credentials", nil) if err != nil { return err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext resp, err := f.client.Do(req) if err != nil { return err diff --git a/backend/putio/fs.go b/backend/putio/fs.go index c97cf6a84..f0cf4827a 100644 --- a/backend/putio/fs.go +++ b/backend/putio/fs.go @@ -282,11 +282,10 @@ func (f *Fs) PutUnchecked(ctx context.Context, in io.Reader, src fs.ObjectInfo, func (f *Fs) createUpload(ctx context.Context, name string, size int64, parentID string, modTime time.Time, options []fs.OpenOption) (location string, err error) { // defer log.Trace(f, "name=%v, size=%v, parentID=%v, modTime=%v", name, size, parentID, modTime.String())("location=%v, err=%v", location, &err) err = f.pacer.Call(func() (bool, error) { - req, err := http.NewRequest("POST", "https://upload.put.io/files/", nil) + req, err := http.NewRequestWithContext(ctx, "POST", "https://upload.put.io/files/", nil) if err != nil { return false, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext req.Header.Set("tus-resumable", "1.0.0") req.Header.Set("upload-length", strconv.FormatInt(size, 10)) b64name := base64.StdEncoding.EncodeToString([]byte(f.opt.Enc.FromStandardName(name))) @@ -428,21 +427,19 @@ func (f *Fs) transferChunk(ctx context.Context, location string, start int64, ch } func (f *Fs) makeUploadHeadRequest(ctx context.Context, location string) (*http.Request, error) { - req, err := http.NewRequest("HEAD", location, nil) + req, err := http.NewRequestWithContext(ctx, "HEAD", location, nil) if err != nil { return nil, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext req.Header.Set("tus-resumable", "1.0.0") return req, nil } func (f *Fs) makeUploadPatchRequest(ctx context.Context, location string, in io.Reader, offset, length int64) (*http.Request, error) { - req, err := http.NewRequest("PATCH", location, in) + req, err := http.NewRequestWithContext(ctx, "PATCH", location, in) if err != nil { return nil, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext req.Header.Set("tus-resumable", "1.0.0") req.Header.Set("upload-offset", strconv.FormatInt(offset, 10)) req.Header.Set("content-length", strconv.FormatInt(length, 10)) diff --git a/backend/putio/object.go b/backend/putio/object.go index 16de2930c..f653ffb0b 100644 --- a/backend/putio/object.go +++ b/backend/putio/object.go @@ -229,11 +229,10 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read var resp *http.Response headers := fs.OpenOptionHeaders(options) err = o.fs.pacer.Call(func() (bool, error) { - req, err := http.NewRequest(http.MethodGet, storageURL, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, storageURL, nil) if err != nil { return shouldRetry(err) } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext req.Header.Set("User-Agent", o.fs.client.UserAgent) // merge headers with extra headers diff --git a/backend/s3/s3.go b/backend/s3/s3.go index 2aec71d25..392c30a6b 100644 --- a/backend/s3/s3.go +++ b/backend/s3/s3.go @@ -3298,11 +3298,10 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op } // create the vanilla http request - httpReq, err := http.NewRequest("PUT", url, in) + httpReq, err := http.NewRequestWithContext(ctx, "PUT", url, in) if err != nil { return errors.Wrap(err, "s3 upload: new request") } - httpReq = httpReq.WithContext(ctx) // go1.13 can use NewRequestWithContext // set the headers we signed and the length httpReq.Header = headers diff --git a/backend/webdav/odrvcookie/fetch.go b/backend/webdav/odrvcookie/fetch.go index 2f0584a56..ececd07c5 100644 --- a/backend/webdav/odrvcookie/fetch.go +++ b/backend/webdav/odrvcookie/fetch.go @@ -176,11 +176,10 @@ func (ca *CookieAuth) getSPToken(ctx context.Context) (conf *SharepointSuccessRe // Create and execute the first request which returns an auth token for the sharepoint service // With this token we can authenticate on the login page and save the returned cookies - req, err := http.NewRequest("POST", "https://login.microsoftonline.com/extSTS.srf", buf) + req, err := http.NewRequestWithContext(ctx, "POST", "https://login.microsoftonline.com/extSTS.srf", buf) if err != nil { return nil, err } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext client := fshttp.NewClient(ctx) resp, err := client.Do(req) diff --git a/cmd/rc/rc.go b/cmd/rc/rc.go index c03ad13a1..2f95098b4 100644 --- a/cmd/rc/rc.go +++ b/cmd/rc/rc.go @@ -184,11 +184,10 @@ func doCall(ctx context.Context, path string, in rc.Params) (out rc.Params, err return nil, errors.Wrap(err, "failed to encode JSON") } - req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(data)) if err != nil { return nil, errors.Wrap(err, "failed to make request") } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext req.Header.Set("Content-Type", "application/json") if authUser != "" || authPass != "" { diff --git a/lib/rest/rest.go b/lib/rest/rest.go index e6e0e6850..e72275a73 100644 --- a/lib/rest/rest.go +++ b/lib/rest/rest.go @@ -213,11 +213,10 @@ func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, e if opts.ContentLength != nil && *opts.ContentLength == 0 { body = nil } - req, err := http.NewRequest(opts.Method, url, body) + req, err := http.NewRequestWithContext(ctx, opts.Method, url, body) if err != nil { return } - req = req.WithContext(ctx) // go1.13 can use NewRequestWithContext headers := make(map[string]string) // Set default headers for k, v := range api.headers {