Created
May 26, 2016 17:40
-
-
Save kkirsche/0156e715993e9da6b1a567fc6362abae to your computer and use it in GitHub Desktop.
Golang URL Tracer
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 ( | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| // TransportWrapper wraps the http.Transport structure to allow us to record the | |
| // URLs which we are redirected through | |
| type TransportWrapper struct { | |
| *http.Transport | |
| } | |
| // RoundTrip executes a single HTTP transaction, returning | |
| // a Response for the provided Request. | |
| func (t *TransportWrapper) RoundTrip(req *http.Request) (*http.Response, error) { | |
| // Use the default transport we set | |
| transport := t.Transport | |
| if transport == nil { | |
| transport = http.DefaultTransport.(*http.Transport) | |
| } | |
| resp, err := transport.RoundTrip(req) | |
| if err != nil { | |
| return resp, err | |
| } | |
| // Record the redirects that we followed | |
| if (resp.StatusCode >= 300) && (resp.StatusCode <= 399) { | |
| log.Printf("Status: %d, Base URL: %s\n", resp.StatusCode, req.URL.Host) | |
| } | |
| return resp, err | |
| } | |
| func main() { | |
| log.SetPrefix("[URL Tracer] ") | |
| t := &TransportWrapper{ | |
| Transport: http.DefaultTransport.(*http.Transport), | |
| } | |
| client := &http.Client{ | |
| Transport: t, | |
| Timeout: 5 * time.Second, | |
| } | |
| _, err := client.Get("https://ge.service-now.com") | |
| if err != nil { | |
| log.Panicln(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment