Skip to content

Commit

Permalink
Refactor to use a Future<Channel> in VertxSniHandler instead of a lis…
Browse files Browse the repository at this point in the history
…tener
  • Loading branch information
vietj committed Apr 24, 2017
1 parent 08318b8 commit ca01622
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/main/java/io/vertx/core/http/impl/HttpServerImpl.java
Expand Up @@ -257,7 +257,13 @@ public synchronized HttpServer listen(int port, String host, Handler<AsyncResult
applyConnectionOptions(bootstrap); applyConnectionOptions(bootstrap);
sslHelper.validate(vertx); sslHelper.validate(vertx);
bootstrap.childHandler(new ChannelInitializer<Channel>() { bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override ChannelHandlerContext ctx;
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
super.handlerAdded(ctx);
}
@Override
protected void initChannel(Channel ch) throws Exception { protected void initChannel(Channel ch) throws Exception {
if (requestStream.isPaused() || wsStream.isPaused()) { if (requestStream.isPaused() || wsStream.isPaused()) {
ch.close(); ch.close();
Expand All @@ -266,11 +272,13 @@ protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
if (sslHelper.isSSL()) { if (sslHelper.isSSL()) {
if (options.getSni()) { if (options.getSni()) {
pipeline.addLast(new VertxSniHandler(sslHelper, vertx, fut -> { VertxSniHandler sniHandler = new VertxSniHandler(ctx, sslHelper, vertx);
sniHandler.handshakeFuture().addListener(fut -> {
if (fut.isSuccess()) { if (fut.isSuccess()) {
postSSLConfig(pipeline); postSSLConfig(pipeline);
} }
})); });
pipeline.addLast(sniHandler);
} else { } else {
pipeline.addLast("ssl", new SslHandler(sslHelper.createEngine(vertx))); pipeline.addLast("ssl", new SslHandler(sslHelper.createEngine(vertx)));
postSSLConfig(pipeline); postSSLConfig(pipeline);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/io/vertx/core/net/impl/NetServerBase.java
Expand Up @@ -22,7 +22,6 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop; import io.netty.channel.EventLoop;
import io.netty.channel.FixedRecvByteBufAllocator; import io.netty.channel.FixedRecvByteBufAllocator;
Expand All @@ -31,7 +30,6 @@
import io.netty.channel.group.DefaultChannelGroup; import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import io.vertx.core.AsyncResult; import io.vertx.core.AsyncResult;
import io.vertx.core.Closeable; import io.vertx.core.Closeable;
Expand Down Expand Up @@ -328,21 +326,23 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
} }


if (sslHelper.isSSL()) { if (sslHelper.isSSL()) {
GenericFutureListener<io.netty.util.concurrent.Future<? super Channel>> handshakeListener = future -> { io.netty.util.concurrent.Future<Channel> handshakeFuture;
if (options.getSni()) {
VertxSniHandler sniHandler = new VertxSniHandler(ctx, sslHelper, vertx);
handshakeFuture = sniHandler.handshakeFuture();
ch.pipeline().addFirst("ssl", sniHandler);
} else {
SslHandler sslHandler = new SslHandler(sslHelper.createEngine(vertx));
handshakeFuture = sslHandler.handshakeFuture();
ch.pipeline().addFirst("ssl", sslHandler);
}
handshakeFuture.addListener(future -> {
if (future.isSuccess()) { if (future.isSuccess()) {
connected(ch, handler); connected(ch, handler);
} else { } else {
log.error("Client from origin " + ch.remoteAddress() + " failed to connect over ssl: " + future.cause()); log.error("Client from origin " + ch.remoteAddress() + " failed to connect over ssl: " + future.cause());
} }
}; });
ChannelOutboundHandler sslHandler;
if (options.getSni()) {
sslHandler = new VertxSniHandler(sslHelper, vertx, handshakeListener);
} else {
sslHandler = new SslHandler(sslHelper.createEngine(vertx));
((SslHandler)sslHandler).handshakeFuture().addListener(handshakeListener);
}
ch.pipeline().addFirst("ssl", sslHandler);
} else { } else {
connected(ch, handler); connected(ch, handler);
} }
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/io/vertx/core/net/impl/VertxSniHandler.java
Expand Up @@ -23,6 +23,7 @@
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import io.vertx.core.impl.VertxInternal; import io.vertx.core.impl.VertxInternal;


import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngine;
Expand All @@ -34,15 +35,19 @@ public class VertxSniHandler extends SniHandler {


private final SSLHelper helper; private final SSLHelper helper;
private final VertxInternal vertx; private final VertxInternal vertx;
private final GenericFutureListener<Future<? super Channel>> handshakeListener; private final Promise<Channel> handshakeFuture;


public VertxSniHandler(SSLHelper helper, VertxInternal vertx, GenericFutureListener<Future<? super Channel>> handshakeListener) { public VertxSniHandler(ChannelHandlerContext ctx, SSLHelper helper, VertxInternal vertx) {
super(input -> { super(input -> {
return helper.getContext(vertx, input); return helper.getContext(vertx, input);
}); });
this.helper = helper; this.helper = helper;
this.vertx = vertx; this.vertx = vertx;
this.handshakeListener = handshakeListener; this.handshakeFuture = ctx.executor().newPromise();
}

public Future<Channel> handshakeFuture() {
return handshakeFuture;
} }


@Override @Override
Expand All @@ -53,7 +58,13 @@ protected void replaceHandler(ChannelHandlerContext ctx, String hostname, SslCon
sslHandler = new SslHandler(engine); sslHandler = new SslHandler(engine);
ctx.pipeline().replace(this, "ssl", sslHandler); ctx.pipeline().replace(this, "ssl", sslHandler);
Future<Channel> fut = sslHandler.handshakeFuture(); Future<Channel> fut = sslHandler.handshakeFuture();
fut.addListener(handshakeListener); fut.addListener(future -> {
if (future.isSuccess()) {
handshakeFuture.setSuccess(ctx.channel());
} else {
handshakeFuture.setFailure(future.cause());
}
});
sslHandler = null; sslHandler = null;
} finally { } finally {
// Since the SslHandler was not inserted into the pipeline the ownership of the SSLEngine was not // Since the SslHandler was not inserted into the pipeline the ownership of the SSLEngine was not
Expand Down

0 comments on commit ca01622

Please sign in to comment.