我正在尝试使用 netty 4.1.16.Final 创建简单的 HTTP 服务器。
以下是 HTTP 服务器的代码 -
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();
Run Code Online (Sandbox Code Playgroud)
HTTP 处理程序类的代码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();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常。但是当我在服务器代码中评论以下行时,我没有得到响应。
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024));
Run Code Online (Sandbox Code Playgroud)
下面是我从服务器得到的日志 -
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.
Run Code Online (Sandbox Code Playgroud)
为什么 HttpObjectAggregator 在 ChannelPipeline 中是强制性的?
问题是,如果你想采取行动,FullHttpRequest你需要HttpObjectAggregator在管道中负责组装这些。
如果没有这个,您将收到HttpRequest, HttpContent,LastHttpContent代表 HTTP 消息部分的实例。它们中的每一个都需要处理,而 aHttpRequest标记新 HTTP 消息的开始和LastHttpContent结束。
| 归档时间: |
|
| 查看次数: |
586 次 |
| 最近记录: |