Skip to content

Commit e664f31

Browse files
committed
Flip URIUtil.toURI behavior to perform URI before Path in the implementation
1 parent bacbdc2 commit e664f31

File tree

1 file changed

+25
-15
lines changed
  • jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util

1 file changed

+25
-15
lines changed

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.io.File;
1717
import java.io.IOException;
1818
import java.net.URI;
19-
import java.net.URISyntaxException;
2019
import java.net.URL;
2120
import java.net.URLClassLoader;
2221
import java.nio.charset.StandardCharsets;
@@ -1919,28 +1918,39 @@ public static URI toURI(String reference)
19191918

19201919
try
19211920
{
1922-
Path path = Path.of(reference).toAbsolutePath();
1923-
return path.toUri();
1921+
/* Perform URI test first.
1922+
* We don't want to perform Path.of(String) first.
1923+
*
1924+
* Example: reference parameter is the String "file:///path/to/dir"
1925+
*
1926+
* On Unix, the Path.of(reference) will result in a relative directory reference
1927+
* that includes the "file:" portion in the path after the current working directory.
1928+
* You'll wind up with something like "file:///home/user/code/jetty/12.1.x/jetty-core/jetty-util/file:///path/to/dir" in this case
1929+
*
1930+
* On Windows, the Path.of(reference) will not allow a Path.of("file:///path/to/dir") to work.
1931+
* This is because there cannot be multi-character drive letters (yes, Windows is limited to only 26 drive letters max)
1932+
*/
1933+
URI uri = URI.create(reference);
1934+
if (uri.isAbsolute())
1935+
return uri;
1936+
if (LOG.isDebugEnabled())
1937+
LOG.debug("Input string is detected as a non-absolute URI \"{}\"", reference);
1938+
throw new IllegalArgumentException("Non-absolute URI reference strings not supported");
19241939
}
1925-
catch (InvalidPathException e)
1940+
catch (IllegalArgumentException e)
19261941
{
1927-
// Not a path reference.
1928-
LOG.trace("ignored", e);
1942+
LOG.trace("IGNORED: Invalid as URI Reference: {}", reference, e);
19291943
}
19301944

19311945
try
19321946
{
1933-
URI uri = new URI(reference);
1934-
if (uri.isAbsolute())
1935-
return uri;
1936-
if (LOG.isDebugEnabled())
1937-
LOG.debug("Input string is detected as a non-absolute URI \"{}\"", reference);
1938-
throw new IllegalArgumentException("Non-absolute URI reference strings not supported");
1947+
Path path = Path.of(reference).toAbsolutePath();
1948+
return path.toUri();
19391949
}
1940-
catch (URISyntaxException x)
1950+
catch (InvalidPathException e)
19411951
{
1942-
// Not a URI reference either.
1943-
LOG.trace("ignored", x);
1952+
// Not a path reference.
1953+
LOG.trace("IGNORED: Invalid as Path Reference: {}", reference, e);
19441954
}
19451955

19461956
// If we reached this here, that means the input string cannot be used as

0 commit comments

Comments
 (0)