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

Issue #8578 - getRequestURL can append "null" if getRequestURI is unspecified in an authority-form request-target #8579

Merged
merged 3 commits into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" +
sbordet marked this conversation as resolved.
Show resolved Hide resolved
"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