在Go中解码http2帧头/数据 [英] Decoding http2 frame header/data in Go

查看:224
本文介绍了在Go中解码http2帧头/数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read from a tcp connection which contains HTTP/2 data. Below is the code for reading HEADERS frame -

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作为-发送请求

I send request using curl as -

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

如果使用上面的代码从conn中读取内容,则这是我得到的输出(在阅读了连接序言和SETTINGS之后)-

This is the output I get (after reading connection preface and SETTINGS), if I read from the conn using above code -

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

我理解输出,除了 blockfragment 部分,以及如何将其解码为ascii字符串?我想知道协议/方法/URL路径信息.

I understand the ouput, except the block fragment part and how to decode it into ascii string? I want to know the protocol/method/url path information.

推荐答案

我使用Go hpack库(

I figured it out using Go hpack library (https://godoc.org/golang.org/x/net/http2/hpack) -

decoder := hpack.NewDecoder(2048, nil)
hf, _ := decoder.DecodeFull(headersframe.HeaderBlockFragment())
for _, h := range hf {
      fmt.Printf("%s\n", h.Name + ":" + h.Value)
}

此打印-

:method:GET
:path:/
:scheme:https
:authority:127.0.0.1:5252
user-agent:curl/7.58.0
accept:*/*

这篇关于在Go中解码http2帧头/数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆