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); }