Search in sources :

Example 1 with Http2DataStreamSinkChannel

use of io.undertow.protocols.http2.Http2DataStreamSinkChannel 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)

Example 2 with Http2DataStreamSinkChannel

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

the class Http2ReceiveListener method handleCommonSetup.

private void handleCommonSetup(Http2HeadersStreamSinkChannel sink, HttpServerExchange exchange, Http2ServerConnection connection) {
    Http2Channel channel = sink.getChannel();
    SSLSession session = channel.getSslSession();
    if (session != null) {
        connection.setSslSessionInfo(new Http2SslSessionInfo(channel));
    }
    sink.setTrailersProducer(new Http2DataStreamSinkChannel.TrailersProducer() {

        @Override
        public HeaderMap getTrailers() {
            Supplier<HeaderMap> supplier = exchange.getAttachment(HttpAttachments.RESPONSE_TRAILER_SUPPLIER);
            if (supplier != null) {
                return supplier.get();
            }
            return exchange.getAttachment(HttpAttachments.RESPONSE_TRAILERS);
        }
    });
    sink.setCompletionListener(new ChannelListener<Http2DataStreamSinkChannel>() {

        @Override
        public void handleEvent(Http2DataStreamSinkChannel channel) {
            Connectors.terminateResponse(exchange);
        }
    });
    exchange.setProtocol(Protocols.HTTP_2_0);
    if (exchange.getRequestMethod().equals(Methods.HEAD)) {
        exchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

            @Override
            public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
                return new HeadStreamSinkConduit(factory.create(), null, true);
            }
        });
    }
}
Also used : SSLSession(javax.net.ssl.SSLSession) Http2DataStreamSinkChannel(io.undertow.protocols.http2.Http2DataStreamSinkChannel) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit) HttpServerExchange(io.undertow.server.HttpServerExchange) HeaderMap(io.undertow.util.HeaderMap) HeadStreamSinkConduit(io.undertow.conduits.HeadStreamSinkConduit) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) Http2Channel(io.undertow.protocols.http2.Http2Channel) Supplier(java.util.function.Supplier)

Example 3 with Http2DataStreamSinkChannel

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

the class Http2ServerConnection method pushResource.

@Override
public boolean pushResource(String path, HttpString method, HeaderMap requestHeaders, final HttpHandler handler) {
    HeaderMap responseHeaders = new HeaderMap();
    try {
        requestHeaders.put(METHOD, method.toString());
        requestHeaders.put(PATH, path.toString());
        requestHeaders.put(AUTHORITY, exchange.getHostAndPort());
        requestHeaders.put(SCHEME, exchange.getRequestScheme());
        Http2HeadersStreamSinkChannel sink = channel.sendPushPromise(responseChannel.getStreamId(), requestHeaders, responseHeaders);
        Http2ServerConnection newConnection = new Http2ServerConnection(channel, sink, getUndertowOptions(), getBufferSize(), rootHandler);
        final HttpServerExchange exchange = new HttpServerExchange(newConnection, requestHeaders, responseHeaders, getUndertowOptions().get(UndertowOptions.MAX_ENTITY_SIZE, UndertowOptions.DEFAULT_MAX_ENTITY_SIZE));
        newConnection.setExchange(exchange);
        exchange.setRequestMethod(method);
        exchange.setProtocol(Protocols.HTTP_1_1);
        exchange.setRequestScheme(this.exchange.getRequestScheme());
        try {
            Connectors.setExchangeRequestPath(exchange, path, getUndertowOptions().get(UndertowOptions.URL_CHARSET, StandardCharsets.UTF_8.name()), getUndertowOptions().get(UndertowOptions.DECODE_URL, true), getUndertowOptions().get(UndertowOptions.ALLOW_ENCODED_SLASH, false), new StringBuilder(), getUndertowOptions().get(UndertowOptions.MAX_PARAMETERS, UndertowOptions.DEFAULT_MAX_HEADERS));
        } catch (ParameterLimitException e) {
            UndertowLogger.REQUEST_IO_LOGGER.debug("Too many parameters in HTTP/2 request", e);
            exchange.setStatusCode(StatusCodes.BAD_REQUEST);
            exchange.endExchange();
            return false;
        }
        sink.setCompletionListener(new ChannelListener<Http2DataStreamSinkChannel>() {

            @Override
            public void handleEvent(Http2DataStreamSinkChannel channel) {
                Connectors.terminateResponse(exchange);
            }
        });
        Connectors.terminateRequest(exchange);
        getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                Connectors.executeRootHandler(handler, exchange);
            }
        });
        return true;
    } catch (IOException e) {
        UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
        return false;
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Http2HeadersStreamSinkChannel(io.undertow.protocols.http2.Http2HeadersStreamSinkChannel) ParameterLimitException(io.undertow.util.ParameterLimitException) HeaderMap(io.undertow.util.HeaderMap) IOException(java.io.IOException) Http2DataStreamSinkChannel(io.undertow.protocols.http2.Http2DataStreamSinkChannel)

Aggregations

Http2DataStreamSinkChannel (io.undertow.protocols.http2.Http2DataStreamSinkChannel)3 HttpServerExchange (io.undertow.server.HttpServerExchange)3 HeaderMap (io.undertow.util.HeaderMap)3 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)2 ParameterLimitException (io.undertow.util.ParameterLimitException)2 SSLSession (javax.net.ssl.SSLSession)2 HeadStreamSinkConduit (io.undertow.conduits.HeadStreamSinkConduit)1 Http2Channel (io.undertow.protocols.http2.Http2Channel)1 HeaderValues (io.undertow.util.HeaderValues)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Supplier (java.util.function.Supplier)1 StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)1