handler.serverUpgrade(ctx, settings, request); DefaultHttp2Headers headers = new DefaultHttp2Headers(); headers.method(request.method().name()); headers.path(request.uri()); headers.authority(request.headers().get("host"));
private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) { CharSequence host = request.headers().get(HttpHeaderNames.HOST); Http2Headers http2Headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); if (host != null) { http2Headers.authority(host); } return http2Headers; }
private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) { CharSequence host = request.headers().get(HttpHeaderNames.HOST); Http2Headers http2Headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); if (host != null) { http2Headers.authority(host); } return http2Headers; }
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) { HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = (HttpServerUpgradeHandler.UpgradeEvent) evt; // Map initial request headers to HTTP2 FullHttpRequest request = upgradeEvent.upgradeRequest(); Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); CharSequence host = request.headers().get(HttpHeaderNames.HOST); if (host != null) { headers.authority(host); } // Process mapped headers onHeadersRead(ctx, 1, headers, 0, true); } super.userEventTriggered(ctx, evt); }
protected Http2Headers getHeadersForPushNotification(final ApnsPushNotification pushNotification, final int streamId) { final Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.POST.asciiName()) .authority(this.authority) .path(APNS_PATH_PREFIX + pushNotification.getToken()) .scheme(HttpScheme.HTTPS.name()) .addInt(APNS_EXPIRATION_HEADER, pushNotification.getExpiration() == null ? 0 : (int) (pushNotification.getExpiration().getTime() / 1000)); if (pushNotification.getCollapseId() != null) { headers.add(APNS_COLLAPSE_ID_HEADER, pushNotification.getCollapseId()); } if (pushNotification.getPriority() != null) { headers.addInt(APNS_PRIORITY_HEADER, pushNotification.getPriority().getCode()); } if (pushNotification.getTopic() != null) { headers.add(APNS_TOPIC_HEADER, pushNotification.getTopic()); } if (pushNotification.getApnsId() != null) { headers.add(APNS_ID_HEADER, FastUUID.toString(pushNotification.getApnsId())); } return headers; }
handler.serverUpgrade(ctx, settings, request); DefaultHttp2Headers headers = new DefaultHttp2Headers(); headers.method(request.method().name()); headers.path(request.uri()); headers.authority(request.headers().get("host"));
.method(method) .authority(authority) .path(path)
private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) { CharSequence host = request.headers().get(HttpHeaderNames.HOST); Http2Headers http2Headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); if (host != null) { http2Headers.authority(host); } return http2Headers; }
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) { HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = (HttpServerUpgradeHandler.UpgradeEvent) evt; // Map initial request headers to HTTP2 FullHttpRequest request = upgradeEvent.upgradeRequest(); Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); CharSequence host = request.headers().get(HttpHeaderNames.HOST); if (host != null) { headers.authority(host); } // Process mapped headers onHeadersRead(ctx, 1, headers, 0, true); } super.userEventTriggered(ctx, evt); }
.method(HttpMethod.POST.asciiName()) .authority(this.authority) .path(APNS_PATH_PREFIX + pushNotification.getToken())
protected Http2Headers getHeadersForPushNotification(final ApnsPushNotification pushNotification, final int streamId) { final Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.POST.asciiName()) .authority(this.authority) .path(APNS_PATH_PREFIX + pushNotification.getToken()) .scheme(HttpScheme.HTTPS.name()) .addInt(APNS_EXPIRATION_HEADER, pushNotification.getExpiration() == null ? 0 : (int) (pushNotification.getExpiration().getTime() / 1000)); if (pushNotification.getCollapseId() != null) { headers.add(APNS_COLLAPSE_ID_HEADER, pushNotification.getCollapseId()); } if (pushNotification.getPriority() != null) { headers.addInt(APNS_PRIORITY_HEADER, pushNotification.getPriority().getCode()); } if (pushNotification.getTopic() != null) { headers.add(APNS_TOPIC_HEADER, pushNotification.getTopic()); } if (pushNotification.getApnsId() != null) { headers.add(APNS_ID_HEADER, FastUUID.toString(pushNotification.getApnsId())); } return headers; }
@Test public void testFullResponse() throws Exception { outputReceived = new CountDownLatch(2); Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headersIn, true); FullResponse responseIn = ResponseBuilders.newOk().streamId(1).body(Unpooled.EMPTY_BUFFER).build(); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks channel.writeOutbound(responseIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Http2Response responseOut = responses.remove(0); assertNotNull(responseOut); assertTrue(responseOut.payload instanceof Http2Headers); assertEquals("200", ((Http2Headers) responseOut.payload).status().toString()); assertTrue(responseOut.eos); assertEquals(1, responseOut.streamId); }
@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()); }
@Test public void testFullResponseWithBody() throws Exception { outputReceived = new CountDownLatch(2); ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "response"); Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headersIn, true); FullResponse responseIn = ResponseBuilders.newOk().streamId(1).body(body).build(); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks channel.writeOutbound(responseIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Http2Response responseOut = responses.remove(0); assertNotNull(responseOut); assertTrue(responseOut.payload instanceof Http2Headers); assertEquals("200", ((Http2Headers) responseOut.payload).status().toString()); assertFalse(responseOut.eos); assertEquals(1, responseOut.streamId); Http2Response bodyOut1 = responses.remove(0); assertNotNull(bodyOut1); assertTrue(bodyOut1.payload instanceof Http2DataFrame); assertEquals(body, ((Http2DataFrame) bodyOut1.payload).content()); assertTrue(bodyOut1.eos); assertEquals(1, bodyOut1.streamId); }
Http2Request.build(streamIdOne, new DefaultHttp2Headers().method("GET").path("/"), false); Http2Request requestSubSequential1 = Http2Request.build(streamIdOne, new DefaultHttp2DataFrame(Unpooled.EMPTY_BUFFER), true); Http2Request.build(streamIdTwo, new DefaultHttp2Headers().method("POST").path("/"), false); Http2Request requestSubSequential2 = Http2Request.build(streamIdTwo, new DefaultHttp2DataFrame(Unpooled.EMPTY_BUFFER), true);
@Test public void testStreamingResponse() throws Exception { outputReceived = new CountDownLatch(3); Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headersIn, true);
@Test public void testStreamingResponseWithTrailingHeaders() { outputReceived = new CountDownLatch(3); Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headersIn, true);
outputReceived = new CountDownLatch(3); Http2Headers headers = new DefaultHttp2Headers().method("POST").path("/"); Http2Request requestIn = Http2Request.build(1, headers, false); ByteBuf body1 = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "body1");
outputReceived = new CountDownLatch(4); Http2Headers headers = new DefaultHttp2Headers().method("POST").path("/"); Http2Request requestIn = Http2Request.build(1, headers, false); ByteBuf body1 = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "body1");