Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UriEncoding.decode to expose a decodeQuery method #9006

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions common/uri/src/main/java/io/helidon/common/uri/UriEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ private UriEncoding() {

/**
* Decode a URI segment.
* <p>
* Percent characters {@code "%s"} found between brackets {@code "[]"} are not decoded to support IPv6 literal.
* E.g. {@code http://[fe80::1%lo0]:8080}.
* <p>
* See <a href="https://tools.ietf.org/html/rfc6874#section-2">RFC 6874, section 2.</a>
*
* @param uriSegment URI segment with percent encoding
* @return decoded string
*/
public static String decodeUri(String uriSegment) {
if (uriSegment.isEmpty()) {
return "";
}
if (uriSegment.indexOf('%') == -1 && uriSegment.indexOf('+') == -1) {
return uriSegment;
}
return decodeUri(uriSegment, true);
}

return decode(uriSegment);
/**
* Decode a URI query.
*
* @param uriQuery URI query with percent encoding
* @return decoded string
*/
public static String decodeQuery(String uriQuery) {
return decodeUri(uriQuery, false);
}

/**
Expand Down Expand Up @@ -123,7 +131,18 @@ private static void appendEscape(StringBuilder appender, int b) {
appender.append(HEX_DIGITS[b & 0x0F]);
}

private static String decode(String string) {
private static String decodeUri(String uriSegment, boolean ignorePercentInBrackets) {
if (uriSegment.isEmpty()) {
return "";
}
if (uriSegment.indexOf('%') == -1 && uriSegment.indexOf('+') == -1) {
return uriSegment;
}
return decode(uriSegment, ignorePercentInBrackets);
}

// see java.net.URI.decode(String, boolean)
private static String decode(String string, boolean ignorePercentInBrackets) {
int len = string.length();

StringBuilder sb = new StringBuilder(len);
Expand All @@ -141,7 +160,7 @@ private static String decode(String string) {
} else if (betweenBrackets && c == ']') {
betweenBrackets = false;
}
if (c != '%' || betweenBrackets) {
if (c != '%' || (betweenBrackets && ignorePercentInBrackets)) {
sb.append(c == '+' && !betweenBrackets ? ' ' : c); // handles '+' decoding
if (++i >= len) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import io.helidon.common.mapper.OptionalValue;
import io.helidon.common.mapper.Value;

import static io.helidon.common.uri.UriEncoding.decodeUri;
import static io.helidon.common.uri.UriEncoding.decodeQuery;

// must be lazily populated to prevent perf overhead when queries are ignored
final class UriQueryImpl implements UriQuery {
Expand Down Expand Up @@ -215,11 +215,11 @@ private void ensureDecoded() {
private void addDecoded(Map<String, List<String>> newQueryParams, String next) {
int eq = next.indexOf('=');
if (eq == -1) {
newQueryParams.putIfAbsent(decodeUri(next), new LinkedList<>());
newQueryParams.putIfAbsent(decodeQuery(next), new LinkedList<>());
} else {
String name = next.substring(0, eq);
String value = next.substring(eq + 1);
newQueryParams.computeIfAbsent(decodeUri(name), it -> new LinkedList<>()).add(decodeUri(value));
newQueryParams.computeIfAbsent(decodeQuery(name), it -> new LinkedList<>()).add(decodeQuery(value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ void testSpaceDecoding() {
assertThat(decodeUri("+hello+world+"), is(" hello world "));
assertThat(decodeUri("[+]hello[+]world[+]"), is("[+]hello[+]world[+]"));
}

@Test
void testIPv6Literal() {
assertThat(decodeUri("http://[fe80::1%lo0]:8080"), is("http://[fe80::1%lo0]:8080"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.helidon.common.uri;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;

Expand All @@ -41,11 +40,17 @@ void sanityParse() {
}

@Test
void testEncoded() throws UnsupportedEncodingException {
void testEncoded() {
UriQuery uriQuery = UriQuery.create("a=" + URLEncoder.encode("1&b=2", US_ASCII));
assertThat(uriQuery.get("a"), is("1&b=2"));
}

@Test
void testEncodedWithinBrackets() {
UriQuery uriQuery = UriQuery.create("msg=[Hello%20World]");
assertThat(uriQuery.get("msg"), is("[Hello World]"));
}

@Test
void testEncodedOtherChars() {
UriQuery uriQuery = UriQuery.create("a=b%26c=d&e=f&e=g&h=x%63%23e%3c");
Expand Down