"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
Use http.NewRequestWithContext where possible after go1.13 minimum version
Nick Craig-Woodbcac8fdc8377
diff --git a/backend/drive/drive.go b/backend/drive/drive.goindex 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, optioif 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 NewRequestWithContextfs.OpenOptionAddHTTPHeaders(req.Header, options)if o.bytes == 0 {// Don't supply range requests for 0 length objects as they always faildiff --git a/backend/drive/upload.go b/backend/drive/upload.goindex 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 NewRequestWithContextgoogleapi.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 infunc (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 = reqSizetotalSize := "*"if rx.ContentLength >= 0 {diff --git a/backend/googlecloudstorage/googlecloudstorage.go b/backend/googlecloudstorage/googlecloudstorage.goindex 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 readfunc (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 NewRequestWithContextfs.FixRangeOption(options, o.bytes)fs.OpenOptionAddHTTPHeaders(req.Header, options)var res *http.Responsediff --git a/backend/http/http.go b/backend/http/http.goindex 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, ereturn 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 NewRequestWithContextaddHeaders(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 errorreturn 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 NewRequestWithContextf.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 NewRequestWithContexto.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 supportedfunc (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 headersfor k, v := range fs.OpenOptionHeaders(options) {diff --git a/backend/hubic/hubic.go b/backend/hubic/hubic.goindex 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 Fsfunc (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 NewRequestWithContextresp, err := f.client.Do(req)if err != nil {return errdiff --git a/backend/putio/fs.go b/backend/putio/fs.goindex 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 NewRequestWithContextreq.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 NewRequestWithContextreq.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 NewRequestWithContextreq.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.goindex 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.Readvar resp *http.Responseheaders := 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 NewRequestWithContextreq.Header.Set("User-Agent", o.fs.client.UserAgent)// merge headers with extra headersdiff --git a/backend/s3/s3.go b/backend/s3/s3.goindex 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 lengthhttpReq.Header = headersdiff --git a/backend/webdav/odrvcookie/fetch.go b/backend/webdav/odrvcookie/fetch.goindex 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 NewRequestWithContextclient := fshttp.NewClient(ctx)resp, err := client.Do(req)diff --git a/cmd/rc/rc.go b/cmd/rc/rc.goindex 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, errreturn 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 NewRequestWithContextreq.Header.Set("Content-Type", "application/json")if authUser != "" || authPass != "" {diff --git a/lib/rest/rest.go b/lib/rest/rest.goindex 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, eif 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 NewRequestWithContextheaders := make(map[string]string)// Set default headersfor k, v := range api.headers {