-
-
Save eahydra/5329924 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"crypto/tls" | |
"net" | |
"net/http" | |
"time" | |
"fmt" | |
"errors" | |
) | |
func redirectPolicy(req *http.Request, via []*http.Request) error { | |
if len(via) >= 3 { | |
return errors.New("stopped after 3 redirects") | |
} | |
return nil | |
} | |
func main() { | |
transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
Dial: func(netw, addr string) (net.Conn, error) { | |
// we want to wait a maximum of 1.75 seconds... | |
// since we're specifying a 1 second connect timeout and deadline | |
// (read/write timeout) is specified in absolute time we want to | |
// calculate that time first (before connecting) | |
deadline := time.Now().Add(800 * time.Millisecond) | |
c, err := net.DialTimeout(netw, addr, time.Second) | |
if err != nil { | |
return nil, err | |
} | |
c.SetDeadline(deadline) | |
return c, nil | |
}} | |
httpclient := &http.Client{Transport: transport, CheckRedirect: redirectPolicy} | |
req, err := http.NewRequest("GET", "http://bukk.it/boss.gif", nil) | |
req.Header.Add("User-Agent", "Bitly ImgGtr - Saving your bandwith and our time") | |
resp, err := httpclient.Do(req) | |
if err != nil { | |
return | |
} | |
start := time.Now().UnixNano() | |
resp.Body.Close() | |
fmt.Println("Close took: ", time.Now().UnixNano() - start) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment