Skip to content

net/http: http2 conn serve's net.Conn type assertion supports non *tls.Conn types #55077

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
6 changes: 4 additions & 2 deletions src/net/http/h2_bundle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,14 @@ func isCommonNetReadError(err error) bool {
return false
}

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

var _ tlsConn = &tls.Conn{}

// Serve a new connection.
func (c *conn) serve(ctx context.Context) {
c.remoteAddr = c.rwc.RemoteAddr().String()
Expand All @@ -1851,7 +1859,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 @@ -1884,7 +1892,11 @@ func (c *conn) serve(ctx context.Context) {
// 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)
if realTLSConn, ok := c.rwc.(*tls.Conn); ok {
fn(c.server, realTLSConn, h)
} else if proto == http2NextProtoTLS {
c.server.h2ProtoHandler(c.server, tlsConn, h)
}
}
return
}
Expand Down Expand Up @@ -2691,6 +2703,8 @@ type Server struct {
onShutdown []func()

listenerGroup sync.WaitGroup

h2ProtoHandler func(hs *Server, c tlsConn, h Handler)
}

// Close immediately closes all active net.Listeners and any
Expand Down Expand Up @@ -3502,7 +3516,7 @@ func (globalOptionsHandler) ServeHTTP(w ResponseWriter, r *Request) {
// Requests come from ALPN protocol handlers.
type initALPNRequest struct {
ctx context.Context
c *tls.Conn
c tlsConn
h serverHandler
}

Expand Down