Skip to content

Commit

Permalink
Revise URIs.decodeParameters, re #9
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Sep 14, 2022
1 parent 30a903b commit b30736a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
33 changes: 21 additions & 12 deletions src/main/java/org/libj/net/URIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.Map;

import org.libj.util.MultiHashMap;
import org.libj.util.StringPaths;

/**
Expand Down Expand Up @@ -315,39 +314,49 @@ public static URI toURI(final URI baseURI, String path) {
}
}

private static void add(final Map<String,List<String>> parameters, final String name, final String value) {
List<String> values = parameters.get(name);
if (values == null)
parameters.put(name, values = new ArrayList<>());

values.add(value);
}

/**
* Returns a map of parameters decoded from the provided {@code data} and {@code charset}.
*
* @param parameters The map into which decoded parameters are to be added.
* @param data The string of parameters to decode.
* @param charset The charset with which the {@code data} is to be decoded.
* @return A map of parameters decoded from the provided {@code data} and {@code charset}.
* @param charset The charset with which the {@code data} is to be decoded, or {@code null} if the data should not be decoded.
* @throws UnsupportedEncodingException If character encoding needs to be consulted, but named character encoding is not
* supported.
* @throws IllegalArgumentException If {@code data} or {@code charset} is null, or when illegal strings are encountered.
* @throws IllegalArgumentException If {@code parameters} or {@code data} is null, or when illegal strings are encountered.
*/
public static Map<String,List<String>> decodeParameters(final String data, final String charset) throws UnsupportedEncodingException {
public static void decodeParameters(final Map<String,List<String>> parameters, final String data, final String charset) throws UnsupportedEncodingException {
assertNotNull(parameters);
assertNotNull(data);
assertNotNull(charset);
final int i$ = data.length();
if (i$ == 0)
return;

final StringBuilder b = new StringBuilder();
String name = null;
final MultiHashMap<String,String,List<String>> map = new MultiHashMap<>(ArrayList::new);
for (int i = 0; i < data.length(); ++i) {
for (int i = 0; i < i$; ++i) {
final char ch = data.charAt(i);
if (ch == '&') {
map.add(name, URLDecoder.decode(b.toString(), charset));
add(parameters, name, charset == null ? b.toString() : URLDecoder.decode(b.toString(), charset));
b.setLength(0);
}
else if (ch == '=') {
name = URLDecoder.decode(b.toString(), charset);
name = charset == null ? b.toString() : URLDecoder.decode(b.toString(), charset);
b.setLength(0);
}
else {
b.append(ch);
}
}

map.add(name, URLDecoder.decode(b.toString(), charset));
return map;
add(parameters, name, charset == null ? b.toString() : URLDecoder.decode(b.toString(), charset));
}

private URIs() {
Expand Down
22 changes: 15 additions & 7 deletions src/test/java/org/libj/net/URIsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,42 @@ private static void assertMap(final Map<String,List<String>> actual, final Strin
expected.add(members[i++], members[i++]);

assertEquals(expected, actual);
actual.clear();
}

private static Map<String,List<String>> decodeParameters(final Map<String,List<String>> parameters, final String data, final String charset) throws UnsupportedEncodingException {
URIs.decodeParameters(parameters, data, charset);
return parameters;
}

@Test
public void testDecodeParameters() throws Exception {
try {
URIs.decodeParameters(null, "UTF-8");
URIs.decodeParameters(null, "f=b", "UTF-8");
fail("Expected IllegalArgumentException");
}
catch (final IllegalArgumentException e) {
}

final MultiHashMap<String,String,List<String>> map = new MultiHashMap<>(ArrayList::new);
try {
URIs.decodeParameters("a=b", null);
URIs.decodeParameters(map, null, "UTF-8");
fail("Expected IllegalArgumentException");
}
catch (final IllegalArgumentException e) {
}

try {
URIs.decodeParameters("a=b", "bla");
URIs.decodeParameters(map, "a=b", "bla");
fail("Expected IllegalArgumentException");
}
catch (final UnsupportedEncodingException e) {
}

assertMap(URIs.decodeParameters("foo=bar", "UTF-8"), "foo", "bar");
assertMap(URIs.decodeParameters("foo=bar&foo=bar", "UTF-8"), "foo", "bar", "foo", "bar");
assertMap(URIs.decodeParameters("a=b&c=d", "UTF-8"), "a", "b", "c", "d");
assertMap(URIs.decodeParameters("a%20a=b%20b&c%20c=d%20d", "UTF-8"), "a a", "b b", "c c", "d d");
assertMap(decodeParameters(map, "foo=bar", "UTF-8"), "foo", "bar");
assertMap(decodeParameters(map, "foo=bar&foo=bar", "UTF-8"), "foo", "bar", "foo", "bar");
assertMap(decodeParameters(map, "a=b&c=d", "UTF-8"), "a", "b", "c", "d");
assertMap(decodeParameters(map, "a%20a=b%20b&c%20c=d%20d", "UTF-8"), "a a", "b b", "c c", "d d");
assertMap(decodeParameters(map, "a%20a=b%20b&c%20c=d%20d", null), "a%20a", "b%20b", "c%20c", "d%20d");
}
}

0 comments on commit b30736a

Please sign in to comment.