diff --git a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java index 4fdf1acd6e5f..56c6b25e45e6 100644 --- a/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java +++ b/jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java @@ -497,18 +497,21 @@ public static Stream decodePathTests() {"http://localhost:9000/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/x\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", EnumSet.noneOf(Violation.class)}, {"http://localhost:9000/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", "/\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32\uD83C\uDF32", EnumSet.noneOf(Violation.class)}, // @checkstyle-enable-check : AvoidEscapedUnicodeCharactersCheck + + // An empty (null) authority + {"http://", null, null, null} }).map(Arguments::of); } @ParameterizedTest @MethodSource("decodePathTests") - public void testDecodedPath(String input, String canonicalPath, String decodedPath, EnumSet expected) + public void testDecodedPath(String input, String expectedCanonicalPath, String expectedDecodedPath, EnumSet expected) { try { HttpURI uri = HttpURI.from(input); - assertThat("Canonical Path", uri.getCanonicalPath(), is(canonicalPath)); - assertThat("Decoded Path", uri.getDecodedPath(), is(decodedPath)); + assertThat("Canonical Path", uri.getCanonicalPath(), is(expectedCanonicalPath)); + assertThat("Decoded Path", uri.getDecodedPath(), is(expectedDecodedPath)); EnumSet ambiguous = EnumSet.copyOf(expected); ambiguous.retainAll(EnumSet.complementOf(EnumSet.of(Violation.UTF16_ENCODINGS, Violation.BAD_UTF8_ENCODING))); @@ -523,9 +526,9 @@ public void testDecodedPath(String input, String canonicalPath, String decodedPa } catch (Exception e) { - if (decodedPath != null) + if (expectedDecodedPath != null) e.printStackTrace(); - assertThat(decodedPath, nullValue()); + assertThat(expectedDecodedPath, nullValue()); } } diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java index b0f73abb8dc1..9ac52169db2d 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java @@ -471,11 +471,14 @@ public static StringBuilder encodeString(StringBuilder buf, /** * Decodes a percent-encoded URI path (assuming UTF-8 characters) and strips path parameters. * @param path The URI path to decode + * @return the decoded path (or null if input path is null) * @see #canonicalPath(String) * @see #normalizePath(String) */ public static String decodePath(String path) { + if (path == null) + return null; return decodePath(path, 0, path.length()); } @@ -484,11 +487,14 @@ public static String decodePath(String path) * @param path A String holding the URI path to decode * @param offset The start of the URI within the path string * @param length The length of the URI within the path string + * @return the decoded path (or null if input path is null) * @see #canonicalPath(String) * @see #normalizePath(String) */ public static String decodePath(String path, int offset, int length) { + if (path == null) + return null; try { Utf8StringBuilder builder = null; diff --git a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java index e482337f0b08..c905070b7417 100644 --- a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java +++ b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java @@ -135,6 +135,7 @@ public static Stream decodePathSource() // Test for null character (real world ugly test case) Arguments.of("/%00/", "/%00/", "/\u0000/"), + Arguments.of(null, null, null), // Deprecated Microsoft Percent-U encoding Arguments.of("abc%u3040", "abc\u3040", "abc\u3040"), @@ -179,10 +180,10 @@ public void testCanonicalEncodedPath(String encodedPath, String canonicalPath, S @ParameterizedTest(name = "[{index}] {0}") @MethodSource("decodePathSource") - public void testDecodePath(String encodedPath, String canonicalPath, String decodedPath) + public void testDecodePath(String encodedPath, String expectedCanonicalPath, String expectedDecodedPath) { String path = URIUtil.decodePath(encodedPath); - assertEquals(decodedPath, path); + assertEquals(expectedDecodedPath, path); } public static Stream decodeBadPathSource()