|
1 | 1 | package io.reactivesocket.netty; |
2 | 2 |
|
| 3 | +import io.netty.channel.ChannelFutureListener; |
3 | 4 | import io.netty.channel.ChannelHandler; |
4 | 5 | import io.netty.channel.ChannelHandlerContext; |
5 | 6 | import io.netty.channel.SimpleChannelInboundHandler; |
6 | | -import io.netty.handler.codec.http.DefaultFullHttpResponse; |
7 | | -import io.netty.handler.codec.http.FullHttpRequest; |
8 | | - |
9 | | -import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
10 | | -import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| 7 | +import io.netty.handler.codec.http.*; |
11 | 8 |
|
12 | 9 | @ChannelHandler.Sharable |
13 | 10 | public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> { |
14 | 11 | @Override |
15 | | - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
16 | | - ctx.flush(); |
17 | | - ctx.close(); |
18 | | - } |
19 | | - |
20 | | - @Override |
21 | | - protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { |
22 | | - ctx.write(new DefaultFullHttpResponse(HTTP_1_1, OK, msg.content().copy())); |
| 12 | + protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { |
| 13 | + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, request.content().retain()); |
| 14 | + HttpUtil.setContentLength(response, response.content().readableBytes()); |
| 15 | + if (HttpUtil.isKeepAlive(request)) { |
| 16 | + response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); |
| 17 | + ctx.writeAndFlush(response); |
| 18 | + } else { |
| 19 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 20 | + } |
23 | 21 | } |
24 | 22 | } |
0 commit comments