fs: Allow the http Transport to have an optional filter request function

This commit is contained in:
Nick Craig-Wood 2017-11-24 09:07:56 +00:00
parent 729e1305b7
commit fdb01437d8

View File

@ -146,7 +146,8 @@ func (ci *ConfigInfo) Client() *http.Client {
// * Does logging
type Transport struct {
*http.Transport
dump DumpFlags
dump DumpFlags
filterRequest func(req *http.Request)
}
// 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
var checkedHostMu sync.RWMutex
@ -249,6 +255,10 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
}
// Force user agent
req.Header.Set("User-Agent", *userAgent)
// Filter the request if required
if t.filterRequest != nil {
t.filterRequest(req)
}
// Logf request
if t.dump&(DumpHeaders|DumpBodies|DumpAuth|DumpRequests|DumpResponses) != 0 {
buf, _ := httputil.DumpRequestOut(req, t.dump&(DumpBodies|DumpRequests) != 0)