diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java index 28a77e18ff9c..84f4114789ab 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java @@ -628,7 +628,7 @@ public String asString() */ public Mutable authority(String host, int port) { - if (host != null && _path != null && !_path.startsWith("/")) + if (host != null && !isPathValidForAuthority(_path)) throw new IllegalArgumentException("Relative path with authority"); _user = null; _host = host; @@ -643,7 +643,7 @@ public Mutable authority(String host, int port) */ public Mutable authority(String hostPort) { - if (hostPort != null && _path != null && !_path.startsWith("/")) + if (hostPort != null && !isPathValidForAuthority(_path)) throw new IllegalArgumentException("Relative path with authority"); HostPort hp = new HostPort(hostPort); _user = null; @@ -653,6 +653,15 @@ public Mutable authority(String hostPort) return this; } + private boolean isPathValidForAuthority(String path) + { + if (path == null) + return true; + if (path.isEmpty() || "*".equals(path)) + return true; + return path.startsWith("/"); + } + public Mutable clear() { _scheme = null; @@ -779,7 +788,7 @@ public int hashCode() public Mutable host(String host) { - if (host != null && _path != null && !_path.startsWith("/")) + if (host != null && !isPathValidForAuthority(_path)) throw new IllegalArgumentException("Relative path with authority"); _host = host; _uri = null; @@ -844,7 +853,7 @@ public Mutable param(String param) */ public Mutable path(String path) { - if (hasAuthority() && path != null && !path.startsWith("/")) + if (hasAuthority() && !isPathValidForAuthority(path)) throw new IllegalArgumentException("Relative path with authority"); _uri = null; _path = path; @@ -854,7 +863,7 @@ public Mutable path(String path) public Mutable pathQuery(String pathQuery) { - if (hasAuthority() && pathQuery != null && !pathQuery.startsWith("/")) + if (hasAuthority() && !isPathValidForAuthority(pathQuery)) throw new IllegalArgumentException("Relative path with authority"); _uri = null; _path = null; diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java index 4b3713f26c52..83869703fb4e 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java @@ -847,5 +847,23 @@ public void testRelativePathWithAuthority() assertThrows(IllegalArgumentException.class, () -> HttpURI.build() .path(";") .host("host")); + + HttpURI.Mutable uri = HttpURI.build() + .path("*") + .authority("host"); + assertEquals("//host*", uri.asString()); + uri = HttpURI.build() + .authority("host") + .path("*"); + assertEquals("//host*", uri.asString()); + + uri = HttpURI.build() + .path("") + .authority("host"); + assertEquals("//host", uri.asString()); + uri = HttpURI.build() + .authority("host") + .path(""); + assertEquals("//host", uri.asString()); } }