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