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

Fix Apache HttpClient host + absolute uri #3694

Merged
merged 13 commits into from
Jul 29, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ public final class ApacheHttpClientRequest {
private final HttpRequest delegate;

public ApacheHttpClientRequest(HttpHost httpHost, HttpRequest httpRequest) {
URI calculatedUri;
try {
calculatedUri = new URI(httpHost.toURI() + httpRequest.getRequestLine().getUri());
} catch (URISyntaxException e) {
logger.debug(e.getMessage(), e);
calculatedUri = null;
}
uri = calculatedUri;
uri = getCalculatedUri(httpHost, httpRequest);
delegate = httpRequest;
}

Expand Down Expand Up @@ -121,4 +114,29 @@ public Integer getPeerPort() {
return null;
}
}

@Nullable
private static URI getCalculatedUri(HttpHost httpHost, HttpRequest httpRequest) {
final URI uri;
try {
// this can be relative or absolute
uri = new URI(httpRequest.getRequestLine().getUri());
trask marked this conversation as resolved.
Show resolved Hide resolved
} catch (URISyntaxException e) {
logger.debug(e.getMessage(), e);
return null;
}
try {
return new URI(
httpHost.getSchemeName(),
null,
httpHost.getHostName(),
httpHost.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment());
} catch (URISyntaxException e) {
logger.debug(e.getMessage(), e);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ abstract class ApacheHttpClientTest<T extends HttpRequest> extends HttpClientTes
}
return builder.toString()
}

static String absoluteUriWithNonResolvingHost(URI uri) {
// substituting a non-resolving host and port to make sure that the host and port from this uri are not used
return new URI(uri.getScheme(), uri.getAuthority(), "nowhere", 1, uri.getPath(), uri.getQuery(), uri.getFragment())
.toString()
}
}

class ApacheClientHostRequest extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicHttpRequest(method, fullPathFromURI(uri))
}

Expand All @@ -115,9 +122,29 @@ class ApacheClientHostRequest extends ApacheHttpClientTest<BasicHttpRequest> {
}
}

class ApacheClientHostAbsoluteUriRequest extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
return new BasicHttpRequest(method, absoluteUriWithNonResolvingHost(uri))
}

@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return client.execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request)
}

@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
client.execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request) {
callback.accept(it)
}
}
}

class ApacheClientHostRequestContext extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicHttpRequest(method, fullPathFromURI(uri))
}

Expand All @@ -134,6 +161,25 @@ class ApacheClientHostRequestContext extends ApacheHttpClientTest<BasicHttpReque
}
}

class ApacheClientHostAbsoluteUriRequestContext extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
return new BasicHttpRequest(method, absoluteUriWithNonResolvingHost(uri))
}

@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return client.execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, new BasicHttpContext())
}

@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
client.execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, {
callback.accept(it)
}, new BasicHttpContext())
}
}

class ApacheClientUriRequest extends ApacheHttpClientTest<HttpUriRequest> {
@Override
HttpUriRequest createRequest(String method, URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ abstract class ApacheHttpClientTest<T extends HttpRequest> extends HttpClientTes
class ApacheClientHostRequest extends ApacheHttpClientTest<ClassicHttpRequest> {
@Override
ClassicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicClassicHttpRequest(method, fullPathFromURI(uri))
}

Expand All @@ -120,9 +121,33 @@ class ApacheClientHostRequest extends ApacheHttpClientTest<ClassicHttpRequest> {
}
}

class ApacheClientHostAbsoluteUriRequest extends ApacheHttpClientTest<ClassicHttpRequest> {
@Override
ClassicHttpRequest createRequest(String method, URI uri) {
// TODO(trask) substitute a non-resolving host and port to make sure that the host and port
// from this uri are not used (currently that causes redirect tests to fail
// because Apache HttpClient 5 appears to resolve relative redirects against this uri
// instead of against the host, which is different from Apache HttpClient 4 behavior)
return new BasicClassicHttpRequest(method, uri.toString())
}

@Override
ClassicHttpResponse executeRequest(ClassicHttpRequest request, URI uri) {
return client.execute(new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort()), request)
}

@Override
void executeRequestWithCallback(ClassicHttpRequest request, URI uri, Consumer<ClassicHttpResponse> callback) {
client.execute(new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort()), request) {
callback.accept(it)
}
}
}

class ApacheClientHostRequestContext extends ApacheHttpClientTest<ClassicHttpRequest> {
@Override
ClassicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicClassicHttpRequest(method, fullPathFromURI(uri))
}

Expand All @@ -139,6 +164,29 @@ class ApacheClientHostRequestContext extends ApacheHttpClientTest<ClassicHttpReq
}
}

class ApacheClientHostAbsoluteUriRequestContext extends ApacheHttpClientTest<ClassicHttpRequest> {
@Override
ClassicHttpRequest createRequest(String method, URI uri) {
// TODO(trask) substitute a non-resolving host and port to make sure that the host and port
// from this uri are not used (currently that causes redirect tests to fail
// because Apache HttpClient 5 appears to resolve relative redirects against this uri
// instead of against the host, which is different from Apache HttpClient 4 behavior)
return new BasicClassicHttpRequest(method, uri.toString())
}

@Override
ClassicHttpResponse executeRequest(ClassicHttpRequest request, URI uri) {
return client.execute(new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort()), request, new BasicHttpContext())
}

@Override
void executeRequestWithCallback(ClassicHttpRequest request, URI uri, Consumer<ClassicHttpResponse> callback) {
client.execute(new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort()), request, new BasicHttpContext()) {
callback.accept(it)
}
}
}

class ApacheClientUriRequest extends ApacheHttpClientTest<ClassicHttpRequest> {
@Override
ClassicHttpRequest createRequest(String method, URI uri) {
Expand Down