private void h2cOrHttp1(final ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline(); HttpServerCodec http1codec = http1Codec(); String baseName = name; baseName = addAfter(p, baseName, "codec", http1codec); baseName = addAfter(p, baseName, "h2upgrade", new HttpServerUpgradeHandler(http1codec, protocol -> { if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return null; } return new Http2ServerUpgradeCodec(newHttp2ConnectionHandler(p)); }, maxContentLength)); }
/** * Decodes the settings header and returns a {@link Http2Settings} object. */ private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader) throws Http2Exception { ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8); try { // Decode the SETTINGS payload. ByteBuf payload = Base64.decode(header, URL_SAFE); // Create an HTTP/2 frame for the settings. ByteBuf frame = createSettingsFrame(ctx, payload); // Decode the SETTINGS frame and return the settings object. return decodeSettings(ctx, frame); } finally { header.release(); } }
@Override public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest, HttpHeaders headers) { try { // Decode the HTTP2-Settings header and set the settings on the handler to make // sure everything is fine with the request. List<String> upgradeHeaders = upgradeRequest.headers().getAll(HTTP_UPGRADE_SETTINGS_HEADER); if (upgradeHeaders.isEmpty() || upgradeHeaders.size() > 1) { throw new IllegalArgumentException("There must be 1 and only 1 " + HTTP_UPGRADE_SETTINGS_HEADER + " header."); } settings = decodeSettingsHeader(ctx, upgradeHeaders.get(0)); // Everything looks good. return true; } catch (Throwable cause) { logger.info("Error during upgrade to HTTP/2", cause); return false; } }
@Override public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(new Http2ChannelHandlerBuilder(serverHandler).build()); } else { return null; } } });
/** * Decodes the settings header and returns a {@link Http2Settings} object. */ private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader) throws Http2Exception { ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8); try { // Decode the SETTINGS payload. ByteBuf payload = Base64.decode(header, URL_SAFE); // Create an HTTP/2 frame for the settings. ByteBuf frame = createSettingsFrame(ctx, payload); // Decode the SETTINGS frame and return the settings object. return decodeSettings(ctx, frame); } finally { header.release(); } }
@Override public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest, HttpHeaders headers) { try { // Decode the HTTP2-Settings header and set the settings on the handler to make // sure everything is fine with the request. List<String> upgradeHeaders = upgradeRequest.headers().getAll(HTTP_UPGRADE_SETTINGS_HEADER); if (upgradeHeaders.isEmpty() || upgradeHeaders.size() > 1) { throw new IllegalArgumentException("There must be 1 and only 1 " + HTTP_UPGRADE_SETTINGS_HEADER + " header."); } settings = decodeSettingsHeader(ctx, upgradeHeaders.get(0)); // Everything looks good. return true; } catch (Throwable cause) { logger.info("Error during upgrade to HTTP/2", cause); return false; } }
@Override public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(new Http2ChannelHandlerBuilder(serverHandler).build()); } else { return null; } } });
/** * Decodes the settings header and returns a {@link Http2Settings} object. */ private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader) throws Http2Exception { ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8); try { // Decode the SETTINGS payload. ByteBuf payload = Base64.decode(header, URL_SAFE); // Create an HTTP/2 frame for the settings. ByteBuf frame = createSettingsFrame(ctx, payload); // Decode the SETTINGS frame and return the settings object. return decodeSettings(ctx, frame); } finally { header.release(); } }
@Override public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest, HttpHeaders headers) { try { // Decode the HTTP2-Settings header and set the settings on the handler to make // sure everything is fine with the request. List<String> upgradeHeaders = upgradeRequest.headers().getAll(HTTP_UPGRADE_SETTINGS_HEADER); if (upgradeHeaders.isEmpty() || upgradeHeaders.size() > 1) { throw new IllegalArgumentException("There must be 1 and only 1 " + HTTP_UPGRADE_SETTINGS_HEADER + " header."); } settings = decodeSettingsHeader(ctx, upgradeHeaders.get(0)); // Everything looks good. return true; } catch (Throwable cause) { logger.info("Error during upgrade to HTTP/2", cause); return false; } }
private void configureHttp1WithUpgrade(ChannelHandlerContext ctx) { final ChannelPipeline p = ctx.pipeline(); final HttpServerCodec http1codec = new HttpServerCodec( config.http1MaxInitialLineLength(), config.http1MaxHeaderSize(), config.http1MaxChunkSize()); String baseName = name; assert baseName != null; baseName = addAfter(p, baseName, http1codec); baseName = addAfter(p, baseName, new HttpServerUpgradeHandler( http1codec, protocol -> { if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return null; } return new Http2ServerUpgradeCodec( newHttp2ConnectionHandler(p)); }, UPGRADE_REQUEST_MAX_LENGTH)); addAfter(p, baseName, new Http1RequestDecoder(config, ctx.channel(), SCHEME_HTTP, responseEncoder)); }
@Override public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container))); } else { return null; } } }));
HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, protocol -> AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol) ? new Http2ServerUpgradeCodec(helidonHandler) : null);
@Override public UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(connectionHandler); } else { return null; } } };
private void h2cOrHttp1(final ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline(); HttpServerCodec http1codec = http1Codec(); String baseName = name; baseName = addAfter(p, baseName, "codec", http1codec); baseName = addAfter(p, baseName, "h2upgrade", new HttpServerUpgradeHandler(http1codec, protocol -> { if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return null; } return new Http2ServerUpgradeCodec(newHttp2ConnectionHandler(p)); }, maxContentLength)); }
@Override @Nullable public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(multiplexCodec); } else { return null; } } }
@Override @Nullable public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(multiplexCodec); } else { return null; } } }
private void configureHttp1WithUpgrade(ChannelHandlerContext ctx) { final ChannelPipeline p = ctx.pipeline(); final HttpServerCodec http1codec = new HttpServerCodec(); String baseName = name; baseName = addAfter(p, baseName, http1codec); baseName = addAfter(p, baseName, new HttpServerUpgradeHandler( http1codec, protocol -> { if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return null; } return new Http2ServerUpgradeCodec(newHttp2ConnectionHandler(p)); }, UPGRADE_REQUEST_MAX_LENGTH)); addAfter(p, baseName, new Http1RequestDecoder(config, ctx.channel(), SCHEME_HTTP)); }
@Override public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(new Http2ChannelHandlerBuilder(serverHandler).build()); } else { return null; } } });
return new Http2ServerUpgradeCodec( Constants.HTTP2_SOURCE_CONNECTION_HANDLER, new Http2SourceConnectionHandlerBuilder(
HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, protocol -> AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol) ? new Http2ServerUpgradeCodec(helidonHandler) : null);