You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2024/07/28 13:13:39 UTC

(httpcomponents-client) branch HTTPCLIENT-2333 created (now 102174217)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch HTTPCLIENT-2333
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


      at 102174217 HTTPCLIENT-2333: update execution scope upon request redirect in order to avoid re-execution of the original request in case of an i/o error

This branch includes the following new commits:

     new 102174217 HTTPCLIENT-2333: update execution scope upon request redirect in order to avoid re-execution of the original request in case of an i/o error

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(httpcomponents-client) 01/01: HTTPCLIENT-2333: update execution scope upon request redirect in order to avoid re-execution of the original request in case of an i/o error

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch HTTPCLIENT-2333
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 102174217d2cf990ca04a00066d95f92e5efb985
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Jul 27 12:30:53 2024 +0200

    HTTPCLIENT-2333: update execution scope upon request redirect in order to avoid re-execution of the original request in case of an i/o error
---
 .../hc/client5/testing/sync/TestRedirects.java     | 46 ++++++++++++++++++++++
 .../client5/http/impl/async/AsyncRedirectExec.java | 35 ++++++++--------
 .../hc/client5/http/impl/classic/RedirectExec.java | 23 ++++++-----
 .../http/impl/classic/TestRedirectExec.java        |  4 +-
 4 files changed, 75 insertions(+), 33 deletions(-)

diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java
index a881d1750..3edac7c5c 100644
--- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java
+++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestRedirects.java
@@ -33,6 +33,7 @@ import java.net.URI;
 import java.util.Collections;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
 
 import org.apache.hc.client5.http.CircularRedirectException;
@@ -56,6 +57,7 @@ import org.apache.hc.client5.testing.redirect.Redirect;
 import org.apache.hc.client5.testing.sync.extension.TestClientResources;
 import org.apache.hc.core5.function.Decorator;
 import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHeaders;
@@ -64,6 +66,7 @@ import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -654,4 +657,47 @@ public abstract class TestRedirects {
         assertThat(values.poll(), CoreMatchers.nullValue());
     }
 
+    @Test
+    public void testRetryUponRedirect() throws Exception {
+        final ClassicTestServer server = startServer(null, requestHandler -> new RedirectingDecorator(
+                requestHandler,
+                new OldPathRedirectResolver("/oldlocation", "/random", HttpStatus.SC_MOVED_TEMPORARILY)));
+        server.registerHandler("/random/*", new HttpRequestHandler() {
+
+            final AtomicLong count = new AtomicLong();
+
+            @Override
+            public void handle(final ClassicHttpRequest request,
+                               final ClassicHttpResponse response,
+                               final HttpContext context) throws HttpException, IOException {
+                if (count.incrementAndGet() == 1) {
+                    throw new IOException("Boom");
+                } else {
+                    response.setCode(200);
+                    response.setEntity(new StringEntity("test"));
+                }
+            }
+
+        });
+        final HttpHost target = targetHost();
+
+        final CloseableHttpClient client = startClient();
+        final HttpClientContext context = HttpClientContext.create();
+
+        final HttpGet httpget = new HttpGet("/oldlocation/50");
+
+        client.execute(target, httpget, context, response -> {
+            Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
+            EntityUtils.consume(response.getEntity());
+            return null;
+        });
+        final HttpRequest reqWrapper = context.getRequest();
+
+        Assertions.assertEquals(new URIBuilder()
+                        .setHttpHost(target)
+                        .setPath("/random/50")
+                        .build(),
+                reqWrapper.getUri());
+    }
+
 }
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
index 225ff1efb..ea7fca231 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java
@@ -90,7 +90,6 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
         volatile URI redirectURI;
         volatile int maxRedirects;
         volatile int redirectCount;
-        volatile HttpRequest originalRequest;
         volatile HttpRequest currentRequest;
         volatile AsyncEntityProducer currentEntityProducer;
         volatile RedirectLocations redirectLocations;
@@ -109,7 +108,6 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
         final AsyncExecChain.Scope scope = state.currentScope;
         final HttpClientContext clientContext = scope.clientContext;
         final String exchangeId = scope.exchangeId;
-        final HttpRoute currentRoute = scope.route;
         chain.proceed(request, entityProducer, scope, new AsyncExecCallback() {
 
             @Override
@@ -137,6 +135,7 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
                     }
                     state.redirectLocations.add(redirectUri);
 
+                    final AsyncExecChain.Scope currentScope = state.currentScope;
                     final HttpHost newTarget = URIUtils.extractHost(redirectUri);
                     if (newTarget == null) {
                         throw new ProtocolException("Redirect URI does not specify a valid host name: " + redirectUri);
@@ -151,7 +150,7 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
                                 redirectBuilder = BasicRequestBuilder.get();
                                 state.currentEntityProducer = null;
                             } else {
-                                redirectBuilder = BasicRequestBuilder.copy(state.originalRequest);
+                                redirectBuilder = BasicRequestBuilder.copy(currentScope.originalRequest);
                             }
                             break;
                         case HttpStatus.SC_SEE_OTHER:
@@ -159,18 +158,18 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
                                 redirectBuilder = BasicRequestBuilder.get();
                                 state.currentEntityProducer = null;
                             } else {
-                                redirectBuilder = BasicRequestBuilder.copy(state.originalRequest);
+                                redirectBuilder = BasicRequestBuilder.copy(currentScope.originalRequest);
                             }
                             break;
                         default:
-                            redirectBuilder = BasicRequestBuilder.copy(state.originalRequest);
+                            redirectBuilder = BasicRequestBuilder.copy(currentScope.originalRequest);
                     }
                     redirectBuilder.setUri(redirectUri);
                     state.reroute = false;
                     state.redirectURI = redirectUri;
-                    state.originalRequest = redirectBuilder.build();
                     state.currentRequest = redirectBuilder.build();
 
+                    HttpRoute currentRoute = currentScope.route;
                     if (!Objects.equals(currentRoute.getTargetHost(), newTarget)) {
                         final HttpRoute newRoute = routePlanner.determineRoute(newTarget, clientContext);
                         if (!Objects.equals(currentRoute, newRoute)) {
@@ -189,21 +188,20 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
                                     proxyAuthExchange.reset();
                                 }
                             }
-                            state.currentScope = new AsyncExecChain.Scope(
-                                    scope.exchangeId,
-                                    newRoute,
-                                    scope.originalRequest,
-                                    scope.cancellableDependency,
-                                    scope.clientContext,
-                                    scope.execRuntime,
-                                    scope.scheduler,
-                                    scope.execCount);
+                            currentRoute = newRoute;
                         }
                     }
-                }
-                if (state.redirectURI != null) {
+                    state.currentScope = new AsyncExecChain.Scope(
+                            scope.exchangeId,
+                            currentRoute,
+                            redirectBuilder.build(),
+                            scope.cancellableDependency,
+                            scope.clientContext,
+                            scope.execRuntime,
+                            scope.scheduler,
+                            scope.execCount);
                     if (LOG.isDebugEnabled()) {
-                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, state.redirectURI, currentRoute);
+                        LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, currentRoute);
                     }
                     return null;
                 }
@@ -272,7 +270,6 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler {
         final State state = new State();
         state.maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50;
         state.redirectCount = 0;
-        state.originalRequest = scope.originalRequest;
         state.currentRequest = request;
         state.currentEntityProducer = entityProducer;
         state.redirectLocations = redirectLocations;
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
index 827c0c7e1..cd62eee39 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java
@@ -108,7 +108,6 @@ public final class RedirectExec implements ExecChainHandler {
 
         final RequestConfig config = context.getRequestConfig();
         final int maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50;
-        ClassicHttpRequest originalRequest = scope.originalRequest;
         ClassicHttpRequest currentRequest = request;
         ExecChain.Scope currentScope = scope;
         for (int redirectCount = 0;;) {
@@ -154,22 +153,22 @@ public final class RedirectExec implements ExecChainHandler {
                             if (Method.POST.isSame(request.getMethod())) {
                                 redirectBuilder = ClassicRequestBuilder.get();
                             } else {
-                                redirectBuilder = ClassicRequestBuilder.copy(originalRequest);
+                                redirectBuilder = ClassicRequestBuilder.copy(currentScope.originalRequest);
                             }
                             break;
                         case HttpStatus.SC_SEE_OTHER:
                             if (!Method.GET.isSame(request.getMethod()) && !Method.HEAD.isSame(request.getMethod())) {
                                 redirectBuilder = ClassicRequestBuilder.get();
                             } else {
-                                redirectBuilder = ClassicRequestBuilder.copy(originalRequest);
+                                redirectBuilder = ClassicRequestBuilder.copy(currentScope.originalRequest);
                             }
                             break;
                         default:
-                            redirectBuilder = ClassicRequestBuilder.copy(originalRequest);
+                            redirectBuilder = ClassicRequestBuilder.copy(currentScope.originalRequest);
                     }
                     redirectBuilder.setUri(redirectUri);
 
-                    final HttpRoute currentRoute = currentScope.route;
+                    HttpRoute currentRoute = currentScope.route;
                     if (!Objects.equals(currentRoute.getTargetHost(), newTarget)) {
                         final HttpRoute newRoute = this.routePlanner.determineRoute(newTarget, context);
                         if (!Objects.equals(currentRoute, newRoute)) {
@@ -190,19 +189,19 @@ public final class RedirectExec implements ExecChainHandler {
                                     proxyAuthExchange.reset();
                                 }
                             }
-                            currentScope = new ExecChain.Scope(
-                                    currentScope.exchangeId,
-                                    newRoute,
-                                    currentScope.originalRequest,
-                                    currentScope.execRuntime,
-                                    currentScope.clientContext);
+                            currentRoute = newRoute;
                         }
                     }
 
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("{} redirecting to '{}' via {}", exchangeId, redirectUri, currentRoute);
                     }
-                    originalRequest = redirectBuilder.build();
+                    currentScope = new ExecChain.Scope(
+                            scope.exchangeId,
+                            currentRoute,
+                            redirectBuilder.build(),
+                            scope.execRuntime,
+                            scope.clientContext);
                     currentRequest = redirectBuilder.build();
                     RequestEntityProxy.enhance(currentRequest);
 
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestRedirectExec.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestRedirectExec.java
index 5095497c3..ba5329a2f 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestRedirectExec.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestRedirectExec.java
@@ -122,7 +122,7 @@ public class TestRedirectExec {
         redirectExec.execute(request, scope, chain);
 
         final ArgumentCaptor<ClassicHttpRequest> reqCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class);
-        Mockito.verify(chain, Mockito.times(2)).proceed(reqCaptor.capture(), ArgumentMatchers.same(scope));
+        Mockito.verify(chain, Mockito.times(2)).proceed(reqCaptor.capture(), ArgumentMatchers.any());
 
         final List<ClassicHttpRequest> allValues = reqCaptor.getAllValues();
         Assertions.assertNotNull(allValues);
@@ -395,7 +395,7 @@ public class TestRedirectExec {
         Assertions.assertEquals(200, finalResponse.getCode());
 
         final ArgumentCaptor<ClassicHttpRequest> reqCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class);
-        Mockito.verify(chain, Mockito.times(3)).proceed(reqCaptor.capture(), ArgumentMatchers.same(scope));
+        Mockito.verify(chain, Mockito.times(3)).proceed(reqCaptor.capture(), ArgumentMatchers.any());
 
         final List<ClassicHttpRequest> allValues = reqCaptor.getAllValues();
         Assertions.assertNotNull(allValues);