Search in sources :

Example 1 with SniHandler

use of io.netty.handler.ssl.SniHandler in project vert.x by eclipse.

the class NetSocketImpl method upgradeToSsl.

@Override
public NetSocket upgradeToSsl(String serverName, Handler<AsyncResult<Void>> handler) {
    ChannelOutboundHandler sslHandler = (ChannelOutboundHandler) chctx.pipeline().get("ssl");
    if (sslHandler == null) {
        ChannelPromise p = chctx.newPromise();
        chctx.pipeline().addFirst("handshaker", new SslHandshakeCompletionHandler(p));
        p.addListener(future -> {
            if (handler != null) {
                AsyncResult<Void> res;
                if (future.isSuccess()) {
                    res = Future.succeededFuture();
                } else {
                    res = Future.failedFuture(future.cause());
                }
                context.emit(res, handler);
            }
        });
        if (remoteAddress != null) {
            sslHandler = new SslHandler(helper.createEngine(vertx, remoteAddress, serverName, false));
            ((SslHandler) sslHandler).setHandshakeTimeout(helper.getSslHandshakeTimeout(), helper.getSslHandshakeTimeoutUnit());
        } else {
            if (helper.isSNI()) {
                sslHandler = new SniHandler(helper.serverNameMapper(vertx));
            } else {
                sslHandler = new SslHandler(helper.createEngine(vertx));
                ((SslHandler) sslHandler).setHandshakeTimeout(helper.getSslHandshakeTimeout(), helper.getSslHandshakeTimeoutUnit());
            }
        }
        chctx.pipeline().addFirst("ssl", sslHandler);
    }
    return this;
}
Also used : SniHandler(io.netty.handler.ssl.SniHandler) SslHandler(io.netty.handler.ssl.SslHandler)

Example 2 with SniHandler

use of io.netty.handler.ssl.SniHandler in project vert.x by eclipse.

the class HttpServerWorker method configurePipeline.

private void configurePipeline(Channel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslHelper.isSSL()) {
        if (options.isSni()) {
            SniHandler sniHandler = new SniHandler(sslHelper.serverNameMapper(vertx));
            pipeline.addLast(sniHandler);
        } else {
            SslHandler handler = new SslHandler(sslHelper.createEngine(vertx));
            handler.setHandshakeTimeout(sslHelper.getSslHandshakeTimeout(), sslHelper.getSslHandshakeTimeoutUnit());
            pipeline.addLast("ssl", handler);
        }
        ChannelPromise p = ch.newPromise();
        pipeline.addLast("handshaker", new SslHandshakeCompletionHandler(p));
        p.addListener(future -> {
            if (future.isSuccess()) {
                if (options.isUseAlpn()) {
                    SslHandler sslHandler = pipeline.get(SslHandler.class);
                    String protocol = sslHandler.applicationProtocol();
                    if ("h2".equals(protocol)) {
                        handleHttp2(ch);
                    } else {
                        handleHttp1(ch);
                    }
                } else {
                    handleHttp1(ch);
                }
            } else {
                handleException(future.cause());
            }
        });
    } else {
        if (disableH2C) {
            handleHttp1(ch);
        } else {
            IdleStateHandler idle;
            int idleTimeout = options.getIdleTimeout();
            int readIdleTimeout = options.getReadIdleTimeout();
            int writeIdleTimeout = options.getWriteIdleTimeout();
            if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
                pipeline.addLast("idle", idle = new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
            } else {
                idle = null;
            }
            // Handler that detects whether the HTTP/2 connection preface or just process the request
            // with the HTTP 1.x pipeline to support H2C with prior knowledge, i.e a client that connects
            // and uses HTTP/2 in clear text directly without an HTTP upgrade.
            pipeline.addLast(new Http1xOrH2CHandler() {

                @Override
                protected void configure(ChannelHandlerContext ctx, boolean h2c) {
                    if (idle != null) {
                        // It will be re-added but this way we don't need to pay attention to order
                        pipeline.remove(idle);
                    }
                    if (h2c) {
                        handleHttp2(ctx.channel());
                    } else {
                        handleHttp1(ch);
                    }
                }

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == IdleState.ALL_IDLE) {
                        ctx.close();
                    }
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    super.exceptionCaught(ctx, cause);
                    handleException(cause);
                }
            });
        }
    }
}
Also used : SniHandler(io.netty.handler.ssl.SniHandler) SslHandler(io.netty.handler.ssl.SslHandler) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) SslHandshakeCompletionHandler(io.vertx.core.net.impl.SslHandshakeCompletionHandler)

Aggregations

SniHandler (io.netty.handler.ssl.SniHandler)2 SslHandler (io.netty.handler.ssl.SslHandler)2 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 SslHandshakeCompletionHandler (io.vertx.core.net.impl.SslHandshakeCompletionHandler)1