mirror of
https://github.com/restic/restic.git
synced 2024-12-23 00:07:25 +00:00
debug: Add RoundTripper
This commit is contained in:
parent
b84e63d503
commit
898613e14f
2 changed files with 54 additions and 0 deletions
43
src/restic/debug/round_tripper_debug.go
Normal file
43
src/restic/debug/round_tripper_debug.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
// +build debug
|
||||
|
||||
package debug
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
)
|
||||
|
||||
type loggingRoundTripper struct {
|
||||
http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTripper returns a new http.RoundTripper which logs all requests (if
|
||||
// debug is enabled). When debug is not enabled, upstream is returned.
|
||||
func RoundTripper(upstream http.RoundTripper) http.RoundTripper {
|
||||
return loggingRoundTripper{upstream}
|
||||
}
|
||||
|
||||
func (tr loggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
||||
trace, err := httputil.DumpRequestOut(req, false)
|
||||
if err != nil {
|
||||
Log("DumpRequestOut() error: %v\n", err)
|
||||
} else {
|
||||
Log("------------ HTTP REQUEST -----------\n%s", trace)
|
||||
}
|
||||
|
||||
res, err = tr.RoundTripper.RoundTrip(req)
|
||||
if err != nil {
|
||||
Log("RoundTrip() returned error: %v", err)
|
||||
}
|
||||
|
||||
if res != nil {
|
||||
trace, err := httputil.DumpResponse(res, false)
|
||||
if err != nil {
|
||||
Log("DumpResponse() error: %v\n", err)
|
||||
} else {
|
||||
Log("------------ HTTP RESPONSE ----------\n%s", trace)
|
||||
}
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
11
src/restic/debug/round_tripper_release.go
Normal file
11
src/restic/debug/round_tripper_release.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
// +build !debug
|
||||
|
||||
package debug
|
||||
|
||||
import "net/http"
|
||||
|
||||
// RoundTripper returns a new http.RoundTripper which logs all requests (if
|
||||
// debug is enabled). When debug is not enabled, upstream is returned.
|
||||
func RoundTripper(upstream http.RoundTripper) http.RoundTripper {
|
||||
return upstream
|
||||
}
|
Loading…
Reference in a new issue