2015-09-08 20:01:26 +00:00
|
|
|
// A logging http transport
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const separator = "------------------------------------------------------------"
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// LoggedTransport is an http transport which logs the traffic
|
|
|
|
type LoggedTransport struct {
|
2015-09-08 20:01:26 +00: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 17:47:16 +00:00
|
|
|
func NewLoggedTransport(transport http.RoundTripper, logBody bool) *LoggedTransport {
|
|
|
|
return &LoggedTransport{
|
2015-09-08 20:01:26 +00: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 17:47:16 +00:00
|
|
|
func (t *LoggedTransport) CancelRequest(req *http.Request) {
|
2015-09-08 20:01:26 +00:00
|
|
|
if wrapped, ok := t.wrapped.(interface {
|
|
|
|
CancelRequest(*http.Request)
|
|
|
|
}); ok {
|
|
|
|
log.Printf("CANCEL REQUEST %v", req)
|
|
|
|
wrapped.CancelRequest(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip implements the RoundTripper interface.
|
2015-09-22 17:47:16 +00:00
|
|
|
func (t *LoggedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
2015-09-08 20:01:26 +00:00
|
|
|
buf, _ := httputil.DumpRequest(req, t.logBody)
|
|
|
|
log.Println(separator)
|
|
|
|
log.Println("HTTP REQUEST")
|
|
|
|
log.Println(string(buf))
|
|
|
|
log.Println(separator)
|
|
|
|
resp, err = t.wrapped.RoundTrip(req)
|
|
|
|
buf, _ = httputil.DumpResponse(resp, t.logBody)
|
|
|
|
log.Println(separator)
|
|
|
|
log.Println("HTTP RESPONSE")
|
|
|
|
log.Println(string(buf))
|
|
|
|
log.Println(separator)
|
|
|
|
return resp, err
|
|
|
|
}
|