mirror of
https://github.com/rclone/rclone.git
synced 2024-12-23 23:49:15 +01:00
fs: Allow the http Transport to have an optional filter request function
This commit is contained in:
parent
729e1305b7
commit
fdb01437d8
12
fs/http.go
12
fs/http.go
@ -146,7 +146,8 @@ func (ci *ConfigInfo) Client() *http.Client {
|
|||||||
// * Does logging
|
// * Does logging
|
||||||
type Transport struct {
|
type Transport struct {
|
||||||
*http.Transport
|
*http.Transport
|
||||||
dump DumpFlags
|
dump DumpFlags
|
||||||
|
filterRequest func(req *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransport wraps the http.Transport passed in and logs all
|
// NewTransport wraps the http.Transport passed in and logs all
|
||||||
@ -158,6 +159,11 @@ func NewTransport(transport *http.Transport, dump DumpFlags) *Transport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetRequestFilter sets a filter to be used on each request
|
||||||
|
func (t *Transport) SetRequestFilter(f func(req *http.Request)) {
|
||||||
|
t.filterRequest = f
|
||||||
|
}
|
||||||
|
|
||||||
// A mutex to protect this map
|
// A mutex to protect this map
|
||||||
var checkedHostMu sync.RWMutex
|
var checkedHostMu sync.RWMutex
|
||||||
|
|
||||||
@ -249,6 +255,10 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
|
|||||||
}
|
}
|
||||||
// Force user agent
|
// Force user agent
|
||||||
req.Header.Set("User-Agent", *userAgent)
|
req.Header.Set("User-Agent", *userAgent)
|
||||||
|
// Filter the request if required
|
||||||
|
if t.filterRequest != nil {
|
||||||
|
t.filterRequest(req)
|
||||||
|
}
|
||||||
// Logf request
|
// Logf request
|
||||||
if t.dump&(DumpHeaders|DumpBodies|DumpAuth|DumpRequests|DumpResponses) != 0 {
|
if t.dump&(DumpHeaders|DumpBodies|DumpAuth|DumpRequests|DumpResponses) != 0 {
|
||||||
buf, _ := httputil.DumpRequestOut(req, t.dump&(DumpBodies|DumpRequests) != 0)
|
buf, _ := httputil.DumpRequestOut(req, t.dump&(DumpBodies|DumpRequests) != 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user