Java Code Examples for io.undertow.server.HttpServerExchange#setQueryString()

The following examples show how to use io.undertow.server.HttpServerExchange#setQueryString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestPathAttribute.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    exchange.setResolvedPath("");
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRequestPath(path);
        exchange.setRelativePath(path);
        exchange.setRequestURI(newValue);

        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }
}
 
Example 2
Source File: RequestURLAttribute.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
        exchange.setResolvedPath("");
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRelativePath(path);
        exchange.setRequestURI(path);
        exchange.setRequestPath(path);
        exchange.setResolvedPath("");
        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }

}
 
Example 3
Source File: RequestPathAttribute.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    exchange.setResolvedPath("");
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRequestPath(path);
        exchange.setRelativePath(path);
        exchange.setRequestURI(newValue);

        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }
}
 
Example 4
Source File: RequestURLAttribute.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    int pos = newValue.indexOf('?');
    if (pos == -1) {
        exchange.setRelativePath(newValue);
        exchange.setRequestURI(newValue);
        exchange.setRequestPath(newValue);
        exchange.setResolvedPath("");
    } else {
        final String path = newValue.substring(0, pos);
        exchange.setRelativePath(path);
        exchange.setRequestURI(path);
        exchange.setRequestPath(path);
        exchange.setResolvedPath("");
        final String newQueryString = newValue.substring(pos);
        exchange.setQueryString(newQueryString);
        exchange.getQueryParameters().putAll(QueryParameterUtils.parseQueryString(newQueryString.substring(1), QueryParameterUtils.getQueryParamEncoding(exchange)));
    }

}
 
Example 5
Source File: QueryStringAttribute.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    exchange.setQueryString(newValue);
}
 
Example 6
Source File: QueryStringAttribute.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    exchange.setQueryString(newValue);
}
 
Example 7
Source File: HttpRequestParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a path value
 *
 * @param buffer   The buffer
 * @param state    The current state
 * @param exchange The exchange builder
 * @return The number of bytes remaining
 */
@SuppressWarnings("unused")
final void handlePath(ByteBuffer buffer, ParseState state, HttpServerExchange exchange) throws BadRequestException {
    StringBuilder stringBuilder = state.stringBuilder;
    int parseState = state.parseState;
    int canonicalPathStart = state.pos;
    boolean urlDecodeRequired = state.urlDecodeRequired;

    while (buffer.hasRemaining()) {
        char next = (char) (buffer.get() & 0xFF);
        if(!allowUnescapedCharactersInUrl && !ALLOWED_TARGET_CHARACTER[next]) {
            throw new BadRequestException(UndertowMessages.MESSAGES.invalidCharacterInRequestTarget(next));
        }
        if (next == ' ' || next == '\t') {
            if (stringBuilder.length() != 0) {
                final String path = stringBuilder.toString();
                if(parseState == SECOND_SLASH) {
                    exchange.setRequestPath("/");
                    exchange.setRelativePath("/");
                    exchange.setRequestURI(path);
                } else if (parseState < HOST_DONE) {
                    String decodedPath = decode(path, urlDecodeRequired, state, allowEncodedSlash, false);
                    exchange.setRequestPath(decodedPath);
                    exchange.setRelativePath(decodedPath);
                    exchange.setRequestURI(path);
                } else {
                    handleFullUrl(state, exchange, canonicalPathStart, urlDecodeRequired, path);
                }
                exchange.setQueryString("");
                state.state = ParseState.VERSION;
                state.stringBuilder.setLength(0);
                state.parseState = 0;
                state.pos = 0;
                state.urlDecodeRequired = false;
                return;
            }
        } else if (next == '\r' || next == '\n') {
            throw UndertowMessages.MESSAGES.failedToParsePath();
        } else if (next == '?' && (parseState == START || parseState == HOST_DONE || parseState == IN_PATH)) {
            beginQueryParameters(buffer, state, exchange, stringBuilder, parseState, canonicalPathStart, urlDecodeRequired);
            return;
        } else if (next == ';' && (parseState == START || parseState == HOST_DONE || parseState == IN_PATH)) {
            beginPathParameters(state, exchange, stringBuilder, parseState, canonicalPathStart, urlDecodeRequired);
            handlePathParameters(buffer, state, exchange);
            return;
        } else {

            if (decode && (next == '%' || next > 127)) {
                urlDecodeRequired = true;
            } else if (next == ':' && parseState == START) {
                parseState = FIRST_COLON;
            } else if (next == '/' && parseState == FIRST_COLON) {
                parseState = FIRST_SLASH;
            } else if (next == '/' && parseState == FIRST_SLASH) {
                parseState = SECOND_SLASH;
            } else if (next == '/' && parseState == SECOND_SLASH) {
                parseState = HOST_DONE;
                canonicalPathStart = stringBuilder.length();
            } else if (parseState == FIRST_COLON || parseState == FIRST_SLASH) {
                parseState = IN_PATH;
            } else if (next == '/' && parseState != HOST_DONE) {
                parseState = IN_PATH;
            }
            stringBuilder.append(next);
        }

    }
    state.parseState = parseState;
    state.pos = canonicalPathStart;
    state.urlDecodeRequired = urlDecodeRequired;
}
 
Example 8
Source File: HttpRequestParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses a path value
 *
 * @param buffer   The buffer
 * @param state    The current state
 * @param exchange The exchange builder
 * @return The number of bytes remaining
 */
@SuppressWarnings("unused")
final void handleQueryParameters(ByteBuffer buffer, ParseState state, HttpServerExchange exchange) throws BadRequestException {
    StringBuilder stringBuilder = state.stringBuilder;
    int queryParamPos = state.pos;
    int mapCount = state.mapCount;
    boolean urlDecodeRequired = state.urlDecodeRequired;
    String nextQueryParam = state.nextQueryParam;

    //so this is a bit funky, because it not only deals with parsing, but
    //also deals with URL decoding the query parameters as well, while also
    //maintaining a non-decoded version to use as the query string
    //In most cases these string will be the same, and as we do not want to
    //build up two separate strings we don't use encodedStringBuilder unless
    //we encounter an encoded character

    while (buffer.hasRemaining()) {
        char next = (char) (buffer.get() & 0xFF);
        if(!allowUnescapedCharactersInUrl && !ALLOWED_TARGET_CHARACTER[next]) {
            throw new BadRequestException(UndertowMessages.MESSAGES.invalidCharacterInRequestTarget(next));
        }
        if (next == ' ' || next == '\t') {
            final String queryString = stringBuilder.toString();
            exchange.setQueryString(queryString);
            if (nextQueryParam == null) {
                if (queryParamPos != stringBuilder.length()) {
                    exchange.addQueryParam(decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true), "");
                }
            } else {
                exchange.addQueryParam(nextQueryParam, decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true));
            }
            state.state = ParseState.VERSION;
            state.stringBuilder.setLength(0);
            state.pos = 0;
            state.nextQueryParam = null;
            state.urlDecodeRequired = false;
            state.mapCount = 0;
            return;
        } else if (next == '\r' || next == '\n') {
            throw UndertowMessages.MESSAGES.failedToParsePath();
        } else {
            if (decode && (next == '+' || next == '%' || next > 127)) { //+ is only a whitespace substitute in the query part of the URL
                urlDecodeRequired = true;
            } else if (next == '=' && nextQueryParam == null) {
                nextQueryParam = decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true);
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
            } else if (next == '&' && nextQueryParam == null) {
                if (++mapCount >= maxParameters) {
                    throw UndertowMessages.MESSAGES.tooManyQueryParameters(maxParameters);
                }
                if (queryParamPos != stringBuilder.length()) {
                    exchange.addQueryParam(decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true), "");
                }
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
            } else if (next == '&') {
                if (++mapCount >= maxParameters) {
                    throw UndertowMessages.MESSAGES.tooManyQueryParameters(maxParameters);
                }
                exchange.addQueryParam(nextQueryParam, decode(stringBuilder.substring(queryParamPos), urlDecodeRequired, state, true, true));
                urlDecodeRequired = false;
                queryParamPos = stringBuilder.length() + 1;
                nextQueryParam = null;
            }
            stringBuilder.append(next);

        }

    }
    state.pos = queryParamPos;
    state.nextQueryParam = nextQueryParam;
    state.urlDecodeRequired = urlDecodeRequired;
    state.mapCount = mapCount;
}