Skip to content

Commit

Permalink
Merge pull request #8579 from eclipse/fix/jetty-9.4.x-abstractproxy-n…
Browse files Browse the repository at this point in the history
…ull-request-url

Issue #8578 - `getRequestURL` can append "null" if `getRequestURI` is unspecified in an authority-form request-target
  • Loading branch information
joakime committed Sep 13, 2022
2 parents 06f2fa4 + 5944ff4 commit b32d739
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Expand Up @@ -102,10 +102,10 @@ public boolean isExpecting102Processing()
}

@Override
public boolean startRequest(String method, String uri, HttpVersion version)
public boolean startRequest(String method, String requestTarget, HttpVersion version)
{
_metadata.setMethod(method);
_metadata.getURI().parseRequestTarget(method, uri);
_metadata.getURI().parseRequestTarget(method, requestTarget);
_metadata.setHttpVersion(version);
_unknownExpectation = false;
_expect100Continue = false;
Expand All @@ -127,7 +127,7 @@ public void parsedHeader(HttpField field)
break;

case HOST:
if (!_metadata.getURI().isAbsolute() && field instanceof HostPortHttpField)
if (!HttpMethod.CONNECT.is(_metadata.getMethod()) && !_metadata.getURI().isAbsolute() && field instanceof HostPortHttpField)
{
HostPortHttpField hp = (HostPortHttpField)field;
_metadata.getURI().setAuthority(hp.getHost(), hp.getPort());
Expand Down
Expand Up @@ -1352,7 +1352,9 @@ public StringBuffer getRequestURL()
{
final StringBuffer url = new StringBuffer(128);
URIUtil.appendSchemeHostPort(url, getScheme(), getServerName(), getServerPort());
url.append(getRequestURI());
String path = getRequestURI();
if (path != null)
url.append(path);
return url;
}

Expand Down
Expand Up @@ -55,6 +55,7 @@
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpComplianceSection;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Connection;
Expand Down Expand Up @@ -83,6 +84,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -872,6 +874,52 @@ public boolean check(HttpServletRequest request, HttpServletResponse response)
assertEquals(" x=z; ", results.get(i++));
}

@Test
public void testConnectRequestURLSameAsHost() throws Exception
{
final AtomicReference<String> resultRequestURL = new AtomicReference<>();
final AtomicReference<String> resultRequestURI = new AtomicReference<>();
_handler._checker = (request, response) ->
{
resultRequestURL.set(request.getRequestURL().toString());
resultRequestURI.set(request.getRequestURI());
return true;
};

String rawResponse = _connector.getResponse(
"CONNECT myhost:9999 HTTP/1.1\n" +
"Host: myhost:9999\n" +
"Connection: close\n" +
"\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat("request.getRequestURL", resultRequestURL.get(), is("http://myhost:9999"));
assertThat("request.getRequestURI", resultRequestURI.get(), is(nullValue()));
}

@Test
public void testConnectRequestURLDifferentThanHost() throws Exception
{
final AtomicReference<String> resultRequestURL = new AtomicReference<>();
final AtomicReference<String> resultRequestURI = new AtomicReference<>();
_handler._checker = (request, response) ->
{
resultRequestURL.set(request.getRequestURL().toString());
resultRequestURI.set(request.getRequestURI());
return true;
};

String rawResponse = _connector.getResponse(
"CONNECT myhost:9999 HTTP/1.1\n" +
"Host: otherhost:8888\n" + // per spec, this is ignored if request-target is authority-form
"Connection: close\n" +
"\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat("request.getRequestURL", resultRequestURL.get(), is("http://myhost:9999"));
assertThat("request.getRequestURI", resultRequestURI.get(), is(nullValue()));
}

@Test
public void testHostPort() throws Exception
{
Expand Down

0 comments on commit b32d739

Please sign in to comment.