Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorsuch/639fbbd98366d4e2ca98 to your computer and use it in GitHub Desktop.
Save gorsuch/639fbbd98366d4e2ca98 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"crypto/tls"
"fmt"
"log"
"net/url"
"strconv"
"strings"
"time"
)
func main() {
u, err := url.Parse("https://www.heroku.com")
if err != nil {
log.Fatal(err)
}
ts := time.Now()
// TODO grab name lookup time
conn, err := tls.Dial("tcp", u.Host+":443", &tls.Config{})
if err != nil {
log.Fatal(err)
}
ttc := time.Now()
fmt.Fprintf(
conn,
"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",
u.RequestURI(),
u.Host)
b := make([]byte, 1)
_, err = conn.Read(b)
if err != nil {
log.Fatal(err)
}
ttfb := time.Now()
r := bufio.NewReader(conn)
// TODO read the status?
status, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}
log.Print(string(b) + status)
headers := make(map[string]string, 0)
for {
line, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}
cleanLine := strings.TrimSpace(line)
if cleanLine == "" {
// end of headers
break
}
parts := strings.SplitN(cleanLine, ": ", 2)
headers[parts[0]] = parts[1]
}
if val, ok := headers["Content-Length"]; ok {
contentLength, err := strconv.Atoi(val)
if err != nil {
log.Fatal(err)
}
n := 0
buf := make([]byte, contentLength)
for i := contentLength; i > 0; i = i - n {
n, err = r.Read(buf)
}
}
// close things down
conn.Close()
te := time.Now()
fmt.Println(ttc.Sub(ts))
fmt.Println(ttfb.Sub(ts))
fmt.Println(te.Sub(ts))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment