Search in sources :

Example 1 with Http2Channel

use of io.undertow.protocols.http2.Http2Channel in project undertow by undertow-io.

the class Http2PriorKnowledgeClientProvider method handleConnected.

private void handleConnected(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final ByteBufferPool bufferPool, final OptionMap options, final String defaultHost) {
    try {
        final ClientStatisticsImpl clientStatistics;
        //first we set up statistics, if required
        if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) {
            clientStatistics = new ClientStatisticsImpl();
            connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() {

                @Override
                public void activity(long bytes) {
                    clientStatistics.written += bytes;
                }
            }));
            connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() {

                @Override
                public void activity(long bytes) {
                    clientStatistics.read += bytes;
                }
            }));
        } else {
            clientStatistics = null;
        }
        final ByteBuffer pri = ByteBuffer.wrap(PRI_REQUEST);
        pri.flip();
        ConduitStreamSinkChannel sink = connection.getSinkChannel();
        sink.write(pri);
        if (pri.hasRemaining()) {
            sink.setWriteListener(new ChannelListener<ConduitStreamSinkChannel>() {

                @Override
                public void handleEvent(ConduitStreamSinkChannel channel) {
                    try {
                        channel.write(pri);
                        if (pri.hasRemaining()) {
                            return;
                        }
                        listener.completed(new Http2ClientConnection(new Http2Channel(connection, null, bufferPool, null, true, false, options), false, defaultHost, clientStatistics, false));
                    } catch (IOException e) {
                        listener.failed(e);
                    }
                }
            });
            return;
        }
        listener.completed(new Http2ClientConnection(new Http2Channel(connection, null, bufferPool, null, true, false, options), false, defaultHost, clientStatistics, false));
    } catch (IOException e) {
        listener.failed(e);
    }
}
Also used : Http2Channel(io.undertow.protocols.http2.Http2Channel) ByteActivityCallback(io.undertow.conduits.ByteActivityCallback) IOException(java.io.IOException) BytesSentStreamSinkConduit(io.undertow.conduits.BytesSentStreamSinkConduit) BytesReceivedStreamSourceConduit(io.undertow.conduits.BytesReceivedStreamSourceConduit) ByteBuffer(java.nio.ByteBuffer) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel)

Example 2 with Http2Channel

use of io.undertow.protocols.http2.Http2Channel in project undertow by undertow-io.

the class Http2OpenListener method handleEvent.

public void handleEvent(final StreamConnection channel, PooledByteBuffer buffer) {
    if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) {
        UndertowLogger.REQUEST_LOGGER.tracef("Opened HTTP/2 connection with %s", channel.getPeerAddress());
    }
    //cool, we have a Http2 connection.
    Http2Channel http2Channel = new Http2Channel(channel, protocol, bufferPool, buffer, false, false, undertowOptions);
    Integer idleTimeout = undertowOptions.get(UndertowOptions.IDLE_TIMEOUT);
    if (idleTimeout != null && idleTimeout > 0) {
        http2Channel.setIdleTimeout(idleTimeout);
    }
    if (statisticsEnabled) {
        channel.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(channel.getSinkChannel().getConduit(), connectorStatistics.sentAccumulator()));
        channel.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(channel.getSourceChannel().getConduit(), connectorStatistics.receivedAccumulator()));
        connectorStatistics.incrementConnectionCount();
        http2Channel.addCloseTask(closeTask);
    }
    http2Channel.getReceiveSetter().set(new Http2ReceiveListener(rootHandler, getUndertowOptions(), bufferSize, connectorStatistics));
    http2Channel.resumeReceives();
}
Also used : Http2Channel(io.undertow.protocols.http2.Http2Channel) BytesSentStreamSinkConduit(io.undertow.conduits.BytesSentStreamSinkConduit) BytesReceivedStreamSourceConduit(io.undertow.conduits.BytesReceivedStreamSourceConduit)

Example 3 with Http2Channel

use of io.undertow.protocols.http2.Http2Channel 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 4 with Http2Channel

use of io.undertow.protocols.http2.Http2Channel in project undertow by undertow-io.

the class Http2ClientProvider method createHttp2Channel.

private static Http2ClientConnection createHttp2Channel(StreamConnection connection, ByteBufferPool bufferPool, OptionMap options, String defaultHost) {
    final ClientStatisticsImpl clientStatistics;
    //first we set up statistics, if required
    if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) {
        clientStatistics = new ClientStatisticsImpl();
        connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() {

            @Override
            public void activity(long bytes) {
                clientStatistics.written += bytes;
            }
        }));
        connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() {

            @Override
            public void activity(long bytes) {
                clientStatistics.read += bytes;
            }
        }));
    } else {
        clientStatistics = null;
    }
    Http2Channel http2Channel = new Http2Channel(connection, null, bufferPool, null, true, false, options);
    return new Http2ClientConnection(http2Channel, false, defaultHost, clientStatistics, true);
}
Also used : Http2Channel(io.undertow.protocols.http2.Http2Channel) ByteActivityCallback(io.undertow.conduits.ByteActivityCallback) BytesSentStreamSinkConduit(io.undertow.conduits.BytesSentStreamSinkConduit) BytesReceivedStreamSourceConduit(io.undertow.conduits.BytesReceivedStreamSourceConduit)

Example 5 with Http2Channel

use of io.undertow.protocols.http2.Http2Channel in project undertow by undertow-io.

the class Http2ReceiveListener method handleInitialRequest.

/**
     * Handles the initial request when the exchange was started by a HTTP ugprade.
     *
     *
     * @param initial The initial upgrade request that started the HTTP2 connection
     */
void handleInitialRequest(HttpServerExchange initial, Http2Channel channel) {
    //we have a request
    Http2HeadersStreamSinkChannel sink = channel.createInitialUpgradeResponseStream();
    final Http2ServerConnection connection = new Http2ServerConnection(channel, sink, undertowOptions, bufferSize, rootHandler);
    HeaderMap requestHeaders = new HeaderMap();
    for (HeaderValues hv : initial.getRequestHeaders()) {
        requestHeaders.putAll(hv.getHeaderName(), hv);
    }
    final HttpServerExchange exchange = new HttpServerExchange(connection, requestHeaders, sink.getHeaders(), maxEntitySize);
    connection.setExchange(exchange);
    exchange.setRequestScheme(initial.getRequestScheme());
    exchange.setProtocol(initial.getProtocol());
    exchange.setRequestMethod(initial.getRequestMethod());
    exchange.setQueryString(initial.getQueryString());
    String uri = exchange.getQueryString().isEmpty() ? initial.getRequestURI() : initial.getRequestURI() + '?' + exchange.getQueryString();
    try {
        Connectors.setExchangeRequestPath(exchange, uri, encoding, decode, allowEncodingSlash, decodeBuffer, maxParameters);
    } catch (ParameterLimitException e) {
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }
    SSLSession session = channel.getSslSession();
    if (session != null) {
        connection.setSslSessionInfo(new Http2SslSessionInfo(channel));
    }
    Connectors.terminateRequest(exchange);
    sink.setCompletionListener(new ChannelListener<Http2DataStreamSinkChannel>() {

        @Override
        public void handleEvent(Http2DataStreamSinkChannel channel) {
            Connectors.terminateResponse(exchange);
        }
    });
    Connectors.executeRootHandler(rootHandler, exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Http2HeadersStreamSinkChannel(io.undertow.protocols.http2.Http2HeadersStreamSinkChannel) ParameterLimitException(io.undertow.util.ParameterLimitException) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) SSLSession(javax.net.ssl.SSLSession) HttpString(io.undertow.util.HttpString) Http2DataStreamSinkChannel(io.undertow.protocols.http2.Http2DataStreamSinkChannel)

Aggregations

Http2Channel (io.undertow.protocols.http2.Http2Channel)6 BytesReceivedStreamSourceConduit (io.undertow.conduits.BytesReceivedStreamSourceConduit)3 BytesSentStreamSinkConduit (io.undertow.conduits.BytesSentStreamSinkConduit)3 HttpServerExchange (io.undertow.server.HttpServerExchange)3 IOException (java.io.IOException)3 ByteActivityCallback (io.undertow.conduits.ByteActivityCallback)2 Http2DataStreamSinkChannel (io.undertow.protocols.http2.Http2DataStreamSinkChannel)2 HttpString (io.undertow.util.HttpString)2 ParameterLimitException (io.undertow.util.ParameterLimitException)2 ByteBuffer (java.nio.ByteBuffer)2 SSLSession (javax.net.ssl.SSLSession)2 StreamConnection (org.xnio.StreamConnection)2 ClientConnection (io.undertow.client.ClientConnection)1 Http2ClientConnection (io.undertow.client.http2.Http2ClientConnection)1 AbstractHttp2StreamSourceChannel (io.undertow.protocols.http2.AbstractHttp2StreamSourceChannel)1 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)1 Http2StreamSourceChannel (io.undertow.protocols.http2.Http2StreamSourceChannel)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)1 Http2ReceiveListener (io.undertow.server.protocol.http2.Http2ReceiveListener)1