dump responses

master
Jordan Orelli 10 years ago
parent e240a4f5a8
commit 809776b7ce

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"time"
) )
var lastId int var lastId int
@ -55,7 +56,8 @@ func setupDB() {
create table if not exists requests ( create table if not exists requests (
id text primary key, id text primary key,
host text not null, host text not null,
path text not null path text not null,
duration integer -- time taken in milliseconds
); );
` `
res, err := db.Exec(sql) res, err := db.Exec(sql)
@ -77,9 +79,9 @@ func saveHostname(hostname string) {
} }
} }
func saveRequest(id RequestId, r *http.Request) { func saveRequest(id RequestId, r *http.Request, elapsed time.Duration) {
_, err := db.Exec(`insert or ignore into requests (id, host, path) _, err := db.Exec(`insert or ignore into requests (id, host, path, duration)
values (?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path) values (?, ?, ?, ?)`, id.String(), r.URL.Host, r.URL.Path, elapsed.Nanoseconds()/1000000)
if err != nil { if err != nil {
log.Printf("unable to save request: %v", err) log.Printf("unable to save request: %v", err)
return return

@ -10,6 +10,7 @@ import (
"net/http/httputil" "net/http/httputil"
"os" "os"
"strings" "strings"
"time"
"unicode" "unicode"
) )
@ -20,6 +21,7 @@ var (
func httpHandler(w http.ResponseWriter, r *http.Request) { func httpHandler(w http.ResponseWriter, r *http.Request) {
id := newRequestId() id := newRequestId()
fmt.Printf("%s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n", id.String())
fmt.Println("from:", r.RemoteAddr) fmt.Println("from:", r.RemoteAddr)
if err := freezeRequest(r); err != nil { if err := freezeRequest(r); err != nil {
fmt.Printf("error freezing request: %s\n", err) fmt.Printf("error freezing request: %s\n", err)
@ -36,6 +38,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
r.RequestURI = "" r.RequestURI = ""
r.URL.Scheme = strings.Map(unicode.ToLower, r.URL.Scheme) r.URL.Scheme = strings.Map(unicode.ToLower, r.URL.Scheme)
start := time.Now()
res, err := client.Do(r) res, err := client.Do(r)
if err != nil { if err != nil {
fmt.Printf("error forwarding request: %s\n", err) fmt.Printf("error forwarding request: %s\n", err)
@ -43,6 +46,13 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
} }
defer res.Body.Close() 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 { for k, v := range res.Header {
w.Header()[k] = v 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 { if _, err := io.Copy(w, res.Body); err != nil {
fmt.Printf("error copying body: %s\n", err) fmt.Printf("error copying body: %s\n", err)
} }
elapsed := time.Since(start)
r.RequestURI = requestURI 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{}) { func bail(status int, t string, args ...interface{}) {

Loading…
Cancel
Save