diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index acdf0c8ff..cc3eae562 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -46,11 +46,13 @@ import org.asynchttpclient.AsyncHandler; import org.asynchttpclient.AsyncHttpClientConfig; import org.asynchttpclient.AsyncHttpClientState; +import org.asynchttpclient.DefaultRequest; import org.asynchttpclient.ListenableFuture; import org.asynchttpclient.Realm; import org.asynchttpclient.Realm.AuthScheme; import org.asynchttpclient.Request; import org.asynchttpclient.RequestBuilder; +import org.asynchttpclient.RequestBuilderBase; import org.asynchttpclient.LoadBalance; import org.asynchttpclient.StoreCookies; import org.asynchttpclient.exception.FilterException; @@ -180,6 +182,11 @@ public ListenableFuture sendRequest(Request request, final AsyncHandler futur if (!headers.contains(HttpHeaderNames.PROXY_AUTHORIZATION)) { return request; } - Request withoutProxyAuthorization = request.toBuilder() + // Exact class only: a subclass or another Request implementation may override what toBuilder() copies. + RequestBuilderBase builder = request.getClass() == DefaultRequest.class + ? new PrototypeRequestBuilder(request) : request.toBuilder(); + Request withoutProxyAuthorization = builder .setHeaders(headers.copy().remove(HttpHeaderNames.PROXY_AUTHORIZATION)) .build(); future.setTargetRequest(withoutProxyAuthorization); @@ -1998,4 +2008,15 @@ public void call() { } }); } + + /** + * Copies a request as {@link Request#toBuilder()} does, but keeps its URI as it is: {@code toBuilder()} encodes + * the URI again, which changes one built with URL encoding disabled. + */ + private static final class PrototypeRequestBuilder extends RequestBuilderBase { + + private PrototypeRequestBuilder(Request prototype) { + super(prototype, true, false); + } + } } diff --git a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java index 360c6529c..ee7b823f3 100644 --- a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java +++ b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java @@ -16,6 +16,7 @@ package org.asynchttpclient.netty.request; import io.github.nettyplus.leakdetector.junit.NettyLeakDetectorExtension; +import io.netty.handler.codec.http.DefaultHttpHeaders; import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.DefaultAsyncHttpClientConfig; import org.asynchttpclient.ListenableFuture; @@ -322,6 +323,14 @@ void aSelectorThatAlternatesKeepsATunnelOnTheProxyThatOpenedIt() throws Exceptio assertEquals("over-tls", response(outcome).getResponseBody()); assertEquals(1, proxyA.seen.size() + proxyB.seen.size(), () -> "a tunnel was opened again: a=" + proxyA.seen + " b=" + proxyB.seen); + + // Selected through A again, so it takes the tunnel A opened from the pool. + Object next = outcome(client.prepareGet("https://127.0.0.1:" + origin.port() + "/y").execute()); + + assertEquals("over-tls", response(next).getResponseBody()); + assertEquals(1, proxyA.seen.size() + proxyB.seen.size(), + () -> "the tunnel was not pooled under the proxy that opened it: a=" + proxyA.seen + + " b=" + proxyB.seen); } } } @@ -539,6 +548,32 @@ void aProxyLoginTheRequestCarriesIsNotTunnelledToTheOriginAfterAMove() throws Ex } } + @Test + void aRequestMovedOffAProxyKeepsAUriBuiltWithoutEncoding() throws Exception { + AtomicBoolean down = new AtomicBoolean(); + try (Peer proxyA = new Peer(); Peer proxyB = new Peer().answer(s -> ok("via-b"))) { + proxyA.answer(s -> { + down.set(true); + return DROP; + }); + ProxyServer a = proxyServer("127.0.0.1", proxyA.port()).build(); + ProxyServer b = proxyServer("127.0.0.1", proxyB.port()).build(); + + try (AsyncHttpClient client = asyncHttpClient(client() + .setMaxRequestRetry(1) + .setDisableUrlEncodingForBoundRequests(true) + .setProxyServerSelector(uri -> down.get() ? b : a))) { + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxyA.port() + "/a|b?q={x}") + .setHeader("Proxy-Authorization", ALICE).execute()); + + assertEquals("via-b", response(outcome).getResponseBody()); + assertEquals(proxyA.seen.get(0).requestLine, proxyB.seen.get(0).requestLine, + "the request was encoded again when it moved to proxy B"); + assertNull(proxyB.seen.get(0).proxyAuthorization(), proxyB.seen::toString); + } + } + } + @Test void anAnswerToAnOriginChallengeGoesBackThroughTheSameProxy() throws Exception { AtomicInteger requests = new AtomicInteger(); @@ -641,6 +676,123 @@ public FilterContext filter(FilterContext ctx) { } } + @Test + void aRedirectAfterAProxyChallengeKeepsAnotherProxysLoginOutOfAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxyA = new Peer(); Peer proxyB = new Peer()) { + proxyA.answer(s -> ALICE.equals(s.proxyAuthorization()) + ? redirect("https://localhost:" + origin.port() + "/landing", false) : challenge("Basic realm=\"a\"")); + proxyB.answer(s -> s.connect() && BOB.equals(s.proxyAuthorization()) ? TUNNEL : status(502)); + ProxyServer a = proxy(proxyA, "alice", "A-secret"); + ProxyServer b = proxyServer("127.0.0.1", proxyB.port()) + .setRealm(basicAuthRealm("bob", "B-secret").setUsePreemptiveAuth(true)).build(); + + try (AsyncHttpClient client = asyncHttpClient(client() + .setProxyServerSelector(uri -> uri.isSecured() ? b : a))) { + // Pools a tunnel to the origin through B. + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxyA.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(s -> s.connect() || s.proxyAuthorization() != null), + () -> "proxy B's CONNECT went into the tunnel to the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), + () -> "the redirect did not take the pooled tunnel: " + origin.seen); + assertEquals(1, proxyB.seen.size(), proxyB.seen::toString); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeTakesAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(proxy(proxy, "alice", "A-secret")))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(Seen::connect), + () -> "a CONNECT went into the tunnel to the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), + () -> "the redirect did not take the pooled tunnel: " + origin.seen); + assertTrue(proxy.seen.stream().allMatch(s -> s.connection <= 2), + () -> "the redirect opened another connection to the proxy: " + proxy.seen); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeReusesATunnelAtTheConnectionLimit() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setMaxConnections(2) + .setProxyServer(proxy(proxy, "alice", "A-secret")))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), origin.seen::toString); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeKeepsTheProxysHeadersOutOfAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + ProxyServer withHeader = proxyServer("127.0.0.1", proxy.port()) + .setRealm(basicAuthRealm("alice", "A-secret")) + .setCustomHeaders(r -> new DefaultHttpHeaders().add("X-Proxy-Token", "proxy-secret")) + .build(); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(withHeader))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(s -> s.headers.containsKey("x-proxy-token")), + () -> "the proxy's header reached the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeOpensATunnelWhenNoneIsPooled() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(proxy(proxy, "alice", "A-secret")))) { + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertEquals("/landing", response(outcome).getResponseBody(), proxy.seen::toString); + assertTrue(origin.seen.stream().noneMatch(Seen::connect), origin.seen::toString); + } + } + } + @Test void aRedirectFromHttpsToHttpThroughTheSameProxyStillAuthenticatesToIt() throws Exception { try (Peer origin = new Peer(serverTls()); Peer proxy = new Peer()) {