Skip to content

Commit 1d4ff48

Browse files
Dreamacroneild
authored andcommitted
http2: add DialTLSContext to Transport
Fixes golang/go#52114 Change-Id: Ibc0ba6b401bb39ea89f88796d55fbf781596140e GitHub-Last-Rev: 488673b GitHub-Pull-Request: #123 Reviewed-on: https://go-review.googlesource.com/c/net/+/377114 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
1 parent 13a9a73 commit 1d4ff48

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

‎http2/transport.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,23 @@ const (
6767
// A Transport internally caches connections to servers. It is safe
6868
// for concurrent use by multiple goroutines.
6969
type Transport struct {
70-
// DialTLS specifies an optional dial function for creating
71-
// TLS connections for requests.
70+
// DialTLSContext specifies an optional dial function with context for
71+
// creating TLS connections for requests.
7272
//
73-
// If DialTLS is nil, tls.Dial is used.
73+
// If DialTLSContext and DialTLS is nil, tls.Dial is used.
7474
//
7575
// If the returned net.Conn has a ConnectionState method like tls.Conn,
7676
// it will be used to set http.Response.TLS.
77+
DialTLSContext func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
78+
79+
// DialTLS specifies an optional dial function for creating
80+
// TLS connections for requests.
81+
//
82+
// If DialTLSContext and DialTLS is nil, tls.Dial is used.
83+
//
84+
// Deprecated: Use DialTLSContext instead, which allows the transport
85+
// to cancel dials as soon as they are no longer needed.
86+
// If both are set, DialTLSContext takes priority.
7787
DialTLS func(network, addr string, cfg *tls.Config) (net.Conn, error)
7888

7989
// TLSClientConfig specifies the TLS configuration to use with
@@ -592,7 +602,7 @@ func (t *Transport) dialClientConn(ctx context.Context, addr string, singleUse b
592602
if err != nil {
593603
return nil, err
594604
}
595-
tconn, err := t.dialTLS(ctx)("tcp", addr, t.newTLSConfig(host))
605+
tconn, err := t.dialTLS(ctx, "tcp", addr, t.newTLSConfig(host))
596606
if err != nil {
597607
return nil, err
598608
}
@@ -613,24 +623,25 @@ func (t *Transport) newTLSConfig(host string) *tls.Config {
613623
return cfg
614624
}
615625

616-
func (t *Transport) dialTLS(ctx context.Context) func(string, string, *tls.Config) (net.Conn, error) {
617-
if t.DialTLS != nil {
618-
return t.DialTLS
626+
func (t *Transport) dialTLS(ctx context.Context, network, addr string, tlsCfg *tls.Config) (net.Conn, error) {
627+
if t.DialTLSContext != nil {
628+
return t.DialTLSContext(ctx, network, addr, tlsCfg)
629+
} else if t.DialTLS != nil {
630+
return t.DialTLS(network, addr, tlsCfg)
619631
}
620-
return func(network, addr string, cfg *tls.Config) (net.Conn, error) {
621-
tlsCn, err := t.dialTLSWithContext(ctx, network, addr, cfg)
622-
if err != nil {
623-
return nil, err
624-
}
625-
state := tlsCn.ConnectionState()
626-
if p := state.NegotiatedProtocol; p != NextProtoTLS {
627-
return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
628-
}
629-
if !state.NegotiatedProtocolIsMutual {
630-
return nil, errors.New("http2: could not negotiate protocol mutually")
631-
}
632-
return tlsCn, nil
632+
633+
tlsCn, err := t.dialTLSWithContext(ctx, network, addr, tlsCfg)
634+
if err != nil {
635+
return nil, err
636+
}
637+
state := tlsCn.ConnectionState()
638+
if p := state.NegotiatedProtocol; p != NextProtoTLS {
639+
return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", p, NextProtoTLS)
640+
}
641+
if !state.NegotiatedProtocolIsMutual {
642+
return nil, errors.New("http2: could not negotiate protocol mutually")
633643
}
644+
return tlsCn, nil
634645
}
635646

636647
// disableKeepAlives reports whether connections should be closed as

0 commit comments

Comments
 (0)