Skip to content

Commit 83ab333

Browse files
committed
net/http: use tlsConn interface
1 parent e32255f commit 83ab333

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/net/http/server.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ func (c *conn) serve(ctx context.Context) {
19571957
}
19581958
}()
19591959

1960-
if tlsConn, ok := c.rwc.(*tls.Conn); ok {
1960+
if tlsConn, ok := c.rwc.(tlsConn); ok {
19611961
tlsTO := c.server.tlsHandshakeTimeout()
19621962
if tlsTO > 0 {
19631963
dl := time.Now().Add(tlsTO)
@@ -1987,13 +1987,20 @@ func (c *conn) serve(ctx context.Context) {
19871987
c.tlsState = new(tls.ConnectionState)
19881988
*c.tlsState = tlsConn.ConnectionState()
19891989
if proto := c.tlsState.NegotiatedProtocol; validNextProto(proto) {
1990+
// New HTTP/2 Handler at https://go-review.googlesource.com/c/go/+/616097/2/src/net/http/server.go
1991+
1992+
tc, ok := tlsConn.(*tls.Conn)
1993+
if !ok {
1994+
return
1995+
}
1996+
19901997
if fn := c.server.TLSNextProto[proto]; fn != nil {
1991-
h := initALPNRequest{ctx, tlsConn, serverHandler{c.server}}
1998+
h := initALPNRequest{ctx, tc, serverHandler{c.server}}
19921999
// Mark freshly created HTTP/2 as active and prevent any server state hooks
19932000
// from being run on these connections. This prevents closeIdleConns from
19942001
// closing such connections. See issue https://golang.org/issue/39776.
19952002
c.setState(c.rwc, StateActive, skipHooks)
1996-
fn(c.server, tlsConn, h)
2003+
fn(c.server, tc, h)
19972004
}
19982005
return
19992006
}

src/net/http/transport.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,11 @@ func (pconn *persistConn) addTLS(ctx context.Context, name string, trace *httptr
17271727
return nil
17281728
}
17291729

1730+
type tlsConn interface {
1731+
HandshakeContext(ctx context.Context) error
1732+
ConnectionState() tls.ConnectionState
1733+
}
1734+
17301735
type erringRoundTripper interface {
17311736
RoundTripErr() error
17321737
}
@@ -1757,7 +1762,7 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
17571762
if err != nil {
17581763
return nil, wrapErr(err)
17591764
}
1760-
if tc, ok := pconn.conn.(*tls.Conn); ok {
1765+
if tc, ok := pconn.conn.(tlsConn); ok {
17611766
// Handshake here, in case DialTLS didn't. TLSNextProto below
17621767
// depends on it for knowing the connection state.
17631768
if trace != nil && trace.TLSHandshakeStart != nil {
@@ -1928,13 +1933,17 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
19281933
}
19291934

19301935
if s := pconn.tlsState; s != nil && s.NegotiatedProtocolIsMutual && s.NegotiatedProtocol != "" {
1931-
if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok {
1932-
alt := next(cm.targetAddr, pconn.conn.(*tls.Conn))
1933-
if e, ok := alt.(erringRoundTripper); ok {
1934-
// pconn.conn was closed by next (http2configureTransports.upgradeFn).
1935-
return nil, e.RoundTripErr()
1936+
// New HTTP/2 Handler at https://go-review.googlesource.com/c/go/+/616097/2/src/net/http/transport.go
1937+
1938+
if tc, ok := pconn.conn.(*tls.Conn); ok {
1939+
if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok {
1940+
alt := next(cm.targetAddr, tc)
1941+
if e, ok := alt.(erringRoundTripper); ok {
1942+
// pconn.conn was closed by next (http2configureTransports.upgradeFn).
1943+
return nil, e.RoundTripErr()
1944+
}
1945+
return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil
19361946
}
1937-
return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil
19381947
}
19391948
}
19401949

0 commit comments

Comments
 (0)