http - 在 Go 中解码 http2 帧头/数据

标签 http go http2

我正在尝试从包含 HTTP/2 数据的 tcp 连接中读取数据。下面是读取 HEADERS 框架的代码 -

framer := http2.NewFramer(conn, conn)
frame, _ := framer.ReadFrame()

fmt.Printf("fh type: %s\n", frame.Header().Type)
fmt.Printf("fh type: %d\n", frame.Header().Type)
fmt.Printf("fh flag: %d\n", frame.Header().Flags)
fmt.Printf("fh length: %d\n", frame.Header().Length)
fmt.Printf("fh streamid: %d\n", frame.Header().StreamID)
headersframe := (frame1.(*http2.HeadersFrame))
fmt.Printf("stream ended? %v\n", headersframe.StreamEnded())
fmt.Printf("block fragment: %x\n", headersframe.HeaderBlockFragment())        

我使用 curl 发送请求 -

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

这是我得到的输出(在阅读连接前言和设置之后),如果我使用上面的代码从 conn 中读取 -

fh type: HEADERS
fh type: 1
fh flag: 5
fh length: 30
fh streamid: 1
stream ended? true
block fragment: 828487418a089d5c0b8170dc6c4d8b7a8825b650c3abb6f2e053032a2f2a

我理解输出,除了 block 片段部分以及如何将其解码为 ascii 字符串?我想知道协议(protocol)/方法/url路径信息。

最佳答案

“头 block 片段”使用 HPACK 编码.

Go 有一个编码和解码 HPACK 的实现,因此您不必自己编写。

你可以找到here同时使用编码器和解码器 Go API 的示例。

关于http - 在 Go 中解码 http2 帧头/数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462477/

相关文章:

go - 如何针对源代码和二进制包编译 Go 程序?

tomcat - CloseNowException : This stream is not writeable

php - 合并请求不起作用

javascript - Angular 将表单中的代码放入 [POST]

c++ - 为什么Boost-Beast 给我部分消息异常

go - 如何确保在结构中定义字段?

Go ReadString 问题 : The filename, 目录名,或卷标语法不正确

apache - Apache (Ubuntu 14.04) 上的 HTTP/2 不工作

amazon-web-services - 如何使用 ingress-nginx 让 HTTP/2 在 Kubernetes 集群中工作

http - std::io::TcpStream::read_as_string 返回空字符串