This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
go-monero/pkg/http/dump_transport.go
Ciro S. Costa 45b42a1fae source formatting
Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
2021-07-18 17:22:57 -04:00

47 lines
1.1 KiB
Go

package http
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
)
// DumpTransport implements the `net/http.RoundTripper` interface wrapping
// another Roundtripper dumping to stdout both the requests and the responses
// that it sees passing through.
//
type DumpTransport struct {
R http.RoundTripper
}
// NewDumpTransport instantiates a new DumpTransport.
//
func NewDumpTransport(rt http.RoundTripper) *DumpTransport {
return &DumpTransport{
R: rt,
}
}
// RoundTrip implements the functionality of dumping http requests and
// responses to `stdout` for each HTTP transaction that passes through it.
//
// It does so by first dumping the request, then passing that down to the
// wrapped roundtripper, and then from the response it sees, dumping it too.
//
func (d *DumpTransport) RoundTrip(h *http.Request) (*http.Response, error) {
requestDump, _ := httputil.DumpRequestOut(h, true)
fmt.Fprintln(os.Stderr, string(requestDump))
resp, err := d.R.RoundTrip(h)
if err != nil {
return nil, err
}
responseDump, _ := httputil.DumpResponse(resp, true)
fmt.Fprintln(os.Stderr, string(responseDump))
return resp, err
}