Skip to content

Commit

Permalink
net/http: support disabling built-in HTTP/2 with a new build tag
Browse files Browse the repository at this point in the history
Fixes #35082
Updates #6853

Change-Id: I4eeb0e15f534cff57fefb6039cd33fadf15b946e
Reviewed-on: https://go-review.googlesource.com/c/go/+/205139
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
  • Loading branch information
bradfitz committed Nov 4, 2019
1 parent 74af7fc commit 2566e21
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/net/http/clientserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
}

func newClientServerTest(t *testing.T, h2 bool, h Handler, opts ...interface{}) *clientServerTest {
if h2 {
CondSkipHTTP2(t)
}
cst := &clientServerTest{
t: t,
h2: h2,
Expand Down
6 changes: 6 additions & 0 deletions src/net/http/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func init() {
}
}

func CondSkipHTTP2(t *testing.T) {
if omitBundledHTTP2 {
t.Skip("skipping HTTP/2 test when nethttpomithttp2 build tag in use")
}
}

var (
SetEnterRoundTripHook = hookSetter(&testHookEnterRoundTrip)
SetRoundTripRetried = hookSetter(&testHookRoundTripRetried)
Expand Down
4 changes: 3 additions & 1 deletion src/net/http/h2_bundle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/net/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:generate bundle -o=h2_bundle.go -prefix=http2 -tags=!nethttpomithttp2 golang.org/x/net/http2

package http

import (
Expand All @@ -22,6 +24,11 @@ const maxInt64 = 1<<63 - 1
// immediate cancellation of network operations.
var aLongTimeAgo = time.Unix(1, 0)

// omitBundledHTTP2 is set by omithttp2.go when the nethttpomithttp2
// build tag is set. That means h2_bundle.go isn't compiled in and we
// shouldn't try to use it.
var omitBundledHTTP2 bool

// TODO(bradfitz): move common stuff here. The other files have accumulated
// generic http stuff in random places.

Expand Down
26 changes: 26 additions & 0 deletions src/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ func TestCmdGoNoHTTPServer(t *testing.T) {
}
}

// Tests that the nethttpomithttp2 build tag doesn't rot too much,
// even if there's not a regular builder on it.
func TestOmitHTTP2(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
t.Parallel()
goTool := testenv.GoToolPath(t)
out, err := exec.Command(goTool, "test", "-short", "-tags=nethttpomithttp2", "net/http").CombinedOutput()
if err != nil {
t.Fatalf("go test -short failed: %v, %s", err, out)
}
}

// Tests that the nethttpomithttp2 build tag at least type checks
// in short mode.
// The TestOmitHTTP2 test above actually runs tests (in long mode).
func TestOmitHTTP2Vet(t *testing.T) {
t.Parallel()
goTool := testenv.GoToolPath(t)
out, err := exec.Command(goTool, "vet", "-tags=nethttpomithttp2", "net/http").CombinedOutput()
if err != nil {
t.Fatalf("go vet failed: %v, %s", err, out)
}
}

var valuesCount int

func BenchmarkCopyValues(b *testing.B) {
Expand Down
3 changes: 3 additions & 0 deletions src/net/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func goroutineLeaked() bool {
// (all.bash), but as a serial test otherwise. Using t.Parallel isn't
// compatible with the afterTest func in non-short mode.
func setParallel(t *testing.T) {
if strings.Contains(t.Name(), "HTTP2") {
http.CondSkipHTTP2(t)
}
if testing.Short() {
t.Parallel()
}
Expand Down
71 changes: 71 additions & 0 deletions src/net/http/omithttp2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build nethttpomithttp2

package http

import (
"errors"
"sync"
"time"
)

func init() {
omitBundledHTTP2 = true
}

const noHTTP2 = "no bundled HTTP/2" // should never see this

var http2errRequestCanceled = errors.New("net/http: request canceled")

var http2goAwayTimeout = 1 * time.Second

const http2NextProtoTLS = "h2"

type http2Transport struct {
MaxHeaderListSize uint32
ConnPool interface{}
}

func (*http2Transport) RoundTrip(*Request) (*Response, error) { panic(noHTTP2) }
func (*http2Transport) CloseIdleConnections() {}

type http2erringRoundTripper struct{}

func (http2erringRoundTripper) RoundTrip(*Request) (*Response, error) { panic(noHTTP2) }

type http2noDialClientConnPool struct {
http2clientConnPool http2clientConnPool
}

type http2clientConnPool struct {
mu *sync.Mutex
conns map[string][]struct{}
}

func http2configureTransport(*Transport) (*http2Transport, error) { panic(noHTTP2) }

func http2isNoCachedConnError(err error) bool {
_, ok := err.(interface{ IsHTTP2NoCachedConnError() })
return ok
}

type http2Server struct {
NewWriteScheduler func() http2WriteScheduler
}

type http2WriteScheduler interface{}

func http2NewPriorityWriteScheduler(interface{}) http2WriteScheduler { panic(noHTTP2) }

func http2ConfigureServer(s *Server, conf *http2Server) error { panic(noHTTP2) }

var http2ErrNoCachedConn = http2noCachedConnError{}

type http2noCachedConnError struct{}

func (http2noCachedConnError) IsHTTP2NoCachedConnError() {}

func (http2noCachedConnError) Error() string { return "http2: no cached connection was available" }
2 changes: 2 additions & 0 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,7 @@ func TestTLSServer(t *testing.T) {
}

func TestServeTLS(t *testing.T) {
CondSkipHTTP2(t)
// Not parallel: uses global test hooks.
defer afterTest(t)
defer SetTestHookServerServe(nil)
Expand Down Expand Up @@ -1657,6 +1658,7 @@ func TestAutomaticHTTP2_ListenAndServe_GetCertificate(t *testing.T) {
}

func testAutomaticHTTP2_ListenAndServe(t *testing.T, tlsConf *tls.Config) {
CondSkipHTTP2(t)
// Not parallel: uses global test hooks.
defer afterTest(t)
defer SetTestHookServerServe(nil)
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@ func (srv *Server) onceSetNextProtoDefaults_Serve() {
// configured otherwise. (by setting srv.TLSNextProto non-nil)
// It must only be called via srv.nextProtoOnce (use srv.setupHTTP2_*).
func (srv *Server) onceSetNextProtoDefaults() {
if strings.Contains(os.Getenv("GODEBUG"), "http2server=0") {
if omitBundledHTTP2 || strings.Contains(os.Getenv("GODEBUG"), "http2server=0") {
return
}
// Enable HTTP/2 by default if the user hasn't otherwise
Expand Down
7 changes: 6 additions & 1 deletion src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (t *Transport) Clone() *Transport {
DialContext: t.DialContext,
Dial: t.Dial,
DialTLS: t.DialTLS,
TLSClientConfig: t.TLSClientConfig.Clone(),
TLSHandshakeTimeout: t.TLSHandshakeTimeout,
DisableKeepAlives: t.DisableKeepAlives,
DisableCompression: t.DisableCompression,
Expand All @@ -302,6 +301,9 @@ func (t *Transport) Clone() *Transport {
WriteBufferSize: t.WriteBufferSize,
ReadBufferSize: t.ReadBufferSize,
}
if t.TLSClientConfig != nil {
t2.TLSClientConfig = t.TLSClientConfig.Clone()
}
if !t.tlsNextProtoWasNil {
npm := map[string]func(authority string, c *tls.Conn) RoundTripper{}
for k, v := range t.TLSNextProto {
Expand Down Expand Up @@ -359,6 +361,9 @@ func (t *Transport) onceSetNextProtoDefaults() {
// However, if ForceAttemptHTTP2 is true, it overrides the above checks.
return
}
if omitBundledHTTP2 {
return
}
t2, err := http2configureTransport(t)
if err != nil {
log.Printf("Error enabling Transport HTTP/2 support: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions src/net/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ func TestTransportMaxConnsPerHostIncludeDialInProgress(t *testing.T) {

func TestTransportMaxConnsPerHost(t *testing.T) {
defer afterTest(t)
CondSkipHTTP2(t)

h := HandlerFunc(func(w ResponseWriter, r *Request) {
_, err := w.Write([]byte("foo"))
Expand Down Expand Up @@ -3994,6 +3995,7 @@ func TestTransportAutomaticHTTP2_DialTLS(t *testing.T) {
}

func testTransportAutoHTTP(t *testing.T, tr *Transport, wantH2 bool) {
CondSkipHTTP2(t)
_, err := tr.RoundTrip(new(Request))
if err == nil {
t.Error("expected error from RoundTrip")
Expand Down Expand Up @@ -5896,6 +5898,7 @@ func TestDontCacheBrokenHTTP2Conn(t *testing.T) {
// only be one decrement regardless of the number of failures.
func TestTransportDecrementConnWhenIdleConnRemoved(t *testing.T) {
defer afterTest(t)
CondSkipHTTP2(t)

h := HandlerFunc(func(w ResponseWriter, r *Request) {
_, err := w.Write([]byte("foo"))
Expand Down

0 comments on commit 2566e21

Please sign in to comment.