Search in sources :

Example 1 with ParameterLimitException

use of io.undertow.util.ParameterLimitException 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 ParameterLimitException

use of io.undertow.util.ParameterLimitException in project undertow by undertow-io.

the class Http2ReceiveListener method handleRequests.

private void handleRequests(Http2Channel channel, Http2StreamSourceChannel frame) {
    //we have a request
    final Http2StreamSourceChannel dataChannel = frame;
    final Http2ServerConnection connection = new Http2ServerConnection(channel, dataChannel, undertowOptions, bufferSize, rootHandler);
    // Check request headers.
    if (!checkRequestHeaders(dataChannel.getHeaders())) {
        channel.sendRstStream(frame.getStreamId(), Http2Channel.ERROR_PROTOCOL_ERROR);
        try {
            Channels.drain(frame, Long.MAX_VALUE);
        } catch (IOException e) {
        // ignore, this is expected because of the RST
        }
        return;
    }
    final HttpServerExchange exchange = new HttpServerExchange(connection, dataChannel.getHeaders(), dataChannel.getResponseChannel().getHeaders(), maxEntitySize);
    connection.setExchange(exchange);
    dataChannel.setMaxStreamSize(maxEntitySize);
    exchange.setRequestScheme(exchange.getRequestHeaders().getFirst(SCHEME));
    exchange.setProtocol(Protocols.HTTP_2_0);
    exchange.setRequestMethod(Methods.fromString(exchange.getRequestHeaders().getFirst(METHOD)));
    exchange.getRequestHeaders().put(Headers.HOST, exchange.getRequestHeaders().getFirst(AUTHORITY));
    final String path = exchange.getRequestHeaders().getFirst(PATH);
    if (path == null || path.isEmpty()) {
        UndertowLogger.REQUEST_IO_LOGGER.debugf("No :path header sent in HTTP/2 request, closing connection. Remote peer %s", connection.getPeerAddress());
        channel.sendGoAway(Http2Channel.ERROR_PROTOCOL_ERROR);
        return;
    }
    try {
        Connectors.setExchangeRequestPath(exchange, path, encoding, decode, allowEncodingSlash, decodeBuffer, maxParameters);
    } catch (ParameterLimitException e) {
        //this can happen if max parameters is exceeded
        UndertowLogger.REQUEST_IO_LOGGER.debug("Failed to set request path", e);
        exchange.setStatusCode(StatusCodes.BAD_REQUEST);
        exchange.endExchange();
        return;
    }
    SSLSession session = channel.getSslSession();
    if (session != null) {
        connection.setSslSessionInfo(new Http2SslSessionInfo(channel));
    }
    dataChannel.getResponseChannel().setCompletionListener(new ChannelListener<Http2DataStreamSinkChannel>() {

        @Override
        public void handleEvent(Http2DataStreamSinkChannel channel) {
            Connectors.terminateResponse(exchange);
        }
    });
    if (!dataChannel.isOpen()) {
        Connectors.terminateRequest(exchange);
    } else {
        dataChannel.setCompletionListener(new ChannelListener<Http2StreamSourceChannel>() {

            @Override
            public void handleEvent(Http2StreamSourceChannel channel) {
                Connectors.terminateRequest(exchange);
            }
        });
    }
    if (connectorStatistics != null) {
        connectorStatistics.setup(exchange);
    }
    //TODO: we should never actually put these into the map in the first place
    exchange.getRequestHeaders().remove(AUTHORITY);
    exchange.getRequestHeaders().remove(PATH);
    exchange.getRequestHeaders().remove(SCHEME);
    exchange.getRequestHeaders().remove(METHOD);
    Connectors.executeRootHandler(rootHandler, exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ParameterLimitException(io.undertow.util.ParameterLimitException) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) HttpString(io.undertow.util.HttpString) AbstractHttp2StreamSourceChannel(io.undertow.protocols.http2.AbstractHttp2StreamSourceChannel) Http2StreamSourceChannel(io.undertow.protocols.http2.Http2StreamSourceChannel) Http2DataStreamSinkChannel(io.undertow.protocols.http2.Http2DataStreamSinkChannel)

Example 3 with ParameterLimitException

use of io.undertow.util.ParameterLimitException 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(Http2ReceiveListener.METHOD, method.toString());
        requestHeaders.put(Http2ReceiveListener.PATH, path.toString());
        requestHeaders.put(Http2ReceiveListener.AUTHORITY, exchange.getHostAndPort());
        requestHeaders.put(Http2ReceiveListener.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;
        }
        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)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)3 ParameterLimitException (io.undertow.util.ParameterLimitException)3 Http2DataStreamSinkChannel (io.undertow.protocols.http2.Http2DataStreamSinkChannel)2 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)2 HeaderMap (io.undertow.util.HeaderMap)2 HttpString (io.undertow.util.HttpString)2 IOException (java.io.IOException)2 SSLSession (javax.net.ssl.SSLSession)2 AbstractHttp2StreamSourceChannel (io.undertow.protocols.http2.AbstractHttp2StreamSourceChannel)1 Http2StreamSourceChannel (io.undertow.protocols.http2.Http2StreamSourceChannel)1 HeaderValues (io.undertow.util.HeaderValues)1