2014-12-19 59 views
3

我正在使用Netty编写一个简单的反向代理。因为我不想处理的原始字节自己,我已经添加了一个请求解码器和目标聚集到处理管道,以及一个响应编码器,最后我自己的处理程序,像这样如何在netty中处理大于1024字节的http请求?

ChannelPipeline p = ch.pipeline(); 
p.addLast(new HttpRequestDecoder()); 
p.addLast(new HttpObjectAggregator(MAX_CONTENT_LENGTH)); 
p.addLast(new HttpResponseEncoder()); 
p.addLast(new FrontendHandler(...)); 

FrontendHandler延伸SimpleChannelInboundHandler<HttpRequest>,所以它有一个

// Start reading as soon as a request comes in... 
public void channelActive(ChannelHandlerContext ctx) { 
    ctx.read(); 
} 

protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) { 
    // copy request, fix headers, forward to backend server 
} 

会发生什么情况是,服务器挂起,如果收到请求时瓦亭大于1024字节(例如,它有一些饼干)。通过一些试验和错误,我发现如果我在处理程序上设置ChannelOption.AUTO_READ一切正常,所以它看起来像我的旧代码丢失了ctx.read()调用某处,但我不知道在哪里。如果我这样做

@Override 
public void channelReadComplete(ChannelHandlerContext ctx) { 
    ctx.read(); 
} 

然后我得到了channelRead0内部异常,似乎被处理的是一个仍然是不完整的HTTP请求,哪一种违背了使用请求解码器/对象聚合的目的是原因。我错过了什么?

+0

查收这可能有帮助... http://stackoverflow.com/questions/10170564/in-netty-we-can-only-write -and-接收数据低于1024bytes知识,我们灿WRI/11373405#11373405 –

回答

0

我不知道HttpObjectAggregator是否可以处理分块消息。你可以尝试使用HttpChunkAggregator,是这样的:

pipeline.addLast("decoder", new HttpRequestDecoder(4096, 4096, 100*1024*1024)); 
    pipeline.addLast("aggregator", new HttpChunkAggregator(100*1024*1024)); 
    pipeline.addLast("encoder", new HttpResponseEncoder()); 
    pipeline.addLast("hndlr", new FrontendHandler(...));