You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2022/11/02 09:16:38 UTC
[httpcomponents-core] branch master updated: Make it possible to read chunked entities with maxResultLength (#373)
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new c2579d3f1 Make it possible to read chunked entities with maxResultLength (#373)
c2579d3f1 is described below
commit c2579d3f151e5ac660785301701d7e0dbcfab398
Author: Anders Jaensson <an...@smartbear.com>
AuthorDate: Wed Nov 2 10:16:32 2022 +0100
Make it possible to read chunked entities with maxResultLength (#373)
---
.../hc/core5/http/io/entity/EntityUtils.java | 3 ++-
.../hc/core5/http/io/entity/TestEntityUtils.java | 24 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
index 37c6f0d3f..ebc0731cb 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
@@ -158,9 +158,10 @@ public final class EntityUtils {
final ByteArrayBuffer buffer = new ByteArrayBuffer(Math.min(maxResultLength, contentLength));
final byte[] tmp = new byte[DEFAULT_BYTE_BUFFER_SIZE];
int l;
- while ((l = inStream.read(tmp, 0, Math.min(DEFAULT_BYTE_BUFFER_SIZE, buffer.capacity() - buffer.length()))) > 0) {
+ while ((l = inStream.read(tmp)) != -1 && buffer.length() < maxResultLength) {
buffer.append(tmp, 0, l);
}
+ buffer.setLength(Math.min(buffer.length(), maxResultLength));
return buffer.toByteArray();
}
}
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java b/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java
index 111389b79..fe60422b5 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestEntityUtils.java
@@ -261,6 +261,30 @@ public class TestEntityUtils {
}
}
+ @Test
+ public void testByteArrayMaxResultLengthWithNoContentLength() throws IOException {
+ final byte b = 'b';
+ final byte[] allBytes = new byte[5000];
+ Arrays.fill(allBytes, b);
+ final Map<Integer, byte[]> testCases = new HashMap<>();
+ testCases.put(0, new byte[]{});
+ testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2));
+ testCases.put(allBytes.length, allBytes);
+ testCases.put(Integer.MAX_VALUE, allBytes);
+
+ for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) {
+ final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), null);
+
+ final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey());
+ final byte[] expectedBytes = tc.getValue();
+ Assertions.assertNotNull(bytes);
+ Assertions.assertEquals(expectedBytes.length, bytes.length);
+ for (int i = 0; i < expectedBytes.length; i++) {
+ Assertions.assertEquals(expectedBytes[i], bytes[i]);
+ }
+ }
+ }
+
@Test
public void testStringMaxResultLength() throws IOException, ParseException {
final String allMessage = "Message content";