计算通过网络接收到的传入请求的大小。

huangapple go评论89阅读模式
英文:

Calculate size of Incoming Request as received over the wire

问题

我正在进行一个基于 ReverseProxy 的项目,并且我正在尝试找出如何计算整个传入请求的大小,包括头部、正文和尾部。

由于速度很重要,我不想通过累加解析请求的各个字段来计算大小,而是希望在读取传入请求时进行计算。

从我所看到的情况来看,"request.go" 中的 readRequest 函数似乎是最适合进行此操作的地方,但我不确定如何修改或包装该函数以计算大小。

英文:

I am working on a ReverseProxy based project and I am trying to figure out how to calculate the size of the entire incoming request. i.e. including headers, body & trailers.

Since speed is important, I don't want to calculate it by adding up the various fields of the parsed request, and instead was looking for a way to do it as the Incoming Request is read.

From what I can see, the readRequest function from "request.go" seems to be the most appropriate place to do this, but I am unsure about how to go about modifying this or wrapping this function to also calculate the size.

答案1

得分: 0

在这里找到了解决方案-https://stackoverflow.com/questions/43944378/count-sent-and-received-bytes-in-go-in-an-http-handler-servehttp-function/43944695#43944695

可以修改为在反向代理场景中工作,并且正好符合我的要求。

更新:这个Github代码在最终解决方案中也非常有用。https://github.com/j0hnsmith/connspy

现在,当使用Golang作为反向代理时,我可以获取通过网络发送和接收的原始数据。

希望这对将来的某个人有所帮助。

英文:

Found a solution for this here - https://stackoverflow.com/questions/43944378/count-sent-and-received-bytes-in-go-in-an-http-handler-servehttp-function/43944695#43944695

It can be modified to work in a Reverse Proxy scenario & does exactly what I wanted.

Update: This Github code was also very useful in the final solution. https://github.com/j0hnsmith/connspy

Now I can get the raw data received & sent over the wire when using Golang as a reverse proxy

Hope this helps someone in the future

huangapple
  • 本文由 发表于 2021年7月29日 10:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/68569240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定