http.ReadRequest doesn't recognize http2?

84 views
Skip to first unread message

Ankit Gupta

unread,
May 21, 2018, 7:54:09 AM5/21/18
to golang-nuts
Hello gophers,

I am trying to read http2 request on a tcp connection, which is set up like this - 

                cert, err := tls.LoadX509KeyPair(certfile, certkeyfile)
                if err != nil {
                        return nil, err
                }
                tlsConfig := &tls.Config{
                        Certificates: []tls.Certificate{cert},
                        ServerName:   "mysrvr",
                        NextProtos:   []string{"h2", "http/1.1", "http/1.0"},
                        Time:         time.Now,
                        Rand:         rand.Reader,
                }
                tlsConfig.BuildNameToCertificate()
                tlsConfig.PreferServerCipherSuites = true
                listener := tls.Listen("tcp", ":"+sqp.ListenerPort, tlsConfig)
                
                for {
                       conn, err := listener.Accept()
                       reader := bufio.NewReader(conn)
                       req, err := http.ReadRequest(reader) // problem here
                       // do other things with req
                 }

When I do **curl https://127.0.0.1:8000 -k --http2** to the above server, I was expecting http.ReadRequest to parse the request same as it does for http/1.1 or 1.0.

What I saw was that the request contained PRI method (which is defined in http2 rfc) and even if I ignore the complete connection preface and the SETTINGS frame after it, I still don't get the original request. Though if I try reading directly from tcp connection, I get a blob right after PRI and SETTINGS frame. 

I looked at ParseHTTPVersion (https://golang.org/src/net/http/request.go?s=29537:29588#L708) called from readRequest and it doesn't even have a case for HTTP/2. Does it mean I can't use http.ReadRequest for HTTP/2 requests?

Ankit Gupta

unread,
May 21, 2018, 1:38:03 PM5/21/18
to golang-nuts
This issue is now opened https://go-review.googlesource.com/c/113817/

I am still looking for an alternative though - to know if a certain sequence of bytes contains http2 data with method/headers/body.
Reply all
Reply to author
Forward
0 new messages