mirror of
				https://github.com/rclone/rclone.git
				synced 2025-10-30 23:17:59 +02:00 
			
		
		
		
	fs: Allow the http Transport to have an optional filter request function
This commit is contained in:
		
							
								
								
									
										12
									
								
								fs/http.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								fs/http.go
									
									
									
									
									
								
							| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user