Search in sources :

Example 6 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class ProxyHandlerXForwardedForTestCase method setup.

@BeforeClass
public static void setup() throws Exception {
    port = DefaultServer.getHostPort("default");
    sslPort = port + 1;
    handlerPort = port + 2;
    DefaultServer.startSSLServer();
    ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.getClientSSLContext());
    server = Undertow.builder().addHttpsListener(handlerPort, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(jvmRoute("JSESSIONID", "s1", path().addPrefixPath("/x-forwarded", new XForwardedHandler()))).build();
    server.start();
}
Also used : UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) BeforeClass(org.junit.BeforeClass)

Example 7 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class Undertow method start.

public synchronized void start() {
    UndertowLogger.ROOT_LOGGER.debugf("starting undertow server %s", this);
    xnio = Xnio.getInstance(Undertow.class.getClassLoader());
    channels = new ArrayList<>();
    try {
        if (internalWorker) {
            worker = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, ioThreads).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, workerThreads).set(Options.WORKER_TASK_MAX_THREADS, workerThreads).set(Options.TCP_NODELAY, true).set(Options.CORK, true).addAll(workerOptions).getMap());
        }
        OptionMap socketOptions = OptionMap.builder().set(Options.WORKER_IO_THREADS, worker.getIoThreadCount()).set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).set(Options.BALANCING_TOKENS, 1).set(Options.BALANCING_CONNECTIONS, 2).set(Options.BACKLOG, 1000).addAll(this.socketOptions).getMap();
        OptionMap serverOptions = OptionMap.builder().set(UndertowOptions.NO_REQUEST_TIMEOUT, 60 * 1000).addAll(this.serverOptions).getMap();
        ByteBufferPool buffers = new DefaultByteBufferPool(directBuffers, bufferSize, -1, 4);
        listenerInfo = new ArrayList<>();
        for (ListenerConfig listener : listeners) {
            UndertowLogger.ROOT_LOGGER.debugf("Configuring listener with protocol %s for interface %s and port %s", listener.type, listener.host, listener.port);
            final HttpHandler rootHandler = listener.rootHandler != null ? listener.rootHandler : this.rootHandler;
            if (listener.type == ListenerType.AJP) {
                AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
                openListener.setRootHandler(rootHandler);
                ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides);
                server.resumeAccepts();
                channels.add(server);
                listenerInfo.add(new ListenerInfo("ajp", server.getLocalAddress(), null, openListener));
            } else {
                OptionMap undertowOptions = OptionMap.builder().set(UndertowOptions.BUFFER_PIPELINED_DATA, true).addAll(serverOptions).getMap();
                if (listener.type == ListenerType.HTTP) {
                    HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
                    openListener.setRootHandler(rootHandler);
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                    OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                    AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides);
                    server.resumeAccepts();
                    channels.add(server);
                    listenerInfo.add(new ListenerInfo("http", server.getLocalAddress(), null, openListener));
                } else if (listener.type == ListenerType.HTTPS) {
                    OpenListener openListener;
                    HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
                    httpOpenListener.setRootHandler(rootHandler);
                    boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
                    if (http2) {
                        AlpnOpenListener alpn = new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
                        if (http2) {
                            Http2OpenListener http2Listener = new Http2OpenListener(buffers, undertowOptions);
                            http2Listener.setRootHandler(rootHandler);
                            alpn.addProtocol(Http2OpenListener.HTTP2, http2Listener, 10);
                            alpn.addProtocol(Http2OpenListener.HTTP2_14, http2Listener, 7);
                        }
                        openListener = alpn;
                    } else {
                        openListener = httpOpenListener;
                    }
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                    XnioSsl xnioSsl;
                    if (listener.sslContext != null) {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), listener.sslContext);
                    } else {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), JsseSslUtils.createSSLContext(listener.keyManagers, listener.trustManagers, new SecureRandom(), OptionMap.create(Options.USE_DIRECT_BUFFERS, true)));
                    }
                    OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
                    AcceptingChannel<SslConnection> sslServer = xnioSsl.createSslConnectionServer(worker, new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides);
                    sslServer.resumeAccepts();
                    channels.add(sslServer);
                    listenerInfo.add(new ListenerInfo("https", sslServer.getLocalAddress(), listener.sslContext, openListener));
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) ByteBufferPool(io.undertow.connector.ByteBufferPool) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) HttpHandler(io.undertow.server.HttpHandler) ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) InetSocketAddress(java.net.InetSocketAddress) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OpenListener(io.undertow.server.OpenListener) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) SecureRandom(java.security.SecureRandom) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) StreamConnection(org.xnio.StreamConnection) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OptionMap(org.xnio.OptionMap) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 8 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method testHttp2ClientMultipleStreamsThreadSafety.

@Test
public void testHttp2ClientMultipleStreamsThreadSafety() throws IOException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
    //not actually a proxy test
    //but convent to put it here
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    final UndertowClient client = UndertowClient.getInstance();
    final ClientConnection connection = client.connect(new URI("https", null, DefaultServer.getHostAddress(), DefaultServer.getHostPort() + 1, "/", null, null), DefaultServer.getWorker(), ssl, DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    final ExecutorService service = Executors.newFixedThreadPool(10);
    try {
        Deque<FutureResult<String>> futures = new ArrayDeque<>();
        for (int i = 0; i < 100; ++i) {
            final FutureResult<String> future = new FutureResult<>();
            futures.add(future);
            service.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    ClientRequest cr = new ClientRequest().setMethod(Methods.GET).setPath("/path").setProtocol(Protocols.HTTP_1_1);
                    connection.sendRequest(cr, new ClientCallback<ClientExchange>() {

                        @Override
                        public void completed(ClientExchange result) {
                            result.setResponseListener(new ClientCallback<ClientExchange>() {

                                @Override
                                public void completed(ClientExchange result) {
                                    new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                        @Override
                                        protected void stringDone(String string) {
                                            future.setResult(string);
                                        }

                                        @Override
                                        protected void error(IOException e) {
                                            future.setException(e);
                                        }
                                    }.setup(result.getResponseChannel());
                                }

                                @Override
                                public void failed(IOException e) {
                                    future.setException(e);
                                }
                            });
                        }

                        @Override
                        public void failed(IOException e) {
                            future.setException(e);
                        }
                    });
                    return null;
                }
            });
        }
        while (!futures.isEmpty()) {
            FutureResult<String> future = futures.poll();
            Assert.assertNotEquals(IoFuture.Status.WAITING, future.getIoFuture().awaitInterruptibly(10, TimeUnit.SECONDS));
            Assert.assertEquals("/path", future.getIoFuture().get());
        }
    } finally {
        service.shutdownNow();
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) URI(java.net.URI) ArrayDeque(java.util.ArrayDeque) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FutureResult(org.xnio.FutureResult) ExecutorService(java.util.concurrent.ExecutorService) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 9 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    int port = DefaultServer.getHostPort("default");
    final HttpHandler handler1 = getRootHandler("s1", "server1");
    server1 = Undertow.builder().addHttpsListener(port + 1, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler1.handleRequest(exchange);
        }
    }).build();
    final HttpHandler handler2 = getRootHandler("s2", "server2");
    server2 = Undertow.builder().addHttpsListener(port + 2, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setSocketOption(Options.REUSE_ADDRESSES, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            handler2.handleRequest(exchange);
        }
    }).build();
    server1.start();
    server2.start();
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    DefaultServer.setRootHandler(new ProxyHandler(new LoadBalancingProxyClient().setConnectionsPerThread(4).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2", ssl, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)), 10000, ResponseCodeHandler.HANDLE_404, false, false, 2));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Http2ServerConnection(io.undertow.server.protocol.http2.Http2ServerConnection) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 10 with UndertowXnioSsl

use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.

the class LoadBalancingProxyHttpsTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    int port = DefaultServer.getHostPort("default");
    server1 = Undertow.builder().addHttpsListener(port + 1, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setSocketOption(Options.REUSE_ADDRESSES, true).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setHandler(getRootHandler("s1", "server1")).build();
    server2 = Undertow.builder().addHttpsListener(port + 2, DefaultServer.getHostAddress("default"), DefaultServer.getServerSslContext()).setServerOption(UndertowOptions.ENABLE_SPDY, false).setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, IDLE_TIMEOUT).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(getRootHandler("s2", "server2")).build();
    server1.start();
    server2.start();
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    DefaultServer.setRootHandler(new ProxyHandler(new LoadBalancingProxyClient().setConnectionsPerThread(4).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1", ssl).addHost(new URI("https", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2", ssl), 10000, ResponseCodeHandler.HANDLE_404, false, false, 2));
}
Also used : SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Aggregations

UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)14 URI (java.net.URI)8 IOException (java.io.IOException)6 SSLContext (javax.net.ssl.SSLContext)6 XnioSsl (org.xnio.ssl.XnioSsl)6 OptionMap (org.xnio.OptionMap)4 HttpHandler (io.undertow.server.HttpHandler)3 InetSocketAddress (java.net.InetSocketAddress)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 BeforeClass (org.junit.BeforeClass)3 Test (org.junit.Test)3 ClientConnection (io.undertow.client.ClientConnection)2 ClientRequest (io.undertow.client.ClientRequest)2 UndertowClient (io.undertow.client.UndertowClient)2 HttpServerExchange (io.undertow.server.HttpServerExchange)2 LoadBalancingProxyClient (io.undertow.server.handlers.proxy.LoadBalancingProxyClient)2 ProxyHandler (io.undertow.server.handlers.proxy.ProxyHandler)2 AjpOpenListener (io.undertow.server.protocol.ajp.AjpOpenListener)2 AlpnOpenListener (io.undertow.server.protocol.http.AlpnOpenListener)2 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)2