在Netty 4中从ByteBuf构造HTTP块



我有一个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类,它可以简单地readInbound()就像这里一样,但我不确定我应该为此改变类型或将裸通道转换为EmbeddedChannel以摆脱问题。

x以后提供了一个开箱的HTTP编解码器,除非你使用HTTP聚合处理程序,否则它会给你HTTP块作为HttpContent对象。

这个例子展示了如何编写一个处理程序来接收这样的数据块:

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

最新更新