Skip to content

net/http: use tlsConn interface #74676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ func (c *conn) serve(ctx context.Context) {
}
}()

if tlsConn, ok := c.rwc.(*tls.Conn); ok {
if tlsConn, ok := c.rwc.(tlsConn); ok {
tlsTO := c.server.tlsHandshakeTimeout()
if tlsTO > 0 {
dl := time.Now().Add(tlsTO)
Expand Down Expand Up @@ -1987,13 +1987,20 @@ func (c *conn) serve(ctx context.Context) {
c.tlsState = new(tls.ConnectionState)
*c.tlsState = tlsConn.ConnectionState()
if proto := c.tlsState.NegotiatedProtocol; validNextProto(proto) {
// New HTTP/2 Handler at https://go-review.googlesource.com/c/go/+/616097/2/src/net/http/server.go

tc, ok := tlsConn.(*tls.Conn)
if !ok {
return
}

if fn := c.server.TLSNextProto[proto]; fn != nil {
h := initALPNRequest{ctx, tlsConn, serverHandler{c.server}}
h := initALPNRequest{ctx, tc, serverHandler{c.server}}
// Mark freshly created HTTP/2 as active and prevent any server state hooks
// from being run on these connections. This prevents closeIdleConns from
// closing such connections. See issue https://golang.org/issue/39776.
c.setState(c.rwc, StateActive, skipHooks)
fn(c.server, tlsConn, h)
fn(c.server, tc, h)
}
return
}
Expand Down
23 changes: 16 additions & 7 deletions src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,11 @@ func (pconn *persistConn) addTLS(ctx context.Context, name string, trace *httptr
return nil
}

type tlsConn interface {
HandshakeContext(ctx context.Context) error
ConnectionState() tls.ConnectionState
}

type erringRoundTripper interface {
RoundTripErr() error
}
Expand Down Expand Up @@ -1757,7 +1762,7 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
if err != nil {
return nil, wrapErr(err)
}
if tc, ok := pconn.conn.(*tls.Conn); ok {
if tc, ok := pconn.conn.(tlsConn); ok {
// Handshake here, in case DialTLS didn't. TLSNextProto below
// depends on it for knowing the connection state.
if trace != nil && trace.TLSHandshakeStart != nil {
Expand Down Expand Up @@ -1928,13 +1933,17 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
}

if s := pconn.tlsState; s != nil && s.NegotiatedProtocolIsMutual && s.NegotiatedProtocol != "" {
if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok {
alt := next(cm.targetAddr, pconn.conn.(*tls.Conn))
if e, ok := alt.(erringRoundTripper); ok {
// pconn.conn was closed by next (http2configureTransports.upgradeFn).
return nil, e.RoundTripErr()
// New HTTP/2 Handler at https://go-review.googlesource.com/c/go/+/616097/2/src/net/http/transport.go

if tc, ok := pconn.conn.(*tls.Conn); ok {
if next, ok := t.TLSNextProto[s.NegotiatedProtocol]; ok {
alt := next(cm.targetAddr, tc)
if e, ok := alt.(erringRoundTripper); ok {
// pconn.conn was closed by next (http2configureTransports.upgradeFn).
return nil, e.RoundTripErr()
}
return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil
}
return &persistConn{t: t, cacheKey: pconn.cacheKey, alt: alt}, nil
}
}

Expand Down