Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #8014 - Review HttpRequest URI construction. #8015

Merged
merged 5 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public Request path(String path)
String rawPath = uri.getRawPath();
if (rawPath == null)
rawPath = "";
if (!rawPath.startsWith("/"))
rawPath = "/" + rawPath;
this.path = rawPath;
String query = uri.getRawQuery();
if (query != null)
Expand Down Expand Up @@ -949,14 +951,14 @@ private URI buildURI(boolean withQuery)
return result;
}

private URI newURI(String uri)
private URI newURI(String path)
{
try
{
// Handle specially the "OPTIONS *" case, since it is possible to create a URI from "*" (!).
if ("*".equals(uri))
if ("*".equals(path))
return null;
URI result = new URI(uri);
URI result = new URI(path);
return result.isOpaque() ? null : result;
}
catch (URISyntaxException x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -36,6 +38,8 @@
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.UriCompliance;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.toolchain.test.Net;
import org.eclipse.jetty.util.Fields;
Expand Down Expand Up @@ -77,6 +81,52 @@ public void testIPv6Host(Scenario scenario) throws Exception
assertEquals(HttpStatus.OK_200, request.send().getStatus());
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testPathWithPathParameter(Scenario scenario) throws Exception
{
AtomicReference<CountDownLatch> serverLatchRef = new AtomicReference<>();
start(scenario, new EmptyServerHandler()
{
@Override
protected void service(String target, org.eclipse.jetty.server.Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
if (jettyRequest.getHttpURI().hasAmbiguousEmptySegment())
response.setStatus(400);
serverLatchRef.get().countDown();
}
});
// Allow empty segments to test them.
connector.getContainedBeans(HttpConfiguration.class)
.forEach(httpConfig -> httpConfig.setUriCompliance(UriCompliance.from("DEFAULT,AMBIGUOUS_EMPTY_SEGMENT")));

serverLatchRef.set(new CountDownLatch(1));
ContentResponse response1 = client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme())
.path("/url;p=v")
.send();
assertEquals(HttpStatus.OK_200, response1.getStatus());
assertTrue(serverLatchRef.get().await(5, TimeUnit.SECONDS));

// Ambiguous empty segment.
serverLatchRef.set(new CountDownLatch(1));
ContentResponse response2 = client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme())
.path(";p=v/url")
.send();
assertEquals(HttpStatus.BAD_REQUEST_400, response2.getStatus());
assertTrue(serverLatchRef.get().await(5, TimeUnit.SECONDS));

// Ambiguous empty segment.
serverLatchRef.set(new CountDownLatch(1));
ContentResponse response3 = client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme())
.path(";@host.org/url")
.send();
assertEquals(HttpStatus.BAD_REQUEST_400, response3.getStatus());
assertTrue(serverLatchRef.get().await(5, TimeUnit.SECONDS));
}

@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testIDNHost(Scenario scenario) throws Exception
Expand Down
32 changes: 24 additions & 8 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,6 +628,8 @@ public String asString()
*/
public Mutable authority(String host, int port)
{
if (host != null && !isPathValidForAuthority(_path))
throw new IllegalArgumentException("Relative path with authority");
_user = null;
_host = host;
_port = port;
Expand All @@ -636,19 +638,30 @@ 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 && !isPathValidForAuthority(_path))
throw new IllegalArgumentException("Relative path with authority");
HostPort hp = new HostPort(hostPort);
_user = null;
_host = hp.getHost();
_port = hp.getPort();
_uri = null;
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 @@ -775,6 +788,8 @@ public int hashCode()

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

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

public Mutable pathQuery(String pathQuery)
{
if (hasAuthority() && !isPathValidForAuthority(pathQuery))
throw new IllegalArgumentException("Relative path with authority");
_uri = null;
_path = null;
_decodedPath = null;
Expand Down Expand Up @@ -911,10 +930,7 @@ public Mutable uri(HttpURI uri)
_query = uri.getQuery();
_uri = null;
_decodedPath = uri.getDecodedPath();
if (uri.hasAmbiguousSeparator())
_violations.add(Violation.AMBIGUOUS_PATH_SEPARATOR);
if (uri.hasAmbiguousSegment())
_violations.add(Violation.AMBIGUOUS_PATH_SEGMENT);
_violations.addAll(uri.getViolations());
return this;
}

Expand Down
42 changes: 42 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 @@ -824,4 +824,46 @@ 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"));

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public void testIterative(Transport transport) throws Exception
public void testConcurrent(Transport transport) throws Exception
{
// TODO: cannot run HTTP/3 (or UDP) in Jenkins.
if ("ci".equals(System.getProperty("env")))
Assumptions.assumeTrue(transport != Transport.H3);
Assumptions.assumeTrue(transport != Transport.H3);

init(transport);
scenario.start(new LoadHandler(), client ->
Expand Down