I think its one of the "Next big Things" on the interweb (the last change to http was 18! years ago)
So i skip SPDY and go directly for HTTP/2 - this post is about how to get a first grip and test it serverside as a go html server example.
Test preparations clientside:
- Get a browser that has http/2 support enabled by default: Chrome Canary
- Download the http2 and spdy indicator addon for chrome
- Test your browsers http2 support at: https://http2.akamai.com/demo
- Cd into your GOPATH/src
- Run go get golang.org/x/net/http2
- Check if the package has been downloaded
- Create a new go project, name it eg. testHttp2Server
- Create a ssl certificate (localhost.cert and localhost.key)
You can ether generate a self signed cert: selfsignedcertificate
or use mine: testHttp2Server
This two files must be in the same directory as the server binary. - Code for http2Server.go:
package mainimport (
"fmt"
"golang.org/x/net/http2"
"html"
"log"
"net/http"
)
func main() {
var srv http.Server
http2.VerboseLogs = true
srv.Addr = ":8080"
// This enables http2 support
http2.ConfigureServer(&srv, nil)
// Plain text test handler
// Open https://localhost:8080/randomtest
// in your Chrome Canary browser
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi tester %q\n", html.EscapeString(r.URL.Path))
ShowRequestInfoHandler(w, r)
})
// Listen as https ssl server
// NOTE: WITHOUT SSL IT WONT WORK!!
// To self generate a test ssl cert/key you could go to
// http://www.selfsignedcertificate.com/
// or read the openssl manual
log.Fatal(srv.ListenAndServeTLS("localhost.cert", "localhost.key"))
}
func ShowRequestInfoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Method: %s\n", r.Method)
fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
fmt.Fprintf(w, "Host: %s\n", r.Host)
fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
fmt.Fprintf(w, "URL: %#v\n", r.URL)
fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
fmt.Fprintf(w, "\nHeaders:\n")
r.Header.Write(w)
}
- Compile: go build http2Server.go
- Run the http2Server binary
Test it:
- Open in Chrome Canary: https://localhost:8080/kim
- Accept the certificate security error, continue to localhost
- Check if Protocol is HTTP/2.0, example output should be like:
Hello tester "/kim" Method: GET Protocol: HTTP/2.0 Host: localhost:8080
...
NOTE: http/2 in go ONLY WORKS WITH SSL/TLS! (currently)
I not sure why, but i did not got the server to support http/2 without TLS!
This could be a clientside or serverside issue - i dont know currently.
Some discussions about this:
why-wouldnt-it-be-great-if-http-2-would-only-allow-communication-via-tls
daniel.haxx.se tls-in-http2
arstechnica http2-finished-coming-to-browsers-within-weeks
As far as i have understood it, the spec says it "should be" possible to use it without the need to buy SSL certificates:
https://http2.github.io/http2-spec/
rfc7540 - html2