Skip to content

Commit

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

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed May 23, 2022
1 parent c1cb2dc commit 2cd7c30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
18 changes: 14 additions & 4 deletions jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
Expand Up @@ -628,6 +628,8 @@ public String asString()
*/
public Mutable authority(String host, int port)
{
if (host != null && _path != null && !_path.startsWith("/"))
throw new IllegalArgumentException("Relative path with authority");
_user = null;
_host = host;
_port = port;
Expand All @@ -636,12 +638,14 @@ public Mutable authority(String host, int port)
}

/**
* @param hostport the host and port combined
* @param hostPort the host and port combined
* @return this mutable
*/
public Mutable authority(String hostport)
public Mutable authority(String hostPort)
{
HostPort hp = new HostPort(hostport);
if (hostPort != null && _path != null && !_path.startsWith("/"))
throw new IllegalArgumentException("Relative path with authority");
HostPort hp = new HostPort(hostPort);
_user = null;
_host = hp.getHost();
_port = hp.getPort();
Expand Down Expand Up @@ -775,6 +779,8 @@ public int hashCode()

public Mutable host(String host)
{
if (host != null && _path != null && !_path.startsWith("/"))
throw new IllegalArgumentException("Relative path with authority");
_host = host;
_uri = null;
return this;
Expand Down Expand Up @@ -834,10 +840,12 @@ public Mutable param(String param)

/**
* @param path the path
* @return this Mutuble
* @return this Mutable
*/
public Mutable path(String path)
{
if (hasAuthority() && path != null && !path.startsWith("/"))
throw new IllegalArgumentException("Relative path with authority");
_uri = null;
_path = path;
_decodedPath = null;
Expand All @@ -846,6 +854,8 @@ public Mutable path(String path)

public Mutable pathQuery(String pathQuery)
{
if (hasAuthority() && pathQuery != null && !pathQuery.startsWith("/"))
throw new IllegalArgumentException("Relative path with authority");
_uri = null;
_path = null;
_decodedPath = null;
Expand Down
24 changes: 24 additions & 0 deletions jetty-http/src/test/java/org/eclipse/jetty/http/HttpURITest.java
Expand Up @@ -824,4 +824,28 @@ public void testEncodedQuery(String input, String expectedQuery)
HttpURI httpURI = HttpURI.build(input);
assertThat("[" + input + "] .query", httpURI.getQuery(), is(expectedQuery));
}

@Test
public void testRelativePathWithAuthority()
{
assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.authority("host")
.path("path"));
assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.authority("host", 8080)
.path(";p=v/url"));
assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.host("host")
.path(";"));

assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.path("path")
.authority("host"));
assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.path(";p=v/url")
.authority("host", 8080));
assertThrows(IllegalArgumentException.class, () -> HttpURI.build()
.path(";")
.host("host"));
}
}

0 comments on commit 2cd7c30

Please sign in to comment.