Skip to content

Commit f8f87ee

Browse files
committed
Don't reuse channel when request is chunked
1 parent 8be1fc1 commit f8f87ee

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

client/src/main/java/org/asynchttpclient/netty/handler/HttpProtocol.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private boolean exitAfterHandling401(//
179179
final Request request,//
180180
int statusCode,//
181181
Realm realm,//
182-
ProxyServer proxyServer) {
182+
ProxyServer proxyServer,//
183+
HttpRequest httpRequest) {
183184

184185
if (statusCode != UNAUTHORIZED.code())
185186
return false;
@@ -291,7 +292,7 @@ private boolean exitAfterHandling401(//
291292
final Request nextRequest = new RequestBuilder(future.getCurrentRequest()).setHeaders(requestHeaders).build();
292293

293294
logger.debug("Sending authentication to {}", request.getUri());
294-
if (future.isKeepAlive() && !HttpHeaders.isTransferEncodingChunked(response)) {
295+
if (future.isKeepAlive() && !HttpHeaders.isTransferEncodingChunked(httpRequest) && !HttpHeaders.isTransferEncodingChunked(response)) {
295296
future.setReuseChannel(true);
296297
requestSender.drainChannelAndExecuteNextRequest(channel, future, nextRequest);
297298
} else {
@@ -308,7 +309,8 @@ private boolean exitAfterHandling407(//
308309
HttpResponse response,//
309310
Request request,//
310311
int statusCode,//
311-
ProxyServer proxyServer) {
312+
ProxyServer proxyServer,//
313+
HttpRequest httpRequest) {
312314

313315
if (statusCode != PROXY_AUTHENTICATION_REQUIRED.code())
314316
return false;
@@ -425,7 +427,7 @@ private boolean exitAfterHandling407(//
425427
final Request nextRequest = nextRequestBuilder.build();
426428

427429
logger.debug("Sending proxy authentication to {}", request.getUri());
428-
if (future.isKeepAlive() && !HttpHeaders.isTransferEncodingChunked(response)) {
430+
if (future.isKeepAlive() && !HttpHeaders.isTransferEncodingChunked(httpRequest) && !HttpHeaders.isTransferEncodingChunked(response)) {
429431
future.setConnectAllowed(true);
430432
future.setReuseChannel(true);
431433
requestSender.drainChannelAndExecuteNextRequest(channel, future, nextRequest);
@@ -464,32 +466,32 @@ private boolean exitAfterHandlingConnect(//
464466
return false;
465467
}
466468

467-
private boolean exitAfterHandlingStatus(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseStatus status)
469+
private boolean exitAfterHandlingStatus(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseStatus status, HttpRequest httpRequest)
468470
throws IOException, Exception {
469471
if (!future.getAndSetStatusReceived(true) && handler.onStatusReceived(status) != State.CONTINUE) {
470-
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(response));
472+
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(httpRequest) || HttpHeaders.isTransferEncodingChunked(response));
471473
return true;
472474
}
473475
return false;
474476
}
475477

476-
private boolean exitAfterHandlingHeaders(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseHeaders responseHeaders)
478+
private boolean exitAfterHandlingHeaders(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, NettyResponseHeaders responseHeaders, HttpRequest httpRequest)
477479
throws IOException, Exception {
478480
if (!response.headers().isEmpty() && handler.onHeadersReceived(responseHeaders) != State.CONTINUE) {
479-
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(response));
481+
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(httpRequest) || HttpHeaders.isTransferEncodingChunked(response));
480482
return true;
481483
}
482484
return false;
483485
}
484486

485-
private boolean exitAfterHandlingReactiveStreams(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler) throws IOException {
487+
private boolean exitAfterHandlingReactiveStreams(Channel channel, NettyResponseFuture<?> future, HttpResponse response, AsyncHandler<?> handler, HttpRequest httpRequest) throws IOException {
486488
if (handler instanceof StreamedAsyncHandler) {
487489
StreamedAsyncHandler<?> streamedAsyncHandler = (StreamedAsyncHandler<?>) handler;
488490
StreamedResponsePublisher publisher = new StreamedResponsePublisher(channel.eventLoop(), channelManager, future, channel);
489491
channel.pipeline().addLast(channel.eventLoop(), "streamedAsyncHandler", publisher);
490492
Channels.setAttribute(channel, publisher);
491493
if (streamedAsyncHandler.onStream(publisher) != State.CONTINUE) {
492-
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(response));
494+
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(httpRequest) || HttpHeaders.isTransferEncodingChunked(response));
493495
return true;
494496
}
495497
}
@@ -515,13 +517,14 @@ private boolean handleHttpResponse(final HttpResponse response, final Channel ch
515517
NettyResponseHeaders responseHeaders = new NettyResponseHeaders(response.headers());
516518

517519
return exitAfterProcessingFilters(channel, future, handler, status, responseHeaders) || //
518-
exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer) || //
519-
exitAfterHandling407(channel, future, response, request, statusCode, proxyServer) || //
520+
exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer, httpRequest) || //
521+
exitAfterHandling407(channel, future, response, request, statusCode, proxyServer, httpRequest) || //
520522
exitAfterHandling100(channel, future, statusCode) || //
521523
exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm) || //
522524
exitAfterHandlingConnect(channel, future, request, proxyServer, statusCode, httpRequest) || //
523-
exitAfterHandlingStatus(channel, future, response, handler, status) || //
524-
exitAfterHandlingHeaders(channel, future, response, handler, responseHeaders) || exitAfterHandlingReactiveStreams(channel, future, response, handler);
525+
exitAfterHandlingStatus(channel, future, response, handler, status, httpRequest) || //
526+
exitAfterHandlingHeaders(channel, future, response, handler, responseHeaders, httpRequest) || //
527+
exitAfterHandlingReactiveStreams(channel, future, response, handler, httpRequest);
525528
}
526529

527530
private void handleChunk(HttpContent chunk,//

0 commit comments

Comments
 (0)