Raw http2 with TLS

137 views
Skip to first unread message

Ian Gudger

unread,
Aug 11, 2020, 2:07:03 PM8/11/20
to golang-nuts
I am trying to use the http2.Server directly. I can't use the standard library http.Server because it has a hard dependency on receiving a *tls.Conn for http2 and I would like to use a wrapper. For now though, I am trying to get http2.Server working with the bare tls types. I am using what seems like the simplest way to use it, but it doesn't work and I am having trouble figuring out why. It seems that my handler is never executed and my browser returns ERR_CONNECTION_CLOSED.

Usage that doesn't work:
l, err := net.Listen("tcp", ":8443")
if err != nil {
    panic(err)
}
l = tls.NewListener(l, &tls.Config{NextProtos: []string{http2.NextProtoTLS}, Certificates: []tls.Certificate{rootTLSCert}})
var srv http2.Server
opts := http2.ServeConnOpts{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello world"))
})}
for {
    c, err := l.Accept()
    // TODO: Handle temporary errors.
    if err != nil {
        panic(err)
    }
    go srv.ServeConn(c, &opts)
}


Ian Gudger

unread,
Aug 12, 2020, 6:32:23 AM8/12/20
to golang-nuts
I figured it out. ServeConn expects Handshake to have been called on the tls.Conn.

It is unclear to me what an appropriate fix would be. Maybe just update the documentation for http2.(*Server).ServeConn?
Reply all
Reply to author
Forward
0 new messages