Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Explicitly enable http/2 for Go 1.7+ due to changes made to address g…
Browse files Browse the repository at this point in the history
…o#5908.
  • Loading branch information
F21 committed Aug 29, 2016
1 parent 842f311 commit 6dd9b0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: go
sudo: false
go:
- 1.7
- 1.6.2
- 1.5.4
- 1.4.3
Expand Down
25 changes: 25 additions & 0 deletions graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func (srv *Server) ListenTLS(certFile, keyFile string) (net.Listener, error) {
return nil, err
}

// Enable http2
enableHTTP2ForTLSConfig(config)

conn, err := srv.newTCPListener(addr)
if err != nil {
return nil, err
Expand All @@ -191,6 +194,28 @@ func (srv *Server) ListenTLS(certFile, keyFile string) (net.Listener, error) {
return tlsListener, nil
}

// Enable HTTP2ForTLSConfig explicitly enables http/2 for a TLS Config. This is due to changes in Go 1.7 where
// http servers are no longer automatically configured to enable http/2 if the server's TLSConfig is set.
// See https://github.com/golang/go/issues/15908
func enableHTTP2ForTLSConfig(t *tls.Config) {

if TLSConfigHasHTTP2Enabled(t) {
return
}

t.NextProtos = append(t.NextProtos, "h2")
}

// TLSConfigHasHTTP2Enabled checks to see if a given TLS Config has http2 enabled.
func TLSConfigHasHTTP2Enabled(t *tls.Config) bool {
for _, value := range t.NextProtos {
if value == "h2" {
return true
}
}
return false
}

// ListenAndServeTLS is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled.
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
l, err := srv.ListenTLS(certFile, keyFile)
Expand Down
3 changes: 2 additions & 1 deletion http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func checkIfConnectionToServerIsHTTP2(t *testing.T, wg *sync.WaitGroup, c chan o
t.Fatalf("Error encountered while connecting to test server: %s", err)
}

if r.Proto != "HTTP/2.0" {
if !r.ProtoAtLeast(2, 0) {
t.Fatalf("Expected HTTP/2 connection to server, but connection was using %s", r.Proto)
}
}
Expand Down Expand Up @@ -108,6 +108,7 @@ func TestHTTP2ListenAndServeTLSConfig(t *testing.T) {

tlsConf := &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"h2"}, // We need to explicitly enable http/2 in Go 1.7+
}

tlsConf.BuildNameToCertificate()
Expand Down

0 comments on commit 6dd9b0d

Please sign in to comment.