@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { log.debug("write: msg={}", msg); if (msg instanceof SegmentedData) { SegmentedData segmentedData = (SegmentedData) msg; HttpContent content = buildContent(segmentedData); getProxyRequestQueue(ctx) .onRequestWriteOrEnqueue(ctx, segmentedData.streamId(), content, promise); } else if (msg instanceof Request) { Request request = (Request) msg; HttpRequest message = buildRequest(request); getProxyRequestQueue(ctx).onRequestWriteOrEnqueue(ctx, request.streamId(), message, promise); } else { ctx.write(msg, promise); } } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpObject) { Response response = wrapResponse(ctx, (HttpObject) msg); ctx.fireChannelRead(response); getProxyRequestQueue(ctx).onResponseDrainNext(ctx, response); } else { ctx.fireChannelRead(msg); } }
.pipeline() .addLast("codec", new HttpClientCodec()) .addLast("application codec", new Http1ClientCodec());
Response wrapResponse(ChannelHandlerContext ctx, HttpObject msg) { log.debug("wrapResponse msg={}", msg); final Response response; if (msg instanceof FullHttpResponse) { response = new FullHttp1Response((FullHttpResponse) msg); } else if (msg instanceof HttpResponse) { response = new SegmentedHttp1Response((HttpResponse) msg); } else if (msg instanceof HttpContent) { response = getProxyRequestQueue(ctx) .currentResponse() .map(r -> new SegmentedResponseData(r, new Http1SegmentedData((HttpContent) msg))) .orElse(null); } else { // TODO(CK): throw an exception if response is null? response = null; } return getProxyRequestQueue(ctx) .currentProxiedH2StreamId() .<Response>map( streamId -> { if (response instanceof SegmentedResponseData) { return new ProxySegmentedResponseData((SegmentedResponseData) response, streamId); } else { return new ProxyResponse(response, streamId); } }) .orElse(response); }
@Override protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { if (protocol.equals(ApplicationProtocolNames.HTTP_1_1)) { replaceCodec(ctx, new HttpClientCodec()); replaceApplicationCodec(ctx, new Http1ClientCodec()); ctx.fireUserEventTriggered(RequestBuffer.WriteReady.INSTANCE); } else if (protocol.equals(ApplicationProtocolNames.HTTP_2)) { replaceCodec(ctx, http2Handler.get()); replaceApplicationCodec(ctx, new Http2ClientCodec()); } else { throw new RuntimeException("Unknown Application Protocol '" + protocol + "'"); } } }
public void initChannel(Channel channel) { if (enableTls) { ClientConfig config = ClientConfig.fromConfig("xio.h1TestClient"); SslContext sslContext = SslContextFactory.buildClientContext(config.getTls()); channel .pipeline() .addLast( sslContext.newHandler( channel.alloc(), remote.getHostString(), remote.getPort())); } channel .pipeline() .addLast(new HttpClientCodec()) .addLast(new Http1ClientCodec()) .addLast(new RawBackendHandler(ctx)); } });
@Before public void setUp() { channel .pipeline() .addLast( "nextRequest", new ChannelOutboundHandlerAdapter() { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { log.debug("request: " + msg); requests.add((HttpObject) msg); outputReceived.countDown(); } }); channel.pipeline().addLast("handler", new Http1ClientCodec()); channel .pipeline() .addLast( "nextResponse", new SimpleChannelInboundHandler<Response>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Response msg) { log.debug("response: " + msg); responses.add(msg); outputReceived.countDown(); } }); }