From 809776b7ce0425050c92ae7843b328a213047dab Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 29 Apr 2015 22:20:31 -0400 Subject: [PATCH] dump responses --- history.go | 16 +++++++++------- main.go | 14 +++++++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/history.go b/history.go index c0cbf6f..242d83d 100644 --- a/history.go +++ b/history.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "time" ) var lastId int @@ -49,13 +50,14 @@ func setupDB() { sql := ` create table if not exists domains ( hostname text primary key, - blocked integer not null default 0 + blocked integer not null default 0 ); create table if not exists requests ( - id text primary key, - host text not null, - path text not null + id text primary key, + host text not null, + path text not null, + duration integer -- time taken in milliseconds ); ` res, err := db.Exec(sql) @@ -77,9 +79,9 @@ func saveHostname(hostname string) { } } -func saveRequest(id RequestId, r *http.Request) { - _, err := db.Exec(`insert or ignore into requests (id, host, path) - values (?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path) +func saveRequest(id RequestId, r *http.Request, elapsed time.Duration) { + _, err := db.Exec(`insert or ignore into requests (id, host, path, duration) + values (?, ?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path, elapsed.Nanoseconds()/1000000) if err != nil { log.Printf("unable to save request: %v", err) return diff --git a/main.go b/main.go index a5d2447..639183d 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "net/http/httputil" "os" "strings" + "time" "unicode" ) @@ -20,6 +21,7 @@ var ( func httpHandler(w http.ResponseWriter, r *http.Request) { id := newRequestId() + fmt.Printf("%s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", id.String()) fmt.Println("from:", r.RemoteAddr) if err := freezeRequest(r); err != nil { fmt.Printf("error freezing request: %s\n", err) @@ -36,6 +38,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { r.RequestURI = "" r.URL.Scheme = strings.Map(unicode.ToLower, r.URL.Scheme) + start := time.Now() res, err := client.Do(r) if err != nil { fmt.Printf("error forwarding request: %s\n", err) @@ -43,6 +46,13 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { } defer res.Body.Close() + fmt.Printf("%s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", id.String()) + resb, err := httputil.DumpResponse(res, false) + if err != nil { + fmt.Printf("fuuuuuuuuuck") + } + os.Stdout.Write(resb) + for k, v := range res.Header { w.Header()[k] = v } @@ -54,9 +64,11 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { if _, err := io.Copy(w, res.Body); err != nil { fmt.Printf("error copying body: %s\n", err) } + elapsed := time.Since(start) r.RequestURI = requestURI - saveRequest(id, r) + fmt.Printf("elapsed: %v (%v)\n", elapsed, elapsed.Nanoseconds()/1000000) + saveRequest(id, r, elapsed) } func bail(status int, t string, args ...interface{}) {