From 898613e14fa7875d1e64bd1a66297f8efdc98343 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 1 May 2017 15:31:57 +0200 Subject: [PATCH] debug: Add RoundTripper --- src/restic/debug/round_tripper_debug.go | 43 +++++++++++++++++++++++ src/restic/debug/round_tripper_release.go | 11 ++++++ 2 files changed, 54 insertions(+) create mode 100644 src/restic/debug/round_tripper_debug.go create mode 100644 src/restic/debug/round_tripper_release.go diff --git a/src/restic/debug/round_tripper_debug.go b/src/restic/debug/round_tripper_debug.go new file mode 100644 index 000000000..4ca686b38 --- /dev/null +++ b/src/restic/debug/round_tripper_debug.go @@ -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 +} diff --git a/src/restic/debug/round_tripper_release.go b/src/restic/debug/round_tripper_release.go new file mode 100644 index 000000000..6efff2c28 --- /dev/null +++ b/src/restic/debug/round_tripper_release.go @@ -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 +}