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

This commit is contained in:
Nick Craig-Wood 2020-04-23 15:24:21 +01:00
parent b502a74cff
commit f6346a4d29
4 changed files with 28 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)