shinseongsu opened a new pull request, #945:
URL: https://github.com/apache/httpcomponents-client/pull/945
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.
```java
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?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]