camget, httputil: more verbose HTTP debugging

Change-Id: I2fc1a1fe0fff99202245d391a8a76e9fab07adc2
This commit is contained in:
Brad Fitzpatrick 2013-01-05 16:44:34 -08:00
parent 6f9f0bdda9
commit 9f6776dddb
2 changed files with 24 additions and 2 deletions

View File

@ -43,6 +43,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -105,6 +106,14 @@ func main() {
httpStats := &httputil.StatsTransport{ httpStats := &httputil.StatsTransport{
VerboseLog: *flagHTTP, VerboseLog: *flagHTTP,
} }
if *flagHTTP {
httpStats.Transport = &http.Transport{
Dial: func(net_, addr string) (net.Conn, error) {
log.Printf("Dialing %s", addr)
return net.Dial(net_, addr)
},
}
}
cl.SetHTTPClient(&http.Client{Transport: httpStats}) cl.SetHTTPClient(&http.Client{Transport: httpStats})
// Put a local disk cache in front of the HTTP client. // Put a local disk cache in front of the HTTP client.

View File

@ -20,6 +20,7 @@ import (
"log" "log"
"net/http" "net/http"
"sync" "sync"
"time"
) )
// StatsTransport wraps another RoundTripper (or uses the default one) and // StatsTransport wraps another RoundTripper (or uses the default one) and
@ -45,14 +46,26 @@ func (t *StatsTransport) Requests() int {
func (t *StatsTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { func (t *StatsTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
t.mu.Lock() t.mu.Lock()
t.reqs++ t.reqs++
n := t.reqs
t.mu.Unlock() t.mu.Unlock()
rt := t.Transport rt := t.Transport
if rt == nil { if rt == nil {
rt = http.DefaultTransport rt = http.DefaultTransport
} }
var t0 time.Time
if t.VerboseLog { if t.VerboseLog {
log.Printf("%s %s", req.Method, req.URL) t0 = time.Now()
log.Printf("(%d) %s %s ...", n, req.Method, req.URL)
} }
return rt.RoundTrip(req) resp, err = rt.RoundTrip(req)
if t.VerboseLog {
td := time.Since(t0)
if err == nil {
log.Printf("(%d) %s %s = status %d (in %v)", n, req.Method, req.URL, resp.StatusCode, td)
} else {
log.Printf("(%d) %s %s = error: %v (in %v)", n, req.Method, req.URL, err, td)
}
}
return
} }