Skip to content

Commit

Permalink
🥅 Avoid closing request channel in AuthenticationCallHandler when Tra…
Browse files Browse the repository at this point in the history
…nsfer-Encoding=chunked
  • Loading branch information
ujibang committed Apr 4, 2023
1 parent e9363f6 commit fa9c3ab
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -122,6 +123,8 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
}
}

private static final HttpString TRANSFER_ENCODING = HttpString.tryFromString("Transfer-Encoding");

/**
* shutdowns the request channel and ends the exchange
*
Expand All @@ -133,8 +136,19 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
*/
private void fastEndExchange(HttpServerExchange exchange) throws IOException {
var requestChannel = exchange.getRequestChannel();
if (requestChannel != null) {
requestChannel.shutdownReads();
var econding = exchange.getRequestHeaders().get(TRANSFER_ENCODING);

// shutdown channel only if Transfer-Encoding is not chunked
// otherwise we get error
// java.io.IOException: UT000029: Channel was closed mid chunk, if you have attempted to write chunked data you cannot shutdown the channel until after it has all been written
if (econding == null || !econding.getFirst().startsWith("chunked")) {
if (requestChannel != null) {
try {
requestChannel.shutdownReads();
} catch(IOException ie) {
LOGGER.debug("ingoring error shutting down reads", ie);
}
}
}

exchange.endExchange();
Expand Down

0 comments on commit fa9c3ab

Please sign in to comment.