@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { List<ByteBuf> payload; HttpHeaders headers = new CombinedHttpHeaders(true); headers.add(HttpHeaderNames.UPGRADE, "TLS/1.2"); headers.add(HttpHeaderNames.UPGRADE, HTTP_1_1); headers.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE); headers.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); headers.add(HttpHeaderNames.CONTENT_LENGTH, "0"); DefaultFullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, UPGRADE_REQUIRED, Unpooled.EMPTY_BUFFER, headers, EmptyHttpHeaders.INSTANCE); payload = Recipes.encodeResponse(response); for (ByteBuf buffer : payload) { ctx.write(buffer.copy()); } ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }
@Override public void push(final String method, final String path, final Map<String, Object> headers) { ctx.channel().eventLoop().execute(() -> { AsciiString streamIdHeader = HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(); Http2Connection connection = encoder.connection(); int nextStreamId = connection.local().incrementAndGetNextStreamId(); Http2Headers h2headers = new DefaultHttp2Headers() .path(path) .method(method) .authority(authority) .scheme(scheme); headers.forEach((n, v) -> h2headers.add(n, v.toString())); encoder.writePushPromise(ctx, streamId, nextStreamId, h2headers, 0, ctx.newPromise()); // TODO: Is there another way of handling a push promise? DefaultFullHttpRequest pushRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method.toUpperCase()), path, Unpooled.EMPTY_BUFFER, new DefaultHttpHeaders(false).set(streamIdHeader, nextStreamId), EmptyHttpHeaders.INSTANCE); ctx.pipeline().fireChannelRead(pushRequest); ctx.pipeline().fireChannelReadComplete(); }); }
HttpRequest buildRequest(ChannelHandlerContext ctx, Request request) { if (!request.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { request.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); } if (request.keepAlive()) { request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } if (request instanceof FullRequest) { FullRequest full = (FullRequest) request; ByteBuf content; if (full.body() != null) { content = full.body(); } else { content = Unpooled.EMPTY_BUFFER; } if (!full.headers().contains(HttpHeaderNames.CONTENT_LENGTH)) { full.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); } // Request request = getChannelRequest(ctx); // setChannelResponse(ctx, null); return new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, full.method(), full.path(), content, full.headers().http1Headers(false, true), EmptyHttpHeaders.INSTANCE); } else { // TODO(CK): TransferEncoding return new DefaultHttpRequest( HttpVersion.HTTP_1_1, request.method(), request.path(), request.headers().http1Headers(false, true)); } }
HttpResponse buildResponse(ChannelHandlerContext ctx, Response response) { if (!response.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); } Request request = getChannelRequest(ctx); if (request.keepAlive()) { response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } if (response instanceof FullResponse) { FullResponse full = (FullResponse) response; ByteBuf content; if (full.body() != null) { content = full.body(); } else { content = Unpooled.EMPTY_BUFFER; } if (!full.headers().contains(HttpHeaderNames.CONTENT_LENGTH)) { full.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); } setChannelRequest(ctx, null); return new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, full.status(), content, full.headers().http1Headers(false, false), EmptyHttpHeaders.INSTANCE); } else { // TODO(CK): TransferEncoding // We don't know the size of the message payload so set TransferEncoding to chunked if (!response.headers().contains(HttpHeaderNames.TRANSFER_ENCODING)) { response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); } return new DefaultHttpResponse( HttpVersion.HTTP_1_1, response.status(), response.headers().http1Headers(false, false)); } }