2015-09-08 22:01:26 +02:00
|
|
|
// A logging http transport
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
)
|
|
|
|
|
2015-10-03 16:39:31 +02:00
|
|
|
const (
|
|
|
|
separatorReq = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
|
|
|
separatorResp = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
|
|
|
)
|
2015-09-08 22:01:26 +02:00
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// LoggedTransport is an http transport which logs the traffic
|
|
|
|
type LoggedTransport struct {
|
2015-09-08 22:01:26 +02:00
|
|
|
wrapped http.RoundTripper
|
|
|
|
logBody bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoggedTransport wraps the transport passed in and logs all roundtrips
|
|
|
|
// including the body if logBody is set.
|
2015-09-22 19:47:16 +02:00
|
|
|
func NewLoggedTransport(transport http.RoundTripper, logBody bool) *LoggedTransport {
|
|
|
|
return &LoggedTransport{
|
2015-09-08 22:01:26 +02:00
|
|
|
wrapped: transport,
|
|
|
|
logBody: logBody,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelRequest cancels an in-flight request by closing its
|
|
|
|
// connection. CancelRequest should only be called after RoundTrip has
|
|
|
|
// returned.
|
2015-09-22 19:47:16 +02:00
|
|
|
func (t *LoggedTransport) CancelRequest(req *http.Request) {
|
2015-09-08 22:01:26 +02:00
|
|
|
if wrapped, ok := t.wrapped.(interface {
|
|
|
|
CancelRequest(*http.Request)
|
|
|
|
}); ok {
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "CANCEL REQUEST %v", req)
|
2015-09-08 22:01:26 +02:00
|
|
|
wrapped.CancelRequest(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip implements the RoundTripper interface.
|
2015-09-22 19:47:16 +02:00
|
|
|
func (t *LoggedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
2015-10-03 16:39:31 +02:00
|
|
|
buf, _ := httputil.DumpRequestOut(req, t.logBody)
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "%s", separatorReq)
|
|
|
|
Debug(nil, "%s", "HTTP REQUEST")
|
|
|
|
Debug(nil, "%s", string(buf))
|
|
|
|
Debug(nil, "%s", separatorReq)
|
2015-09-08 22:01:26 +02:00
|
|
|
resp, err = t.wrapped.RoundTrip(req)
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "%s", separatorResp)
|
|
|
|
Debug(nil, "%s", "HTTP RESPONSE")
|
2015-11-26 17:52:54 +01:00
|
|
|
if err != nil {
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "Error: %v", err)
|
2015-11-26 17:52:54 +01:00
|
|
|
} else {
|
|
|
|
buf, _ = httputil.DumpResponse(resp, t.logBody)
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "%s", string(buf))
|
2015-11-26 17:52:54 +01:00
|
|
|
}
|
2016-06-06 22:23:54 +02:00
|
|
|
Debug(nil, "%s", separatorResp)
|
2015-09-08 22:01:26 +02:00
|
|
|
return resp, err
|
|
|
|
}
|