Skip to content

Commit

Permalink
Fixes #8014 - Review HttpRequest URI construction.
Browse files Browse the repository at this point in the history
More fixes after review in HttpURI.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed May 23, 2022
1 parent 2cd7c30 commit 9b4f40d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
19 changes: 14 additions & 5 deletions jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 9b4f40d

Please sign in to comment.