Java Code Examples for io.undertow.util.URLUtils

The following examples show how to use io.undertow.util.URLUtils. These examples are extracted from open source projects. 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 Project: quarkus-http   Source File: URLDecodingHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    boolean decodeDone = exchange.getUndertowOptions().get(UndertowOptions.DECODE_URL, true);
    if (!decodeDone) {
        final StringBuilder sb = new StringBuilder();
        final boolean decodeSlash = exchange.getUndertowOptions().get(UndertowOptions.ALLOW_ENCODED_SLASH, false);
        exchange.setRequestPath(URLUtils.decode(exchange.getRequestPath(), charset, decodeSlash, false, sb));
        exchange.setRelativePath(URLUtils.decode(exchange.getRelativePath(), charset, decodeSlash, false, sb));
        exchange.setResolvedPath(URLUtils.decode(exchange.getResolvedPath(), charset, decodeSlash, false, sb));
        if (!exchange.getQueryString().isEmpty()) {
            final TreeMap<String, Deque<String>> newParams = new TreeMap<>();
            for (Map.Entry<String, Deque<String>> param : exchange.getQueryParameters().entrySet()) {
                final Deque<String> newVales = new ArrayDeque<>(param.getValue().size());
                for (String val : param.getValue()) {
                    newVales.add(URLUtils.decode(val, charset, true, true, sb));
                }
                newParams.put(URLUtils.decode(param.getKey(), charset, true, true, sb), newVales);
            }
            exchange.getQueryParameters().clear();
            exchange.getQueryParameters().putAll(newParams);
        }
    }
    next.handleRequest(exchange);
}
 
Example 2
Source Project: quarkus-http   Source File: QueryParametersTestCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@ProxyIgnore
public void testQueryParameterParsingIncorrectlyEncodedURI() throws IOException, ParameterLimitException {
    StringBuilder out = new StringBuilder();
    out.append((char)0xc7);
    out.append((char)0xd1);
    out.append((char)0x25);
    out.append((char)0x32);
    out.append((char)0x30);
    out.append((char)0xb1);
    out.append((char)0xdb);
    String s = "p=" + out.toString();
    HttpServerExchange exchange = new HttpServerExchange(new MockHttpExchange(),-1 );
    URLUtils.parseQueryString(s, exchange, "MS949", true, 1000);
    Assert.assertEquals("한 글", exchange.getQueryParameters().get("p").getFirst());

}
 
Example 3
Source Project: SikuliX1   Source File: SikulixServer.java    License: MIT License 6 votes vote down vote up
private String[] getScriptArgs(final HttpServerExchange exchange) {
  String[] args = {};
  Optional<String> argsString = Optional.empty();
  String queryString = exchange.getQueryString();
  if (queryString != null) {
    Matcher matcher = PATTERN_QUERY_ARGS.matcher(queryString);
    if (matcher.find()) {
      argsString = Optional.of(matcher.group("args"));
    }
  }
  if (exchange.getRequestMethod().equals(Methods.POST)) {
    FormData form = exchange.getAttachment(FormDataParser.FORM_DATA);
    if (form != null) {
      argsString = Optional.ofNullable(form.getLast("args")).map(fVal -> fVal.getValue());
    }
  }
  if (argsString.isPresent()) {
    StringBuilder buf = new StringBuilder();
    String[] tokens = argsString.get().split(";");
    args = new String[tokens.length];
    for (int i=0; i<tokens.length; i++) {
      args[i] = URLUtils.decode(tokens[i], "UTF-8", true, buf);
    }
  }
  return args;
}
 
Example 4
Source Project: lams   Source File: VirtualHost.java    License: GNU General Public License v2.0 6 votes vote down vote up
public synchronized void removeContext(final String path, final String jvmRoute, final Context context) {
    if (path == null || path.isEmpty()) {
        throw UndertowMessages.MESSAGES.pathMustBeSpecified();
    }

    final String normalizedPath = URLUtils.normalizeSlashes(path);
    if (STRING_PATH_SEPARATOR.equals(normalizedPath)) {
        defaultHandler.contexts.remove(jvmRoute, context);
    }

    final HostEntry hostEntry = contexts.get(normalizedPath);
    if (hostEntry != null) {
        if (hostEntry.contexts.remove(jvmRoute, context)) {
            if (hostEntry.contexts.isEmpty()) {
                contexts.remove(normalizedPath);
                buildLengths();
            }
        }
    }
}
 
Example 5
Source Project: lams   Source File: URLDecodingHandler.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    boolean decodeDone = exchange.getConnection().getUndertowOptions().get(UndertowOptions.DECODE_URL, true);
    if (!decodeDone) {
        final StringBuilder sb = new StringBuilder();
        final boolean decodeSlash = exchange.getConnection().getUndertowOptions().get(UndertowOptions.ALLOW_ENCODED_SLASH, false);
        exchange.setRequestPath(URLUtils.decode(exchange.getRequestPath(), charset, decodeSlash, false, sb));
        exchange.setRelativePath(URLUtils.decode(exchange.getRelativePath(), charset, decodeSlash, false, sb));
        exchange.setResolvedPath(URLUtils.decode(exchange.getResolvedPath(), charset, decodeSlash, false, sb));
        if (!exchange.getQueryString().isEmpty()) {
            final TreeMap<String, Deque<String>> newParams = new TreeMap<>();
            for (Map.Entry<String, Deque<String>> param : exchange.getQueryParameters().entrySet()) {
                final Deque<String> newVales = new ArrayDeque<>(param.getValue().size());
                for (String val : param.getValue()) {
                    newVales.add(URLUtils.decode(val, charset, true, true, sb));
                }
                newParams.put(URLUtils.decode(param.getKey(), charset, true, true, sb), newVales);
            }
            exchange.getQueryParameters().clear();
            exchange.getQueryParameters().putAll(newParams);
        }
    }
    next.handleRequest(exchange);
}
 
Example 6
Source Project: lams   Source File: AjpRequestParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String decode(String url, final boolean containsUrlCharacters) throws UnsupportedEncodingException {
    if (doDecode && containsUrlCharacters) {
        try {
            if(decodeBuffer == null) {
                decodeBuffer = new StringBuilder();
            }
            return URLUtils.decode(url, this.encoding, allowEncodedSlash, false, decodeBuffer);
        } catch (Exception e) {
            throw UndertowMessages.MESSAGES.failedToDecodeURL(url, encoding, e);
        }
    }
    return url;
}
 
Example 7
Source Project: lams   Source File: HttpRequestParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String decode(final String value, boolean urlDecodeRequired, ParseState state, final boolean allowEncodedSlash, final boolean formEncoded) {
    if (urlDecodeRequired) {
        return URLUtils.decode(value, charset, allowEncodedSlash, formEncoded, state.decodeBuffer);
    } else {
        return value;
    }
}
 
Example 8
Source Project: wildfly-camel   Source File: CamelUndertowHostService.java    License: Apache License 2.0 5 votes vote down vote up
private String getBasePath(URI httpURI) {
    String path = httpURI.getPath();
    if (path.contains(REST_PATH_PLACEHOLDER)) {
        path = PathTemplate.create(path).getBase();
    }
    return URLUtils.normalizeSlashes(path);
}
 
Example 9
Source Project: wildfly-camel   Source File: CamelUndertowHostService.java    License: Apache License 2.0 5 votes vote down vote up
private String getRelativePath(URI httpURI, boolean matchOnUriPrefix) {
    String path = httpURI.getPath();
    String contextPath = getContextPath(httpURI);
    String normalizedPath = URLUtils.normalizeSlashes(path.substring(contextPath.length()));
    if (matchOnUriPrefix) {
        normalizedPath += "*";
    }
    return normalizedPath;
}
 
Example 10
Source Project: quarkus-http   Source File: FormEncodedDataDefinition.java    License: Apache License 2.0 4 votes vote down vote up
private void doParse(ByteBuf buffer) {
    try {
        if (buffer != null) {
            while (buffer.isReadable()) {
                byte n = buffer.readByte();
                switch (state) {
                    case 0: {
                        if (n == '=') {
                            name = builder.toString();
                            builder.setLength(0);
                            state = 2;
                        } else if (n == '&') {
                            data.add(builder.toString(), "");
                            builder.setLength(0);
                            state = 0;
                        } else if (n == '%' || n == '+') {
                            state = 1;
                            builder.append((char) n);
                        } else {
                            builder.append((char) n);
                        }
                        break;
                    }
                    case 1: {
                        if (n == '=') {
                            name = URLUtils.decode(builder.toString(), charset, true, new StringBuilder());
                            builder.setLength(0);
                            state = 2;
                        } else if (n == '&') {
                            data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), "");
                            builder.setLength(0);
                            state = 0;
                        } else {
                            builder.append((char) n);
                        }
                        break;
                    }
                    case 2: {
                        if (n == '&') {
                            data.add(name, builder.toString());
                            builder.setLength(0);
                            state = 0;
                        } else if (n == '%' || n == '+') {
                            state = 3;
                            builder.append((char) n);
                        } else {
                            builder.append((char) n);
                        }
                        break;
                    }
                    case 3: {
                        if (n == '&') {
                            data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder()));
                            builder.setLength(0);
                            state = 0;
                        } else {
                            builder.append((char) n);
                        }
                        break;
                    }
                }
            }
        } else {
            if (state == 2) {
                data.add(name, builder.toString());
            } else if (state == 3) {
                data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder()));
            } else if (builder.length() > 0) {
                if (state == 1) {
                    data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), "");
                } else {
                    data.add(builder.toString(), "");
                }
            }
            state = 4;
            exchange.putAttachment(FORM_DATA, data);
        }
    } finally {
        if (buffer != null) {
            buffer.release();
        }
    }
}
 
Example 11
private void doParse(final StreamSourceChannel channel) throws IOException {
    int c = 0;
    final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
    try {
        final ByteBuffer buffer = pooled.getBuffer();
        do {
            buffer.clear();
            c = channel.read(buffer);
            if (c > 0) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    byte n = buffer.get();
                    switch (state) {
                        case 0: {
                            if (n == '=') {
                                name = builder.toString();
                                builder.setLength(0);
                                state = 2;
                            } else if (n == '&') {
                                data.add(builder.toString(), "");
                                builder.setLength(0);
                                state = 0;
                            } else if (n == '%' || n == '+') {
                                state = 1;
                                builder.append((char) n);
                            } else {
                                builder.append((char) n);
                            }
                            break;
                        }
                        case 1: {
                            if (n == '=') {
                                name = URLUtils.decode(builder.toString(), charset, true, new StringBuilder());
                                builder.setLength(0);
                                state = 2;
                            } else if (n == '&') {
                                data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), "");
                                builder.setLength(0);
                                state = 0;
                            } else {
                                builder.append((char) n);
                            }
                            break;
                        }
                        case 2: {
                            if (n == '&') {
                                data.add(name, builder.toString());
                                builder.setLength(0);
                                state = 0;
                            } else if (n == '%' || n == '+') {
                                state = 3;
                                builder.append((char) n);
                            } else {
                                builder.append((char) n);
                            }
                            break;
                        }
                        case 3: {
                            if (n == '&') {
                                data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder()));
                                builder.setLength(0);
                                state = 0;
                            } else {
                                builder.append((char) n);
                            }
                            break;
                        }
                    }
                }
            }
        } while (c > 0);
        if (c == -1) {
            if (state == 2) {
                data.add(name, builder.toString());
            } else if (state == 3) {
                data.add(name, URLUtils.decode(builder.toString(), charset, true, new StringBuilder()));
            } else if(builder.length() > 0) {
                if(state == 1) {
                    data.add(URLUtils.decode(builder.toString(), charset, true, new StringBuilder()), "");
                } else {
                    data.add(builder.toString(), "");
                }
            }
            state = 4;
            exchange.putAttachment(FORM_DATA, data);
        }
    } finally {
        pooled.close();
    }
}