You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ab...@apache.org on 2026/08/22 06:45:51 UTC

(httpcomponents-client) 01/02: Fix Brotli decompression hang with large input buffers

This is an automated email from the ASF dual-hosted git repository.

arturobernalg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit a01e51d56e913f5308248269824d1eeba61958d7
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Fri Aug 21 08:40:20 2026 +0200

    Fix Brotli decompression hang with large input buffers
---
 .../async/methods/InflatingBrotliDataConsumer.java |  7 +--
 .../methods/InflatingBrotliDataConsumerTest.java   | 53 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java
index 8da481566..71ca9bc11 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java
@@ -87,12 +87,9 @@ public void updateCapacity(final CapacityChannel capacityChannel) throws IOExcep
     public void consume(final ByteBuffer src) throws IOException {
         while (src.hasRemaining()) {
             final ByteBuffer in = decoder.getInputBuffer();
+            in.clear();
+
             final int xfer = Math.min(src.remaining(), in.remaining());
-            if (xfer == 0) {
-                decoder.push(0);
-                pump();
-                continue;
-            }
             final int lim = src.limit();
             src.limit(src.position() + xfer);
             in.put(src);
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumerTest.java b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumerTest.java
index 38afbf529..a60815554 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumerTest.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumerTest.java
@@ -26,19 +26,24 @@
  */
 package org.apache.hc.client5.http.async.methods;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Random;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -246,4 +251,52 @@ void registerInExec() {
         final ContentCompressionAsyncExec exec = new ContentCompressionAsyncExec(map);
         assertNotNull(exec);
     }
+
+    @Test
+    void inflateBrotliLargerThanInputBuffer() {
+        assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
+            final byte[] original = new byte[64 * 1024];
+            new Random(42).nextBytes(original);
+
+            final byte[] compressed = Encoder.compress(
+                    original,
+                    new Encoder.Parameters()
+                            .setQuality(6)
+                            .setWindow(22));
+
+            assertTrue(compressed.length > 8 * 1024);
+
+            final ByteArrayOutputStream output = new ByteArrayOutputStream();
+
+            final AsyncDataConsumer downstream = new AsyncDataConsumer() {
+
+                @Override
+                public void updateCapacity(final CapacityChannel capacityChannel) {
+                }
+
+                @Override
+                public void consume(final ByteBuffer src) {
+                    final byte[] buf = new byte[src.remaining()];
+                    src.get(buf);
+                    output.write(buf, 0, buf.length);
+                }
+
+                @Override
+                public void streamEnd(final List<? extends Header> trailers) {
+                }
+
+                @Override
+                public void releaseResources() {
+                }
+            };
+
+            final InflatingBrotliDataConsumer inflating =
+                    new InflatingBrotliDataConsumer(downstream);
+
+            inflating.consume(ByteBuffer.wrap(compressed));
+            inflating.streamEnd(Collections.emptyList());
+
+            assertArrayEquals(original, output.toByteArray());
+        });
+    }
 }