Created
January 10, 2017 09:24
-
-
Save theanalyst/3e6edc4f14fe70131724b7f54063ceae to your computer and use it in GitHub Desktop.
http abs uri with tracing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "net/http/httptrace" | |
| "net/http/httputil" | |
| ) | |
| type transport struct { | |
| current *http.Request | |
| } | |
| func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { | |
| t.current = req | |
| return http.DefaultTransport.RoundTrip(req) | |
| } | |
| func (t *transport) GotConn(info httptrace.GotConnInfo) { | |
| fmt.Printf("Connection reused for %v? %v\n", t.current.URL, info.Reused) | |
| } | |
| func (t *transport) ConnectStart(network, addr string) { | |
| fmt.Printf("Dial Start %v %v\n", network, addr) | |
| } | |
| func (t *transport) GotFirstResponseByte() { | |
| fmt.Println("First response byte got") | |
| } | |
| func (t *transport) WroteHeaders() { | |
| fmt.Println("WroteHeaders") | |
| } | |
| func (t *transport) WroteRequest(wr httptrace.WroteRequestInfo) { | |
| fmt.Println("Wrote Request", wr) | |
| } | |
| func main() { | |
| t := &transport{} | |
| req, _ := http.NewRequest("GET", "http://localhost:8000", nil) | |
| req.URL.Opaque = "//localhost:8000" | |
| trace := &httptrace.ClientTrace{ | |
| GotConn: t.GotConn, | |
| ConnectStart: t.ConnectStart, | |
| GotFirstResponseByte: t.GotFirstResponseByte, | |
| WroteHeaders: t.WroteHeaders, | |
| WroteRequest: t.WroteRequest, | |
| } | |
| req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) | |
| client := &http.Client{Transport: t} | |
| requestDump, err := httputil.DumpRequest(req, true) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Println(string(requestDump)) | |
| //resp, err := http.DefaultClient.Do(req) | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println(resp) | |
| log.Println(resp.Proto) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment