Search in sources :

Example 11 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;
    } 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 12 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 13 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);
                    }
                }
            });
            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());
    }
}
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)

Example 14 with StreamConnection

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

the class Http2UpgradeHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final String upgrade = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
    if (upgrade != null && upgradeStrings.contains(upgrade)) {
        String settings = exchange.getRequestHeaders().getFirst("HTTP2-Settings");
        if (settings != null) {
            //required by spec
            final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings);
            exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade);
            exchange.upgradeChannel(new HttpUpgradeListener() {

                @Override
                public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                    OptionMap undertowOptions = exchange.getConnection().getUndertowOptions();
                    Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions);
                    Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() {

                        @Override
                        public void handleRequest(HttpServerExchange exchange) throws Exception {
                            //as the request was only to create the initial request
                            if (exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) {
                                exchange.endExchange();
                                return;
                            }
                            exchange.setProtocol(Protocols.HTTP_2_0);
                            next.handleRequest(exchange);
                        }
                    }, undertowOptions, exchange.getConnection().getBufferSize(), null);
                    channel.getReceiveSetter().set(receiveListener);
                    receiveListener.handleInitialRequest(exchange, channel);
                    channel.resumeReceives();
                }
            });
            return;
        }
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) OptionMap(org.xnio.OptionMap) Http2Channel(io.undertow.protocols.http2.Http2Channel) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) ByteBuffer(java.nio.ByteBuffer)

Example 15 with StreamConnection

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

the class WebSocketProtocolHandshakeHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (!exchange.getRequestMethod().equals(Methods.GET)) {
        // Only GET is supported to start the handshake
        next.handleRequest(exchange);
        return;
    }
    final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
    Handshake handshaker = null;
    for (Handshake method : handshakes) {
        if (method.matches(facade)) {
            handshaker = method;
            break;
        }
    }
    if (handshaker == null) {
        next.handleRequest(exchange);
    } else {
        WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
        final Handshake selected = handshaker;
        if (upgradeListener == null) {
            exchange.upgradeChannel(new HttpUpgradeListener() {

                @Override
                public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                    WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
                    peerConnections.add(channel);
                    callback.onConnect(facade, channel);
                }
            });
        } else {
            exchange.upgradeChannel(upgradeListener);
        }
        handshaker.handshake(facade);
    }
}
Also used : AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) Handshake(io.undertow.websockets.core.protocol.Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake)

Aggregations

StreamConnection (org.xnio.StreamConnection)20 IOException (java.io.IOException)9 HttpServerExchange (io.undertow.server.HttpServerExchange)8 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)8 InetSocketAddress (java.net.InetSocketAddress)7 ChannelListener (org.xnio.ChannelListener)6 OptionMap (org.xnio.OptionMap)5 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)4 Handshake (io.undertow.websockets.core.protocol.Handshake)4 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)3 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)3 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)3 Http2Channel (io.undertow.protocols.http2.Http2Channel)2 HttpHandler (io.undertow.server.HttpHandler)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)2 DeploymentManager (io.undertow.servlet.api.DeploymentManager)2 FilterInfo (io.undertow.servlet.api.FilterInfo)2 ServletContainer (io.undertow.servlet.api.ServletContainer)2 ServletWebSocketHttpExchange (io.undertow.servlet.websockets.ServletWebSocketHttpExchange)2 Hybi07Handshake (io.undertow.websockets.core.protocol.version07.Hybi07Handshake)2