【问题标题】:Constructing HTTP Chunks from ByteBuf in Netty 4在 Netty 4 中从 ByteBuf 构造 HTTP 块
【发布时间】:2021-09-08 04:15:57
【问题描述】:

我有一个 Netty 3 代码库,带有一个扩展 ReplayingDecoder 的 HTTP 消息解码器,我需要迁移分析消息块的代码,这意味着我需要获取这些块。

protected Object decode(ChannelHandlerContext ctx, Channel channel, 
                        ByteBuf buffer, State state) {

    //...

    HttpChunk chunk = new DefaultHttpChunk(buffer.readBytes(toRead));

    //...

}

根据我收集到的信息,我需要改用 HttpChunkedInput,但创建一个非常困难。

                                           //requires an InputStream...
HttpChunkedInput hc = new HttpChunkedInput(new ChunkedStream(...));

//but this seems really clunky/too awkward to be right.
HttpChunkedInput hc = new HttpChunkedInput(new ChunkedStream(new ByteArrayInputStream(buffer.array()));

ByteBuf 似乎没有办法直接转储流。我是否缺少更好的 Util 类 API?我确实发现有一个新的EmbeddedChannel 类,它可以像here 一样简单地 readInbound(),但我不确定我是否应该为此更改类型或将裸通道转换为 EmbeddedChannel 以摆脱问题.

【问题讨论】:

    标签: java netty netty4


    【解决方案1】:

    Netty 4.x 及更高版本带有一个开箱即用的 HTTP 编解码器,除非您使用 HTTP 聚合处理程序,否则它将为您提供 HTTP 块作为 HttpContent 对象。

    这个例子展示了如何编写一个接收此类块的处理程序:

    https://github.com/netty/netty/blob/a329857ec20cc1b93ceead6307c6849f93b3f101/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java#L60

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      相关资源
      最近更新 更多