|
| 1 | +package io.github.kimmking.netty.server; |
| 2 | + |
| 3 | +import io.netty.buffer.Unpooled; |
| 4 | +import io.netty.channel.ChannelFutureListener; |
| 5 | +import io.netty.channel.ChannelHandlerContext; |
| 6 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 7 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 8 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 9 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 10 | +import io.netty.handler.codec.http.HttpUtil; |
| 11 | +import io.netty.util.ReferenceCountUtil; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; |
| 16 | +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; |
| 17 | +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; |
| 18 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
| 19 | +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| 20 | + |
| 21 | +public class HttpHandler extends ChannelInboundHandlerAdapter { |
| 22 | + |
| 23 | + private static Logger logger = LoggerFactory.getLogger(HttpHandler.class); |
| 24 | + |
| 25 | + @Override |
| 26 | + public void channelReadComplete(ChannelHandlerContext ctx) { |
| 27 | + ctx.flush(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 32 | + try { |
| 33 | + //logger.info("channelRead流量接口请求开始,时间为{}", startTime); |
| 34 | + FullHttpRequest fullRequest = (FullHttpRequest) msg; |
| 35 | + String uri = fullRequest.uri(); |
| 36 | + //logger.info("接收到的请求url为{}", uri); |
| 37 | + if (uri.contains("/test")) { |
| 38 | + handlerTest(fullRequest, ctx); |
| 39 | + } |
| 40 | + } finally { |
| 41 | + ReferenceCountUtil.release(msg); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { |
| 46 | + FullHttpResponse response = null; |
| 47 | + try { |
| 48 | + String value = "hello,kimmking"; |
| 49 | + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); |
| 50 | + response.headers().set("Content-Type", "application/json"); |
| 51 | + response.headers().setInt("Content-Length", response.content().readableBytes()); |
| 52 | + |
| 53 | + } catch (Exception e) { |
| 54 | + logger.error("处理测试接口出错", e); |
| 55 | + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); |
| 56 | + } finally { |
| 57 | + if (fullRequest != null) { |
| 58 | + if (!HttpUtil.isKeepAlive(fullRequest)) { |
| 59 | + ctx.write(response).addListener(ChannelFutureListener.CLOSE); |
| 60 | + } else { |
| 61 | + response.headers().set(CONNECTION, KEEP_ALIVE); |
| 62 | + ctx.write(response); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 70 | + cause.printStackTrace(); |
| 71 | + ctx.close(); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments