Search code examples
gotcphttp2

Golang TCP server reading HTTP/2 frames


I'm trying to set up a TCP server which accepts and decodes HTTP/2 data. The code to actually parse the frames can be found in this article:

Decoding http2 frame header/data in Go

I'm having issues, however, with setting up the server. The connection gets accepted, but it hangs on framer.ReadFrame(). Here is a code example:

// generate with: openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.pem -days 365 -nodes
cert, err := tls.LoadX509KeyPair("server.pem", "server.key")
if err != nil {
    log.Fatal(err)
}

tlsCfg := &tls.Config{
    Certificates: []tls.Certificate{cert},
    NextProtos:   []string{"h2"},
}

l, err := tls.Listen("tcp", ":8787", tlsCfg)
if err != nil {
    log.Fatal(err)
}
defer l.Close()

conn, err := l.Accept()
if err != nil {
    log.Fatalln(err)
}
defer conn.Close()

framer := http2.NewFramer(conn, conn)
frame, _ := framer.ReadFrame() // Here it hangs

I request the server using curl

curl -v https://127.0.0.1:8787/ -k --http2

Solution

  • The conversation doesn't start with a frame. First the client sends the string

    "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
    

    You can tell by inspecting the bytes sent by the client:

    framer := http2.NewFramer(conn, io.TeeReader(conn, hex.Dumper(os.Stdout)))
    
    // 00000000  50 52 49 20 2a 20 48 54  54 50 2f 32 2e 30 0d 0a  |PRI * HTTP/2.0..|
    // 00000010  0d 0a 53 4d 0d 0a 0d 0a  00 00 12 04 00 00 00 00  |..SM............|
    // 00000020  00 00 03 00 00 00 64 00  04 40 00 00 00 00 02 00  |......d..@......|
    // 00000030  00 00 00 00 00 04 08 00  00 00 00 00 3f ff 00 01  |............?...|
    // 00000040  00 00 1e 01 05 00 00 00  01 82 84 87 41 8a a0 e4  |............A...|
    // 00000050  1d 13 9d 09 b8 f3 af 3b  7a 88 25 b6 50 c3 ab b6  |.......;z.%.P...|
    // 00000060  fa e0 53 03 2a 2f 2a ^C
    

    If you modify your code to read that string first it works as expected:

    conn, err := l.Accept()
    if err != nil {
        log.Fatalln(err)
    }   
    defer conn.Close()
    
    const preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
    b := make([]byte, len(preface))
    if _, err := io.ReadFull(conn, b); err != nil {
        log.Fatalln(err)
    }
    if string(b) != preface {
        log.Fatalln("invalid preface")
    }
    
    framer := http2.NewFramer(conn, conn)
    frame, err := framer.ReadFrame()
    fmt.Println(frame, err)
    
    // Output:
    // [FrameHeader SETTINGS len=18] <nil>
    

    If found these articles useful when playing with HTTP2 myself: