Skip to content

AsyncRedirectExec: return the redirect response when the request entity can't be repeated - #945

Merged
ok2c merged 1 commit into
apache:masterfrom
shinseongsu:async-redirect-non-repeatable
Nov 19, 2026
Merged

ok2c merged 1 commit into
apache:masterfrom
shinseongsu:async-redirect-non-repeatable

Conversation

@shinseongsu

Copy link
Copy Markdown
Contributor

The async client never completes a request with a non-repeatable entity when the server answers 307 or 308 (or 301/302 for a non-POST method). Neither the callback nor the future is ever called.

AsyncEntityProducer producer = AsyncEntityProducers.createBinary(channel -> {
    try {
        channel.write(ByteBuffer.wrap("hello".getBytes(StandardCharsets.US_ASCII)));
        channel.endStream();
    } catch (final IOException ex) {
        throw new UncheckedIOException(ex);
    }
}, ContentType.TEXT_PLAIN);

Future<SimpleHttpResponse> future = client.execute(
        new BasicRequestProducer(Method.POST, URI.create("http://localhost:8080/a"), producer),
        SimpleResponseConsumer.create(), null);
SimpleHttpResponse response = future.get(5, TimeUnit.SECONDS);   // /a returns 308 Location: /b
master this PR
POST, 307 TimeoutException, future never completes 307 returned
POST, 308 TimeoutException, future never completes 308 returned

Same with ReactiveEntityProducer. I first hit it through Spring's WebClient with HttpComponentsClientHttpConnector: request bodies there are always non-repeatable, so a POST to an endpoint that answers 308 (http -> https, or a trailing-slash redirect) just hangs until a timeout cancels it.

AsyncRedirectExec decides in handleResponse() that the response is a redirect and hands back a discarding consumer, so the response never reaches the caller. Only in completed() does it notice the entity can't be repeated, and then it calls asyncExecCallback.completed() without a response. The classic RedirectExec returns the redirect response in this case, and AsyncHttpRequestRetryExec does the same for a non-repeatable entity (asyncExecCallback.handleResponse(response, entityDetails)).

The change moves the repeatability check into handleResponse(), after the method rewrite, and passes the response on when the redirect can't be followed. A 303, or a 301/302 of a POST, is still followed because the redirect is a GET without the entity.

Added testRedirectNonRepeatableEntity (301 / 307 / 308) to AbstractHttpAsyncRedirectsTest. It times out on master and passes with the change.

I went with returning the redirect response because that's what the classic client already does. Other clients handle this case in one of two ways:

  • Return the 307/308 response to the caller: OkHttp (RetryAndFollowUpInterceptor, one-shot body), Go net/http (redirectBehavior, GetBody nil), Rust reqwest (tower-http FollowRedirect, body not cloneable).
  • Fail the request: curl (upload rewind failed), Python requests (UnrewindableBodyError), and fetch per the Fetch spec, e.g. undici in Node.js (network error when the body has no source).

Is returning the response what you want here, or would you prefer the exchange to fail with an exception (for example a RedirectException) so the caller can tell the redirect wasn't followed?

@arturobernalg

Copy link
Copy Markdown
Member

The fix looks reasonable and belongs in AsyncRedirectExec.

However, I would like to see the non-repeatable entity case handled before redirect URI validation and redirect limit checks, consistently with the classic implementation.

The regression test also needs to cover POST with 307/308 and method-changing 301/302/303 redirects. We should verify that the original response is delivered and the request entity is never replayed when it cannot be repeated.

@ok2c

ok2c commented Nov 17, 2026

Copy link
Copy Markdown
Member

Is returning the response what you want here, or would you prefer the exchange to fail with an exception (for example a RedirectException) so the caller can tell the redirect wasn't followed?

@shinseongsu The behavior of the async transport should be consistent with that of the classic one. Your current approach is correct.

@shinseongsu

Copy link
Copy Markdown
Contributor Author

Thanks @arturobernalg, @ok2c.

I moved the non-repeatable check up so it runs right after isRedirected(), before the redirect limit and location checks, same as RedirectExec. One behavior change came with it: a POST with a non-repeatable body used to be followed as a GET on 301/302/303, and now it gets the redirect response back, which is what classic does.

The test now sends a POST for 301, 302, 303, 307 and 308. It checks that the 3xx response comes back, that the body producer ran once, and that the redirect target never got a request. With the previous commit, the 301/302/303 cases fail on all four transports.

Does that match what you had in mind for the method-changing codes?

@ok2c

ok2c commented Nov 18, 2026

Copy link
Copy Markdown
Member

@shinseongsu Please squash the commits.

…ty can't be repeated

The non-repeatable check ran in completed(), after the response had already
been replaced by a discarding consumer, so the exchange finished without a
response and the future never completed. Do the check in handleResponse()
right after isRedirected(), ahead of the redirect limit and location checks,
like the classic RedirectExec. A POST with a non-repeatable entity now gets
the 301/302/303 response back instead of being followed as a GET.
@shinseongsu
shinseongsu force-pushed the async-redirect-non-repeatable branch from a9846b3 to 5cc6aed Compare November 19, 2026 00:19
@shinseongsu

Copy link
Copy Markdown
Contributor Author

@ok2c Squashed into a single commit.

@ok2c
ok2c merged commit 8cb46b6 into apache:master Nov 19, 2026
12 checks passed
@ok2c

ok2c commented Nov 19, 2026

Copy link
Copy Markdown
Member

Cherry-picked to 5.6.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants