1 回答
TA贡献1868条经验 获得超4个赞
对话不是从框架开始的。首先客户端发送字符串
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
您可以通过检查客户端发送的字节来判断:
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
如果您修改代码以首先读取该字符串,它会按预期工作:
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>
如果发现这些文章在我自己使用 HTTP2 时有用:
https://developers.google.com/web/fundamentals/performance/http2/
http://undertow.io/blog/2015/04/27/An-in-depth-overview-of-HTTP2.html
- 1 回答
- 0 关注
- 223 浏览
添加回答
举报