java - Netty - 如何将 ChunkedWriteHandler 与 HttpContentCompressor 结合起来

标签 java http netty http-compression chunked-encoding

我正在尝试实现一个 HTTP 服务器(使用 Netty),它不仅可以提供“常规”html 页面,还可以提供大文件。因此,我想在我的管道中使用 ChunkedWriteHandler 以及 HttpContentCompressor

目前,这个管道初始化如下:

pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("deflater", new HttpContentCompressor());
pipeline.addLast(new NettyHandler());

NettyHandler 遵循以下方案:

@Override
public void channelRead(final ChannelHandlerContext context, final Object message) throws Exception {
    try {
        if (message instanceof HttpRequest) {
            final HttpRequest request = (HttpRequest) message;
            final HttpContext httpContext = new HttpContext(request, context);
            final ChannelFuture future = handleHttpMessage(httpContext);
            httpContext.closeOn(future);
        }
    } finally {
        ReferenceCountUtil.release(message);
    }
}


private ChannelFuture handleHttpMessage(final HttpContext context) {
    //writing to the wire via ChannelHandlerContext.write(...)
    return context.getChannelContext().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}

如果我请求/发送小文件(我的测试文件大约 500 字节),一切正常。但是一旦请求的文件变大(我的测试文件大约 350 MB),浏览器(使用 chrome 和 firefox 进行测试)报告有关接收到的主体的编码部分的问题。 chrome 说 ERR_CONTENT_DECODING_FAILED,firefox 说类似无法读取源文件

我做错了什么吗?我是否必须即时操纵管道?在此先感谢您的帮助!

最佳答案

您需要将写入的 block 包装到 DefaultHttpContent 中,因为 HttpContentCompressor 不理解 ByteBuf 实例。

因此只需将一个特殊的 HttpContentCompressor 放入知道如何处理 ByteBuf 实例的 ChannelPipeline 中。类似于 HttpChunkContentCompressorvert.x 项目中。

确保将它放在 ChunkedWriteHandler 之前。

关于java - Netty - 如何将 ChunkedWriteHandler 与 HttpContentCompressor 结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18982925/

相关文章:

java - 如何添加新玩家?

http - 您可以使用 Wireshark 检查/监控请求中发送的客户端证书吗?

ruby - Ruby 套接字库获取方法的说明

spring-boot - Docker - java netty_tcnative 的问题

java - netty中如何在断开连接后自动连接到TCP服务器

java - 如何创建 Redisson 客户端来连接到 redis

java.lang.LinkageError : loader constraint violation: when resolving method "org. jboss.resteasy.client.core.executors.ApacheHttpClient4Executor 错误

java - 页面加载和 Selenium - 加载后驱动程序不执行任何操作

android - 基本的android http获取

java - Android 上的安全性