Skip to content

Commit 94e7200

Browse files
namusyakabradfitz
authored andcommitted
net/http: introduce DialerAndTLSConfigSupportsHTTP2 in Transport
Even when a custom TLS config or custom dialer is specified, enables HTTP/2 if DialerAndTLSConfigSupportsHTTP2 is true. By this change, avoid automatically enabling HTTP/2 if DialContext is set. This change also ensures that DefaultTransport still automatically enable HTTP/2 as discussed in #14391. Updates #14391 Fixes #27011 Change-Id: Icc46416810bee61dbd65ebc96468335030b80573 Reviewed-on: https://go-review.googlesource.com/c/go/+/130256 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> Run-TryBot: Kunpei Sakai <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 1eed2a5 commit 94e7200

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/net/http/transport.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ var DefaultTransport RoundTripper = &Transport{
4646
KeepAlive: 30 * time.Second,
4747
DualStack: true,
4848
}).DialContext,
49-
MaxIdleConns: 100,
50-
IdleConnTimeout: 90 * time.Second,
51-
TLSHandshakeTimeout: 10 * time.Second,
52-
ExpectContinueTimeout: 1 * time.Second,
49+
DialerAndTLSConfigSupportsHTTP2: true,
50+
MaxIdleConns: 100,
51+
IdleConnTimeout: 90 * time.Second,
52+
TLSHandshakeTimeout: 10 * time.Second,
53+
ExpectContinueTimeout: 1 * time.Second,
5354
}
5455

5556
// DefaultMaxIdleConnsPerHost is the default value of Transport's
@@ -257,6 +258,12 @@ type Transport struct {
257258
// h2transport (via onceSetNextProtoDefaults)
258259
nextProtoOnce sync.Once
259260
h2transport h2Transport // non-nil if http2 wired up
261+
262+
// DialerAndTLSConfigSupportsHTTP2 controls whether HTTP/2 is enabled when a non-zero
263+
// TLSClientConfig or Dial, DialTLS or DialContext func is provided. By default, use of any those fields conservatively
264+
// disables HTTP/2. To use a customer dialer or TLS config and still attempt HTTP/2
265+
// upgrades, set this to true.
266+
DialerAndTLSConfigSupportsHTTP2 bool
260267
}
261268

262269
// h2Transport is the interface we expect to be able to call from
@@ -296,12 +303,13 @@ func (t *Transport) onceSetNextProtoDefaults() {
296303
// Transport.
297304
return
298305
}
299-
if t.TLSClientConfig != nil || t.Dial != nil || t.DialTLS != nil {
306+
if !t.DialerAndTLSConfigSupportsHTTP2 && (t.TLSClientConfig != nil || t.Dial != nil || t.DialTLS != nil || t.DialContext != nil) {
300307
// Be conservative and don't automatically enable
301308
// http2 if they've specified a custom TLS config or
302309
// custom dialers. Let them opt-in themselves via
303310
// http2.ConfigureTransport so we don't surprise them
304311
// by modifying their tls.Config. Issue 14275.
312+
// However, if DialerAndTLSConfigSupportsHTTP2 is true, it overrides the above checks.
305313
return
306314
}
307315
t2, err := http2configureTransport(t)

src/net/http/transport_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,13 @@ func TestTransportAutomaticHTTP2(t *testing.T) {
35933593
testTransportAutoHTTP(t, &Transport{}, true)
35943594
}
35953595

3596+
func TestTransportAutomaticHTTP2_DialerAndTLSConfigSupportsHTTP2AndTLSConfig(t *testing.T) {
3597+
testTransportAutoHTTP(t, &Transport{
3598+
DialerAndTLSConfigSupportsHTTP2: true,
3599+
TLSClientConfig: new(tls.Config),
3600+
}, true)
3601+
}
3602+
35963603
// golang.org/issue/14391: also check DefaultTransport
35973604
func TestTransportAutomaticHTTP2_DefaultTransport(t *testing.T) {
35983605
testTransportAutoHTTP(t, DefaultTransport.(*Transport), true)
@@ -3623,6 +3630,13 @@ func TestTransportAutomaticHTTP2_Dial(t *testing.T) {
36233630
}, false)
36243631
}
36253632

3633+
func TestTransportAutomaticHTTP2_DialContext(t *testing.T) {
3634+
var d net.Dialer
3635+
testTransportAutoHTTP(t, &Transport{
3636+
DialContext: d.DialContext,
3637+
}, false)
3638+
}
3639+
36263640
func TestTransportAutomaticHTTP2_DialTLS(t *testing.T) {
36273641
testTransportAutoHTTP(t, &Transport{
36283642
DialTLS: func(network, addr string) (net.Conn, error) {

0 commit comments

Comments
 (0)