Skip to content

Commit

Permalink
support Application-Layer Protocol Negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
walklown committed May 9, 2024
1 parent 7b9da20 commit 6016080
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
if (providerConnectionConfig != null && isSsl(in)) {
enableSsl(ctx, providerConnectionConfig);
} else {
invokeProtocol(ctx, url, channel, in);
detectProtocol(ctx, url, channel, in);
}
}

Expand All @@ -140,7 +140,7 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) thr
}
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
ByteBuf in = ctx.alloc().buffer();
invokeProtocol(ctx, url, channel, in);
detectProtocol(ctx, url, channel, in);
}
});
p.remove(this);
Expand All @@ -154,7 +154,7 @@ private boolean isSsl(ByteBuf buf) {
return false;
}

private void invokeProtocol(ChannelHandlerContext ctx, URL url, NettyChannel channel, ByteBuf in) {
private void detectProtocol(ChannelHandlerContext ctx, URL url, NettyChannel channel, ByteBuf in) {
Set<String> supportedProtocolNames = new HashSet<>(protocols.keySet());
supportedProtocolNames.retainAll(urlMapper.keySet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,20 @@ private void configurerHttp1Handlers(URL url, List<ChannelHandler> handlers) {
handlers.add(new ChannelHandlerPretender(new HttpServerUpgradeHandler(
sourceCodec,
protocol -> {
if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
// Not upgrade request
return null;
if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
Configuration config =
ConfigurationUtils.getGlobalConfiguration(url.getOrDefaultApplicationModel());
return new Http2ServerUpgradeCodec(
buildHttp2FrameCodec(config, url.getOrDefaultApplicationModel()),
new HttpServerAfterUpgradeHandler(),
new HttpWriteQueueHandler(),
new FlushConsolidationHandler(64, true),
new TripleServerConnectionHandler(),
buildHttp2MultiplexHandler(url),
new TripleTailHandler());
}
return buildHttp2ServerUpgradeCodec(url);
// Not upgrade request
return null;
},
Integer.MAX_VALUE)));
// If the upgrade was successful, remove the message from the output list
Expand All @@ -171,10 +180,8 @@ private void configurerHttp1Handlers(URL url, List<ChannelHandler> handlers) {
url, frameworkModel, DefaultHttp11ServerTransportListenerFactory.INSTANCE)));
}

private Http2ServerUpgradeCodec buildHttp2ServerUpgradeCodec(URL url) {
Configuration config = ConfigurationUtils.getGlobalConfiguration(url.getOrDefaultApplicationModel());
final Http2FrameCodec codec = buildHttp2FrameCodec(config, url.getOrDefaultApplicationModel());
final Http2MultiplexHandler handler = new Http2MultiplexHandler(new ChannelInitializer<Http2StreamChannel>() {
private Http2MultiplexHandler buildHttp2MultiplexHandler(URL url) {
return new Http2MultiplexHandler(new ChannelInitializer<Http2StreamChannel>() {
@Override
protected void initChannel(Http2StreamChannel ch) {
final ChannelPipeline p = ch.pipeline();
Expand All @@ -183,15 +190,6 @@ protected void initChannel(Http2StreamChannel ch) {
url, frameworkModel, GenericHttp2ServerTransportListenerFactory.INSTANCE));
}
});

return new Http2ServerUpgradeCodec(
codec,
new HttpServerAfterUpgradeHandler(),
new HttpWriteQueueHandler(),
new FlushConsolidationHandler(64, true),
new TripleServerConnectionHandler(),
handler,
new TripleTailHandler());
}

private void configurerHttp2Handlers(URL url, List<ChannelHandler> handlers) {
Expand Down

0 comments on commit 6016080

Please sign in to comment.