dougaicha5258 2018-08-23 08:11
浏览 133
已采纳

Golang TCP服务器读取HTTP / 2帧

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
  • 写回答

1条回答 默认 最新

  • duanlan4801 2018-08-23 08:59
    关注

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

    "PRI * HTTP/2.0
    
    SM
    
    "
    

    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
    
    SM
    
    "
    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题