Skip to content

Instantly share code, notes, and snippets.

@eahydra
Forked from mynameisfiber/gist:2853066
Created April 7, 2013 10:27
Show Gist options
  • Save eahydra/5329924 to your computer and use it in GitHub Desktop.
Save eahydra/5329924 to your computer and use it in GitHub Desktop.
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