Skip to content

Commit

Permalink
DefaultNettyConnection.initChannel() does not work on channels that a…
Browse files Browse the repository at this point in the history
…lready completed a SSL handshake.
  • Loading branch information
thomdev committed Jul 28, 2023
1 parent 67308bd commit e6ad6aa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected void handleSubscribe(final Subscriber<? super H2ClientParentConnection
initializer.init(channel);
pipeline = channel.pipeline();
parentChannelInitializer = new DefaultH2ClientParentConnection(connection, subscriber,
delayedCancellable, NettyPipelineSslUtils.isSslEnabled(pipeline),
delayedCancellable, NettyPipelineSslUtils.hasPendingSslHandshake(pipeline),
allowDropTrailersReadFromTransport, config.headersFactory(), reqRespFactory, observer);
} catch (Throwable cause) {
close(channel, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void handleSubscribe(final Subscriber<? super H2ServerParentConnection
pipeline = channel.pipeline();

parentChannelInitializer = new DefaultH2ServerParentConnection(connection, subscriber,
delayedCancellable, NettyPipelineSslUtils.isSslEnabled(pipeline), observer);
delayedCancellable, NettyPipelineSslUtils.hasPendingSslHandshake(pipeline), observer);

new H2ServerParentChannelInitializer(h2ServerConfig,
new io.netty.channel.ChannelInitializer<Http2StreamChannel>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ protected void handleSubscribe(
initializer.init(channel);
ChannelPipeline pipeline = connection.channel().pipeline();
nettyInboundHandler = new NettyToStChannelHandler<>(connection, subscriber,
delayedCancellable, NettyPipelineSslUtils.isSslEnabled(pipeline), observer);
delayedCancellable, NettyPipelineSslUtils.hasPendingSslHandshake(pipeline), observer);
} catch (Throwable cause) {
close(channel, cause);
deliverErrorFromSource(subscriber, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import io.servicetalk.transport.api.ConnectionObserver.SecurityHandshakeObserver;
import io.servicetalk.transport.netty.internal.ConnectionObserverInitializer.ConnectionObserverHandler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.ssl.SniHandler;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,11 +47,36 @@ private NettyPipelineSslUtils() {
*
* @param pipeline The pipeline to check.
* @return {@code true} if the pipeline is configured to use SSL/TLS.
*
* @deprecated Use {@link #hasPendingSslHandshake(ChannelPipeline)} instead
*/
@Deprecated
public static boolean isSslEnabled(ChannelPipeline pipeline) {
return pipeline.get(SslHandler.class) != null || pipeline.get(SniHandler.class) != null;
}

/**
* Determine if the {@link ChannelPipeline} has a pending SSL/TLS handshake.
*
* @param pipeline The pipeline to check.
* @return {@code true} if the pipeline is configured to use SSL/TLS, but the handshake has not completed yet.
* @throws Throwable Handshake exception, if the handshake failed.
*/
public static boolean hasPendingSslHandshake(ChannelPipeline pipeline) throws Throwable {
final SslHandler sslHandler = pipeline.get(SslHandler.class);
if (sslHandler != null) {
final Future<Channel> handshakeFuture = sslHandler.handshakeFuture();
if (handshakeFuture.isDone()) {
if (handshakeFuture.cause() != null) {
throw handshakeFuture.cause();
}
return false;
}
return true;
}
return pipeline.get(SniHandler.class) != null;
}

/**
* Extracts the {@link SSLSession} from the {@link ChannelPipeline} if the {@link SslHandshakeCompletionEvent}
* is successful and reports the result to {@link SecurityHandshakeObserver} if available.
Expand Down

0 comments on commit e6ad6aa

Please sign in to comment.