From f6da2bac6fc891db4f7da9af9158068f3dc87e3d Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 9 Aug 2020 19:13:18 +0200 Subject: [PATCH] HTTPCLIENT-2105: async clients incorrectly handle redirects of requests with enclosed entity --- .../async/AbstractHttpAsyncRedirectsTest.java | 28 +++++++++++++++++++ .../http/impl/async/AsyncRedirectExec.java | 3 ++ 2 files changed, 31 insertions(+) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncRedirectsTest.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncRedirectsTest.java index 185722973..31f11d3cc 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncRedirectsTest.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncRedirectsTest.java @@ -376,6 +376,34 @@ public AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler exch } } + @Test + public void testPostRedirect() throws Exception { + final HttpHost target = start(new Decorator() { + + @Override + public AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler exchangeHandler) { + return new RedirectingAsyncDecorator( + exchangeHandler, + new OldPathRedirectResolver("/oldlocation", "/echo", HttpStatus.SC_TEMPORARY_REDIRECT)); + } + + }); + + final HttpClientContext context = HttpClientContext.create(); + + final SimpleHttpRequest post = SimpleHttpRequests.post(target, "/oldlocation/stuff"); + post.setBody("stuff", ContentType.TEXT_PLAIN); + final Future future = httpclient.execute(post, context, null); + final HttpResponse response = future.get(); + Assert.assertNotNull(response); + + final HttpRequest request = context.getRequest(); + + Assert.assertEquals(HttpStatus.SC_OK, response.getCode()); + Assert.assertEquals("/echo/stuff", request.getRequestUri()); + Assert.assertEquals("POST", request.getMethod()); + } + @Test public void testPostRedirectSeeOther() throws Exception { final HttpHost target = start(new Decorator() { 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 2803fc619..c2b331c6e 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 @@ -209,6 +209,9 @@ public void completed() { asyncExecCallback.completed(); } else { final AsyncEntityProducer entityProducer = state.currentEntityProducer; + if (entityProducer != null) { + entityProducer.releaseResources(); + } if (entityProducer != null && !entityProducer.isRepeatable()) { if (LOG.isDebugEnabled()) { LOG.debug("{}: cannot redirect non-repeatable request", exchangeId);