Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@
*/
package org.apache.hc.client5.testing.async;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hc.client5.http.CircularRedirectException;
import org.apache.hc.client5.http.RedirectException;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.CookieStore;
Expand All @@ -57,8 +64,12 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.TimeValue;
Expand Down Expand Up @@ -411,6 +422,55 @@ void testPostRedirectSeeOther() throws Exception {
Assertions.assertEquals("GET", request.getMethod());
}

@ParameterizedTest(name = "{0}")
@ValueSource(ints = {
HttpStatus.SC_MOVED_PERMANENTLY,
HttpStatus.SC_MOVED_TEMPORARILY,
HttpStatus.SC_SEE_OTHER,
HttpStatus.SC_TEMPORARY_REDIRECT,
HttpStatus.SC_PERMANENT_REDIRECT})
void testPostRedirectNonRepeatableEntity(final int statusCode) throws Exception {
final AtomicInteger redirectedRequests = new AtomicInteger();
configureServer(bootstrap -> bootstrap
.register("/echo/*", () -> {
redirectedRequests.incrementAndGet();
return new AsyncEchoHandler();
})
.setExchangeHandlerDecorator(exchangeHandler -> new RedirectingAsyncDecorator(
exchangeHandler,
new OldPathRedirectResolver("/oldlocation", "/echo", statusCode))));
final HttpHost target = startServer();

final TestAsyncClient client = startClient();

final AtomicInteger bodyWrites = new AtomicInteger();
final AsyncEntityProducer entityProducer = AsyncEntityProducers.createBinary(channel -> {
bodyWrites.incrementAndGet();
try {
channel.write(ByteBuffer.wrap("stuff".getBytes(StandardCharsets.US_ASCII)));
channel.endStream();
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}, ContentType.TEXT_PLAIN);
Assertions.assertFalse(entityProducer.isRepeatable());

final HttpClientContext context = HttpClientContext.create();
final Future<SimpleHttpResponse> future = client.execute(
new BasicRequestProducer(Method.POST, target, "/oldlocation/stuff", entityProducer),
SimpleResponseConsumer.create(), context, null);
final HttpResponse response = future.get(10, TimeUnit.SECONDS);
Assertions.assertNotNull(response);

final HttpRequest request = context.getRequest();

Assertions.assertEquals(statusCode, response.getCode());
Assertions.assertEquals("/oldlocation/stuff", request.getRequestUri());
Assertions.assertEquals("POST", request.getMethod());
Assertions.assertEquals(1, bodyWrites.get());
Assertions.assertEquals(0, redirectedRequests.get());
}

@Test
void testRelativeRedirect() throws Exception {
configureServer(bootstrap -> bootstrap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public AsyncDataConsumer handleResponse(
state.redirectURI = null;
final RequestConfig config = clientContext.getRequestConfigOrDefault();
if (config.isRedirectsEnabled() && redirectStrategy.isRedirected(request, response, clientContext)) {
if (entityProducer != null && !entityProducer.isRepeatable()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cannot redirect non-repeatable request", exchangeId);
}
return asyncExecCallback.handleResponse(response, entityDetails);
}
if (state.redirectCount >= state.maxRedirects) {
throw new RedirectException("Maximum redirects (" + state.maxRedirects + ") exceeded");
}
Expand Down Expand Up @@ -238,20 +244,13 @@ public void completed() {
if (entityProducer != null) {
entityProducer.releaseResources();
}
if (entityProducer != null && !entityProducer.isRepeatable()) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cannot redirect non-repeatable request", exchangeId);
}
asyncExecCallback.completed();
} else {
try {
if (state.reroute) {
scope.execRuntime.releaseEndpoint();
}
internalExecute(state, chain, asyncExecCallback);
} catch (final IOException | HttpException ex) {
asyncExecCallback.failed(ex);
try {
if (state.reroute) {
scope.execRuntime.releaseEndpoint();
}
internalExecute(state, chain, asyncExecCallback);
} catch (final IOException | HttpException ex) {
asyncExecCallback.failed(ex);
}
}
}
Expand Down
Loading