从管道中删除HttpObjectAggregator时,Netty Server不会返回响应

时间:2018-05-30 04:58:00

标签: netty

我正在尝试使用netty 4.1.16.Final创建简单的HTTP Server。

以下是HTTP Server的代码 -

EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup slaveGroup = new NioEventLoopGroup();
final ServerBootstrap bootstrap =
                    new ServerBootstrap()
                            .group(masterGroup, slaveGroup)
                            .channel(NioServerSocketChannel.class)
                            .childHandler(new ChannelInitializer<SocketChannel>() {
                                @Override
                                protected void initChannel(SocketChannel ch) {
                                    ch.pipeline().addLast("codec", new HttpServerCodec());
                                    ch.pipeline().addLast("aggregator",
                                            new HttpObjectAggregator(512 * 1024));
                                    ch.pipeline().addLast("request",
                                            new HTTPSimpleChannelInboundHandler());
                                }
                            }).option(ChannelOption.SO_BACKLOG, 128)
                            .childOption(ChannelOption.SO_KEEPALIVE, true);
            channel = bootstrap.bind(8080).sync();
HTTP Handler类HTTPSimpleChannelInboundHandler

代码位于 -

之下
public class HTTPSimpleChannelInboundHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
        HttpResponseStatus responseStatus = OK;
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus, Unpooled.copiedBuffer("My Netty".getBytes()));
        response.headers().add(request.headers());
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        if (isKeepAlive(request)) {
            response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        }
        if (is100ContinueExpected(request)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        ctx.writeAndFlush(response);
    }

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

上面的代码工作正常。 但是当我在服务器代码中评论下面一行时,我没有收到回复。

ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024));

以下是我从服务器获取的日志 -

01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
GET /test HTTP/1.1
Host: localhost:5055
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: _ga=GA1.1.177481759.1523295602; Idea-46064427=2276f52b-2928-4410-8f4c-c7561bd33457
Connection: keep-alive
Upgrade-Insecure-Requests: 1 that reached at the tail of the pipeline. Please check your pipeline configuration.
01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message EmptyLastHttpContent that reached at the tail of the pipeline. Please check your pipeline configuration.

为什么HttpObjectAggregator在ChannelPipeline中是强制性的?

1 个答案:

答案 0 :(得分:1)

问题在于,如果你想对FullHttpRequest采取行动,你需要管道中的HttpObjectAggregator来处理这些问题。

如果没有此项,您将收到代表HTTP消息部分的HttpRequestHttpContentLastHttpContent个实例。每个都需要处理,而HttpRequest标记新HTTP消息的开始,LastHttpContent标记结束。