]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: support disabling built-in HTTP/2 with a new build tag
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Nov 2019 04:00:29 +0000 (04:00 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Nov 2019 23:16:09 +0000 (23:16 +0000)
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>
src/net/http/clientserver_test.go
src/net/http/export_test.go
src/net/http/h2_bundle.go
src/net/http/http.go
src/net/http/http_test.go
src/net/http/main_test.go
src/net/http/omithttp2.go [new file with mode: 0644]
src/net/http/serve_test.go
src/net/http/server.go
src/net/http/transport.go
src/net/http/transport_test.go

index c3877d70712b7ab509b648edccbf2b6b1f894c60..def5c424f0e8e45a5aaeb92964cdea5c6b946774 100644 (file)
@@ -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,
index e5c06a890325647e777e8b4cbf9485b449691bb2..657ff9dba490228e69896bb447b6fbf7ae35e0ea 100644 (file)
@@ -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)
index ad00f0611b628182e95d5e745b113430ff6b4274..a583a0d6cb7754471a63d1b01ba7ead9b2777b5c 100644 (file)
@@ -1,5 +1,7 @@
+// +build !nethttpomithttp2
+
 // Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
-//go:generate bundle -o h2_bundle.go -prefix http2 golang.org/x/net/http2
+//   $ bundle -o=h2_bundle.go -prefix=http2 -tags=!nethttpomithttp2 golang.org/x/net/http2
 
 // Package http2 implements the HTTP/2 protocol.
 //
index 3510fe604d091a220004df33d5f8e27dd5c39105..89e86d80e8e97cac3e214991ce1191f3668e81ee 100644 (file)
@@ -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 (
@@ -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.
 
index 224b46c7963e2605a8ff8f4015361dfa17d1ab3b..f4ea52db3bc518b0e8cb936415f9d8f08288acb2 100644 (file)
@@ -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) {
index 85aa9096c31ceaac739f1c1bd220f6c2d085f756..35cc80977c4780149dbda735dc07a4e9531c794e 100644 (file)
@@ -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()
        }
diff --git a/src/net/http/omithttp2.go b/src/net/http/omithttp2.go
new file mode 100644 (file)
index 0000000..a0b33e9
--- /dev/null
@@ -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" }
index 4c53c95edad18ae8ccfd149890d05b8ecad36a53..af43421fce938d629915b25d7d7532a79ca3e5fa 100644 (file)
@@ -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)
@@ -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)
index b2c071fc2108cc4a5ba752eb3a6ac6fae30004ec..4f1c73dbdfc6656fcf8ce5fa4a18585f0d3f0e0a 100644 (file)
@@ -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
index 8989f65f2580d34f36cc1ad10ec9140cab38c048..6fade795ab64f13601d37ecb3cd26b024507ca27 100644 (file)
@@ -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,
@@ -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 {
@@ -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)
index 00d6b2608b8b15bb6eabca3f96cf1afa59ae469e..692868094c11234e3a2228aa61409f1588796a1e 100644 (file)
@@ -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"))
@@ -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")
@@ -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"))