Search in sources :

Example 21 with StreamConnection

use of org.xnio.StreamConnection in project wildfly by wildfly.

the class DistributableSessionManagerTestCase method createSessionResponseCommitted.

@Test
public void createSessionResponseCommitted() {
    // Ugh - all this, just to get HttpServerExchange.isResponseStarted() to return true
    Configurable configurable = mock(Configurable.class);
    StreamSourceConduit sourceConduit = mock(StreamSourceConduit.class);
    ConduitStreamSourceChannel sourceChannel = new ConduitStreamSourceChannel(configurable, sourceConduit);
    StreamSinkConduit sinkConduit = mock(StreamSinkConduit.class);
    ConduitStreamSinkChannel sinkChannel = new ConduitStreamSinkChannel(configurable, sinkConduit);
    StreamConnection stream = mock(StreamConnection.class);
    when(stream.getSourceChannel()).thenReturn(sourceChannel);
    when(stream.getSinkChannel()).thenReturn(sinkChannel);
    ByteBufferPool bufferPool = mock(ByteBufferPool.class);
    HttpHandler handler = mock(HttpHandler.class);
    HttpServerConnection connection = new HttpServerConnection(stream, bufferPool, handler, OptionMap.create(UndertowOptions.ALWAYS_SET_DATE, false), 0, null);
    HttpServerExchange exchange = new HttpServerExchange(connection);
    exchange.setProtocol(Protocols.HTTP_1_1);
    exchange.getResponseChannel();
    SessionConfig config = mock(SessionConfig.class);
    Assert.assertThrows(IllegalStateException.class, () -> this.adapter.createSession(exchange, config));
}
Also used : ByteBufferPool(io.undertow.connector.ByteBufferPool) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpServerConnection(io.undertow.server.protocol.http.HttpServerConnection) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) SessionConfig(io.undertow.server.session.SessionConfig) Configurable(org.xnio.channels.Configurable) ConduitStreamSourceChannel(org.xnio.conduits.ConduitStreamSourceChannel) StreamConnection(org.xnio.StreamConnection) StreamSourceConduit(org.xnio.conduits.StreamSourceConduit) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel) Test(org.junit.Test)

Example 22 with StreamConnection

use of org.xnio.StreamConnection in project undertow by undertow-io.

the class Undertow method start.

public synchronized void start() {
    UndertowLogger.ROOT_LOGGER.infof("starting server: %s", Version.getFullVersionString());
    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 = this.byteBufferPool;
        if (buffers == null) {
            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;
            OptionMap socketOptionsWithOverrides = OptionMap.builder().addAll(socketOptions).addAll(listener.overrideSocketOptions).getMap();
            if (listener.type == ListenerType.AJP) {
                AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
                openListener.setRootHandler(rootHandler);
                final ChannelListener<StreamConnection> finalListener;
                if (listener.useProxyProtocol) {
                    finalListener = new ProxyProtocolOpenListener(openListener, null, buffers, OptionMap.EMPTY);
                } else {
                    finalListener = openListener;
                }
                ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(finalListener);
                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(), openListener, null, server));
            } else {
                OptionMap undertowOptions = OptionMap.builder().set(UndertowOptions.BUFFER_PIPELINED_DATA, true).addAll(serverOptions).getMap();
                boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
                if (listener.type == ListenerType.HTTP) {
                    HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
                    HttpHandler handler = rootHandler;
                    if (http2) {
                        handler = new Http2UpgradeHandler(handler);
                    }
                    openListener.setRootHandler(handler);
                    final ChannelListener<StreamConnection> finalListener;
                    if (listener.useProxyProtocol) {
                        finalListener = new ProxyProtocolOpenListener(openListener, null, buffers, OptionMap.EMPTY);
                    } else {
                        finalListener = openListener;
                    }
                    ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(finalListener);
                    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(), openListener, null, server));
                } else if (listener.type == ListenerType.HTTPS) {
                    OpenListener openListener;
                    HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
                    httpOpenListener.setRootHandler(rootHandler);
                    if (http2) {
                        AlpnOpenListener alpn = new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
                        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;
                    }
                    UndertowXnioSsl xnioSsl;
                    if (listener.sslContext != null) {
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), listener.sslContext, sslEngineDelegatedTaskExecutor);
                    } else {
                        OptionMap.Builder builder = OptionMap.builder().addAll(socketOptionsWithOverrides);
                        if (!socketOptionsWithOverrides.contains(Options.SSL_PROTOCOL)) {
                            builder.set(Options.SSL_PROTOCOL, "TLSv1.2");
                        }
                        xnioSsl = new UndertowXnioSsl(xnio, OptionMap.create(Options.USE_DIRECT_BUFFERS, true), JsseSslUtils.createSSLContext(listener.keyManagers, listener.trustManagers, new SecureRandom(), builder.getMap()), sslEngineDelegatedTaskExecutor);
                    }
                    AcceptingChannel<? extends StreamConnection> sslServer;
                    if (listener.useProxyProtocol) {
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(new ProxyProtocolOpenListener(openListener, xnioSsl, buffers, socketOptionsWithOverrides));
                        sslServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides);
                    } else {
                        ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
                        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(), openListener, xnioSsl, sslServer));
                }
            }
        }
    } catch (Exception e) {
        if (internalWorker && worker != null) {
            worker.shutdownNow();
        }
        throw new RuntimeException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) ByteBufferPool(io.undertow.connector.ByteBufferPool) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) ChannelListener(org.xnio.ChannelListener) InetSocketAddress(java.net.InetSocketAddress) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) ProxyProtocolOpenListener(io.undertow.server.protocol.proxy.ProxyProtocolOpenListener) HttpHandler(io.undertow.server.HttpHandler) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) Http2UpgradeHandler(io.undertow.server.protocol.http2.Http2UpgradeHandler) Http2OpenListener(io.undertow.server.protocol.http2.Http2OpenListener) AlpnOpenListener(io.undertow.server.protocol.http.AlpnOpenListener) ProxyProtocolOpenListener(io.undertow.server.protocol.proxy.ProxyProtocolOpenListener) AjpOpenListener(io.undertow.server.protocol.ajp.AjpOpenListener) OpenListener(io.undertow.server.OpenListener) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) SecureRandom(java.security.SecureRandom) StreamConnection(org.xnio.StreamConnection) IOException(java.io.IOException) 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 23 with StreamConnection

use of org.xnio.StreamConnection in project undertow by undertow-io.

the class Http2ClearClientProvider method connect.

@Override
public void connect(final ClientCallback<ClientConnection> listener, final InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    final URI upgradeUri;
    try {
        upgradeUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
    } catch (URISyntaxException e) {
        listener.failed(new IOException(e));
        return;
    }
    if (bindAddress != null) {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    } else {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {

            @Override
            public void handleEvent(StreamConnection channel) {
                Map<String, String> headers = createHeaders(options, bufferPool, uri);
                HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
            }
        }, new ChannelListener<BoundChannel>() {

            @Override
            public void handleEvent(BoundChannel channel) {
            }
        }, options).addNotifier(new FailedNotifier(listener), null);
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StreamConnection(org.xnio.StreamConnection) URI(java.net.URI) BoundChannel(org.xnio.channels.BoundChannel)

Example 24 with StreamConnection

use of org.xnio.StreamConnection in project undertow by undertow-io.

the class HttpClientConnection method doHttp2Upgrade.

protected void doHttp2Upgrade() {
    try {
        StreamConnection connectedStreamChannel = this.performUpgrade();
        Http2Channel http2Channel = new Http2Channel(connectedStreamChannel, null, bufferPool, null, true, true, options);
        Http2ClientConnection http2ClientConnection = new Http2ClientConnection(http2Channel, currentRequest.getResponseCallback(), currentRequest.getRequest(), currentRequest.getRequest().getRequestHeaders().getFirst(Headers.HOST), clientStatistics, false);
        http2ClientConnection.getCloseSetter().set(new ChannelListener<ClientConnection>() {

            @Override
            public void handleEvent(ClientConnection channel) {
                ChannelListeners.invokeChannelListener(HttpClientConnection.this, HttpClientConnection.this.closeSetter.get());
            }
        });
        http2Delegate = http2ClientConnection;
        // make sure the read listener is immediately invoked, as it may not happen if data is pushed back
        connectedStreamChannel.getSourceChannel().wakeupReads();
        currentRequest = null;
        pendingResponse = null;
    } catch (IOException e) {
        UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
        safeClose(this);
    }
}
Also used : Http2ClientConnection(io.undertow.client.http2.Http2ClientConnection) Http2Channel(io.undertow.protocols.http2.Http2Channel) Http2ClientConnection(io.undertow.client.http2.Http2ClientConnection) ClientConnection(io.undertow.client.ClientConnection) IOException(java.io.IOException) StreamConnection(org.xnio.StreamConnection)

Example 25 with StreamConnection

use of org.xnio.StreamConnection in project undertow by undertow-io.

the class PipeliningBufferingStreamSinkConduit method performFlush.

void performFlush(final HttpServerExchange exchange, final HttpServerConnection connection) {
    try {
        final HttpServerConnection.ConduitState oldState = connection.resetChannel();
        if (!flushPipelinedData()) {
            final StreamConnection channel = connection.getChannel();
            channel.getSinkChannel().setWriteListener(new ChannelListener<Channel>() {

                @Override
                public void handleEvent(Channel c) {
                    try {
                        if (flushPipelinedData()) {
                            channel.getSinkChannel().setWriteListener(null);
                            channel.getSinkChannel().suspendWrites();
                            connection.restoreChannel(oldState);
                            connection.getReadListener().exchangeComplete(exchange);
                        }
                    } catch (IOException e) {
                        UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
                        IoUtils.safeClose(channel);
                    } catch (Throwable t) {
                        UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t);
                        IoUtils.safeClose(channel);
                    }
                }
            });
            connection.getChannel().getSinkChannel().resumeWrites();
            return;
        } else {
            connection.restoreChannel(oldState);
            connection.getReadListener().exchangeComplete(exchange);
        }
    } catch (IOException e) {
        UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
        IoUtils.safeClose(connection.getChannel());
    } catch (Throwable t) {
        UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t);
        IoUtils.safeClose(connection.getChannel());
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) ConduitWritableByteChannel(org.xnio.conduits.ConduitWritableByteChannel) FileChannel(java.nio.channels.FileChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException) StreamConnection(org.xnio.StreamConnection)

Aggregations

StreamConnection (org.xnio.StreamConnection)26 IOException (java.io.IOException)11 HttpServerExchange (io.undertow.server.HttpServerExchange)10 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)8 InetSocketAddress (java.net.InetSocketAddress)7 ChannelListener (org.xnio.ChannelListener)7 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)7 OptionMap (org.xnio.OptionMap)6 HttpHandler (io.undertow.server.HttpHandler)4 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)4 Handshake (io.undertow.websockets.core.protocol.Handshake)4 ConduitStreamSourceChannel (org.xnio.conduits.ConduitStreamSourceChannel)4 ByteBufferPool (io.undertow.connector.ByteBufferPool)3 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)3 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)3 HttpServerConnection (io.undertow.server.protocol.http.HttpServerConnection)3 ByteBuffer (java.nio.ByteBuffer)3 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)2 Http2Channel (io.undertow.protocols.http2.Http2Channel)2 SessionConfig (io.undertow.server.session.SessionConfig)2