Skip to content

Commit 433f8cd

Browse files
committed
net/http: http2 conn serve's net.Conn type assertion supports non *tls.Conn types
1 parent 585c438 commit 433f8cd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/net/http/h2_bundle.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/net/http/server.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,14 @@ func isCommonNetReadError(err error) bool {
18261826
return false
18271827
}
18281828

1829+
type tlsConn interface {
1830+
net.Conn
1831+
ConnectionState() tls.ConnectionState
1832+
HandshakeContext(ctx context.Context) error
1833+
}
1834+
1835+
var _ tlsConn = &tls.Conn{}
1836+
18291837
// Serve a new connection.
18301838
func (c *conn) serve(ctx context.Context) {
18311839
c.remoteAddr = c.rwc.RemoteAddr().String()
@@ -1851,7 +1859,7 @@ func (c *conn) serve(ctx context.Context) {
18511859
}
18521860
}()
18531861

1854-
if tlsConn, ok := c.rwc.(*tls.Conn); ok {
1862+
if tlsConn, ok := c.rwc.(tlsConn); ok {
18551863
tlsTO := c.server.tlsHandshakeTimeout()
18561864
if tlsTO > 0 {
18571865
dl := time.Now().Add(tlsTO)
@@ -1884,7 +1892,11 @@ func (c *conn) serve(ctx context.Context) {
18841892
// from being run on these connections. This prevents closeIdleConns from
18851893
// closing such connections. See issue https://golang.org/issue/39776.
18861894
c.setState(c.rwc, StateActive, skipHooks)
1887-
fn(c.server, tlsConn, h)
1895+
if realTLSConn, ok := c.rwc.(*tls.Conn); ok {
1896+
fn(c.server, realTLSConn, h)
1897+
} else if proto == http2NextProtoTLS {
1898+
c.server.h2ProtoHandler(c.server, tlsConn, h)
1899+
}
18881900
}
18891901
return
18901902
}
@@ -2691,6 +2703,8 @@ type Server struct {
26912703
onShutdown []func()
26922704

26932705
listenerGroup sync.WaitGroup
2706+
2707+
h2ProtoHandler func(hs *Server, c tlsConn, h Handler)
26942708
}
26952709

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

0 commit comments

Comments
 (0)