在用 Netty 编写的 HTTP 反向代理中,如何确定为单个 HTTP 请求( header + 任何正文内容)读取或为单个 HTTP 响应写入的字节数? Netty 开箱即用的 HTTPRequestDecoder
和 HTTPResponseEncoder
(或父类(super class))如何使其他 ChannelHandler
可以使用此信息?
我知道在管道的开头放置一个 ChannelHandler
来记录为 channel 写入和读取的总字节数很容易,但我不想那样 - 我想知道每个请求和每个响应读取或写入的字节数。由于 Netty 开箱即用的 HTTP 编码器和解码器是进行读取和写入的,我希望我可以将某些东西子类化并“ Hook ”到读/写逻辑以获得编码或解码的每条消息的计数。这可能吗?
在表示请求/响应大小直方图以及支持 HTTP 扩展访问日志格式时,此数据很重要:
http://httpd.apache.org/docs/current/mod/mod_log_config.html
从该页面,访问记录单个请求/响应对时使用的日志格式字符串 token :
%I Bytes received, including request and headers. Cannot be zero. You need to enable mod_logio to use this.
%O Bytes sent, including headers. May be zero in rare cases such as when a request is aborted before a response is sent. You need to enable mod_logio to use this.
%S Bytes transferred (received and sent), including request and headers, cannot be zero. This is the combination of %I and %O. You need to enable mod_logio to use this.
一旦请求被完全读取或响应被 Netty 处理程序完全写入,我将如何确定在输出字符串中替换上述格式标记所需的计数?
最佳答案
目前无法获取这些信息,因为 Netty 仅将 HttpRequest/HttpResponse/HttpContent 的“已解析”表示传递给管道中的下一个处理程序。您需要自己进行计算,例如计算标题中的每个字符等。
关于java - Netty:单个 HTTP 请求或响应中的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38547902/