Skip to content

Instantly share code, notes, and snippets.

@mattfoster
Created February 16, 2017 08:44
Show Gist options
  • Select an option

  • Save mattfoster/04ffe9366a0dc333e20d1c1a1d11c8d4 to your computer and use it in GitHub Desktop.

Select an option

Save mattfoster/04ffe9366a0dc333e20d1c1a1d11c8d4 to your computer and use it in GitHub Desktop.

Revisions

  1. mattfoster created this gist Feb 16, 2017.
    49 changes: 49 additions & 0 deletions google.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    package main

    import (
    "crypto/tls"
    "fmt"
    "log"
    "net/http"
    "net/http/httptrace"
    )

    // transport is an http.RoundTripper that keeps track of the in-flight
    // request and implements hooks to report HTTP tracing events.
    type transport struct {
    current *http.Request
    }

    // RoundTrip wraps http.DefaultTransport.RoundTrip to keep track
    // of the current request.
    func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
    t.current = req
    return http.DefaultTransport.RoundTrip(req)
    }

    // GotConn prints whether the connection has been used previously
    // for the current request.
    func (t *transport) GotConn(info httptrace.GotConnInfo) {
    fmt.Printf("Connection reused for %v? %v\n", t.current.URL, info.Reused)
    c := info.Conn

    if cc, ok := c.(*tls.Conn); ok {
    log.Printf("%+v", cc)
    }

    }

    func main() {
    t := &transport{}

    req, _ := http.NewRequest("GET", "https://google.com", nil)
    trace := &httptrace.ClientTrace{
    GotConn: t.GotConn,
    }
    req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))

    client := &http.Client{Transport: t}
    if _, err := client.Do(req); err != nil {
    log.Fatal(err)
    }
    }