@Override public int streamId() { return request.streamId(); }
void onInboundRequest(Request request) { MessageMetaState initialRequest = streamIdRequests.get(request.streamId()); if (initialRequest == null) { if (request.startOfMessage()) { streamIdRequests.put( request.streamId(), new MessageMetaState(request, request.endOfMessage())); } else { log.error( "Received an h2 message segment without initial startOfMessage == true - request: {}", request); } } else { initialRequest.requestFinished = request.endOfMessage(); } }
@VisibleForTesting Optional<ClientConfig> getClientConfig( IntObjectMap<Optional<ClientConfig>> cachedClientConfig, Request request) { int hashKey = request.streamId(); // we only do a fresh round robin if the request is a either a FULL HTTP REQUEST or the First Part of a Chunked Requets if (request.isFullMessage()) { return computationFunction.get(); } else if (request.startOfMessage()) { Optional<ClientConfig> newClientConfig = computationFunction.get(); cachedClientConfig.put(hashKey, newClientConfig); return newClientConfig; } else if (request.endOfMessage()) { return cachedClientConfig.remove(hashKey); } else { return cachedClientConfig.get(hashKey); } }
@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 protected void channelRead0(ChannelHandlerContext ctx, Request msg) throws Exception { if (msg instanceof SegmentedRequestData && msg.endOfMessage()) { sendResponse(ctx, msg.httpTraceInfo(), msg.streamId()); return; } else if (msg instanceof FullRequest) { sendResponse(ctx, msg.httpTraceInfo(), msg.streamId()); } ctx.write(msg); }
public static DefaultFullResponse.Builder newOk(Request request) { return defaultHeaders( DefaultFullResponse.builder() .streamId(request.streamId()) .httpTraceInfo(request.httpTraceInfo()) .status(HttpResponseStatus.OK)); } }
when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.isFullMessage()).thenReturn(true); when(request2.path()).thenReturn("dontcare"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.isFullMessage()).thenReturn(true); when(request3.path()).thenReturn("dontcare"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request4.isFullMessage()).thenReturn(true); when(request4.path()).thenReturn("dontcare"); when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.isFullMessage()).thenReturn(true); when(request2.path()).thenReturn("dontcare"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.isFullMessage()).thenReturn(true); when(request3.path()).thenReturn("dontcare"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request4.isFullMessage()).thenReturn(true); when(request4.path()).thenReturn("dontcare"); when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
when(request1.endOfMessage()).thenReturn(false); when(request1.path()).thenReturn("/foo"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.endOfMessage()).thenReturn(false); when(request2.path()).thenReturn("/foo"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.endOfMessage()).thenReturn(true); when(request3.path()).thenReturn("/foo"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
public static Response newServiceTimeout(Request request) { return defaultHeaders(DefaultFullResponse.builder()) .body(Unpooled.EMPTY_BUFFER) .httpTraceInfo(request.httpTraceInfo()) .streamId(request.streamId()) .status(HttpResponseStatus.GATEWAY_TIMEOUT) .build(); }
public static Response newNotFound(Request request) { return defaultHeaders(DefaultFullResponse.builder()) .body(Unpooled.EMPTY_BUFFER) .httpTraceInfo(request.httpTraceInfo()) .streamId(request.streamId()) .status(HttpResponseStatus.NOT_FOUND) .build(); }
when(request1.endOfMessage()).thenReturn(false); when(request1.path()).thenReturn("/foo"); when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request2.endOfMessage()).thenReturn(false); when(request2.path()).thenReturn("/foo"); when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE); when(request3.endOfMessage()).thenReturn(true); when(request3.path()).thenReturn("/foo"); when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
public static Response newServiceUnavailable(Request request) { return defaultHeaders(DefaultFullResponse.builder()) .body(Unpooled.EMPTY_BUFFER) .httpTraceInfo(request.httpTraceInfo()) .streamId(request.streamId()) .status(HttpResponseStatus.SERVICE_UNAVAILABLE) .build(); }
when(request1a.endOfMessage()).thenReturn(false); when(request1a.path()).thenReturn("/foo"); when(request1a.streamId()).thenReturn(streamId1); when(request1b.endOfMessage()).thenReturn(false); when(request1b.path()).thenReturn("/foo"); when(request1b.streamId()).thenReturn(streamId2); when(request2a.endOfMessage()).thenReturn(false); when(request2a.path()).thenReturn("/foo"); when(request2a.streamId()).thenReturn(streamId1); when(request2b.endOfMessage()).thenReturn(false); when(request2b.path()).thenReturn("/foo"); when(request2b.streamId()).thenReturn(streamId2); when(request3a.endOfMessage()).thenReturn(true); when(request3a.path()).thenReturn("/foo"); when(request3a.streamId()).thenReturn(streamId1); when(request3b.endOfMessage()).thenReturn(true); when(request3b.path()).thenReturn("/foo"); when(request3b.streamId()).thenReturn(streamId2);
String httpType = request.streamId() == Message.H1_STREAM_ID_NONE ? "http1.1" : "h2"; Tracer.SpanBuilder spanBuilder = tracer .withTag(Tags.HTTP_URL.getKey(), request.path()) .withTag("http.request.type", httpType) .withTag("http.request.streamId", request.streamId()) .withTag("http.request.source-ip-address", localIp(ctx)) .withTag("http.request.originating-ip-address", requestorIpAddress); new HttpHeadersInjectAdapter(request)); request.httpTraceInfo().setSpan(scope.span()); setSpan(ctx, request.streamId(), scope.span());
private void writeRequest(ChannelHandlerContext ctx, Request request, ChannelPromise promise) { /* // TOOD(CK): define ACCEPT? if (!response.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); } */ Http2Headers headers = request.headers().http2Headers(); headers.authority(request.host()).method(request.method().asciiName()).path(request.path()); int streamId = request.streamId(); if (request instanceof FullRequest) { if (request.body().readableBytes() > 0) { PromiseCombiner combiner = new PromiseCombiner(); combiner.add(ctx.write(Http2Request.build(streamId, headers, false), ctx.newPromise())); Http2DataFrame data = new DefaultHttp2DataFrame(request.body(), true); combiner.add(ctx.write(Http2Request.build(streamId, data, true), ctx.newPromise())); combiner.finish(promise); } else { ctx.write(Http2Request.build(streamId, headers, true), promise); } } else { ctx.write(Http2Request.build(streamId, headers, false), promise); } }
String httpType = request.streamId() == Message.H1_STREAM_ID_NONE ? "http1.1" : "h2"; Tracer.SpanBuilder spanBuilder = tracer .withTag(Tags.HTTP_URL.getKey(), request.path()) .withTag("http.request.type", httpType) .withTag("http.request.streamId", request.streamId()) .withTag("http.request.source-ip-address", localIp(ctx)) .withTag("http.request.originating-ip-address", requestorIpAddress); setSpan(ctx, request.streamId(), scope.span()); request.httpTraceInfo().setSpan(scope.span());
private void sendResponse(ChannelHandlerContext ctx, Request msg) { val resp = DefaultFullResponse.builder() .headers(new DefaultHeaders()) .status(HttpResponseStatus.OK) .body(Unpooled.EMPTY_BUFFER) .streamId(msg.streamId()) .build(); ctx.writeAndFlush(resp); } }
public Request buildRequest(Request request, String proxyHost, String path) { Request result; if (request instanceof FullRequest) { result = DefaultFullRequest.builder() .body(request.body()) .method(request.method()) .path(path) .streamId(request.streamId()) .headers(request.headers()) .httpTraceInfo(request.httpTraceInfo()) .host(proxyHost) .build(); } else if (request instanceof SegmentedRequest) { result = DefaultSegmentedRequest.builder() .method(request.method()) .path(path) .streamId(request.streamId()) .headers(request.headers()) .httpTraceInfo(request.httpTraceInfo()) .host(proxyHost) .build(); } else { // this should never happen! log.error("Unknown request type: {}", request); result = null; } return result; }
@Test public void testFullRequest() throws Exception { outputReceived = new CountDownLatch(1); Http2Headers headers = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headers, true); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Request requestOut = requests.remove(0); assertNotNull(requestOut); assertTrue(requestOut instanceof FullRequest); assertEquals("h2", requestOut.version()); assertEquals(HttpMethod.GET, requestOut.method()); assertEquals("/", requestOut.path()); assertFalse(requestOut.hasBody()); assertNotNull(requestOut.body()); assertEquals(0, requestOut.body().readableBytes()); assertEquals(1, requestOut.streamId()); }