Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: add --header flag to add options to every HTTP transaction #59

Nick Craig-Woodf6346a4d2967
diff --git a/docs/content/docs.md b/docs/content/docs.md
index 838141f65..95517618a 100644
--- a/docs/content/docs.md
+++ b/docs/content/docs.md
@@ -564,6 +564,22 @@ triggering follow-on actions if data was copied, or skipping if not.
NB: Enabling this option turns a usually non-fatal error into a potentially
fatal one - please check and adjust your scripts accordingly!
+### --header ###
+
+Add an HTTP header for all transactions. The flag can be repeated to
+add multiple headers.
+
+If you want to add headers only for uploads use `--header-upload` and
+if you want to add headers only for downloads use `--header-download`.
+
+This flag is supported for all HTTP based backends even those not
+supported by `--header-upload` and `--header-download` so may be used
+as a workaround for those with care.
+
+```
+rclone ls remote:test --header "X-Rclone: Foo" --header "X-LetMeIn: Yes"
+```
+
### --header-download ###
Add an HTTP header for all download transactions. The flag can be repeated to
diff --git a/fs/config.go b/fs/config.go
index 555085c34..43f63ca03 100644
--- a/fs/config.go
+++ b/fs/config.go
@@ -113,6 +113,7 @@ type ConfigInfo struct {
OrderBy string // instructions on how to order the transfer
UploadHeaders []*HTTPOption
DownloadHeaders []*HTTPOption
+ Headers []*HTTPOption
}
// NewConfig creates a new config with everything set to the default
diff --git a/fs/config/configflags/configflags.go b/fs/config/configflags/configflags.go
index f06f83ec8..9233b5813 100644
--- a/fs/config/configflags/configflags.go
+++ b/fs/config/configflags/configflags.go
@@ -31,6 +31,7 @@ var (
disableFeatures string
uploadHeaders []string
downloadHeaders []string
+ headers []string
)
// AddFlags adds the non filing system specific flags to the command
@@ -116,6 +117,7 @@ func AddFlags(flagSet *pflag.FlagSet) {
flags.StringVarP(flagSet, &fs.Config.OrderBy, "order-by", "", fs.Config.OrderBy, "Instructions on how to order the transfers, eg 'size,descending'")
flags.StringArrayVarP(flagSet, &uploadHeaders, "header-upload", "", nil, "Set HTTP header for upload transactions")
flags.StringArrayVarP(flagSet, &downloadHeaders, "header-download", "", nil, "Set HTTP header for download transactions")
+ flags.StringArrayVarP(flagSet, &headers, "header", "", nil, "Set HTTP header for all transactions")
}
// ParseHeaders converts the strings passed in via the header flags into HTTPOptions
@@ -236,10 +238,12 @@ func SetFlags() {
if len(uploadHeaders) != 0 {
fs.Config.UploadHeaders = ParseHeaders(uploadHeaders)
}
-
if len(downloadHeaders) != 0 {
fs.Config.DownloadHeaders = ParseHeaders(downloadHeaders)
}
+ if len(headers) != 0 {
+ fs.Config.Headers = ParseHeaders(headers)
+ }
// Make the config file absolute
configPath, err := filepath.Abs(config.ConfigPath)
diff --git a/fs/fshttp/http.go b/fs/fshttp/http.go
index 8caef8a4f..c72444998 100644
--- a/fs/fshttp/http.go
+++ b/fs/fshttp/http.go
@@ -223,6 +223,7 @@ type Transport struct {
dump fs.DumpFlags
filterRequest func(req *http.Request)
userAgent string
+ headers []*fs.HTTPOption
}
// newTransport wraps the http.Transport passed in and logs all
@@ -232,6 +233,7 @@ func newTransport(ci *fs.ConfigInfo, transport *http.Transport) *Transport {
Transport: transport,
dump: ci.Dump,
userAgent: ci.UserAgent,
+ headers: ci.Headers,
}
}
@@ -331,6 +333,10 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
}
// Force user agent
req.Header.Set("User-Agent", t.userAgent)
+ // Set user defined headers
+ for _, option := range t.headers {
+ req.Header.Set(option.Key, option.Value)
+ }
// Filter the request if required
if t.filterRequest != nil {
t.filterRequest(req)