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 2020/11/22 13:02:54 UTC

[httpcomponents-core] branch 5.0.x updated: Bug fix: Fixed ignoring maxResultLength in toString method of EntityUtils.

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

olegk pushed a commit to branch 5.0.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/5.0.x by this push:
     new 455a9df  Bug fix: Fixed ignoring maxResultLength in toString method of EntityUtils.
455a9df is described below

commit 455a9df8b71dcd9b71880552907c4bfed6af3c17
Author: Klaw <Kl...@gmail.com>
AuthorDate: Sun Nov 22 13:43:29 2020 +0300

    Bug fix: Fixed ignoring maxResultLength in toString method of EntityUtils.
---
 .../apache/hc/core5/http/io/entity/EntityUtils.java   |  7 ++++---
 .../hc/core5/http/io/entity/TestEntityUtils.java      | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

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 0e6d70a..c2a3bd2 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
@@ -165,19 +165,20 @@ public final class EntityUtils {
         }
     }
 
-    private static CharArrayBuffer toCharArrayBuffer(final InputStream inStream, final long contentLength,
+    private static CharArrayBuffer toCharArrayBuffer(final InputStream inStream, final int contentLength,
             final Charset charset, final int maxResultLength) throws IOException {
         Args.notNull(inStream, "InputStream");
         Args.positive(maxResultLength, "maxResultLength");
         final Charset actualCharset = charset == null ? DEFAULT_CHARSET : charset;
         final CharArrayBuffer buf = new CharArrayBuffer(
-                Math.min(maxResultLength, contentLength > 0 ? (int) contentLength : DEFAULT_CHAR_BUFFER_SIZE));
+                Math.min(maxResultLength, contentLength > 0 ? contentLength : DEFAULT_CHAR_BUFFER_SIZE));
         final Reader reader = new InputStreamReader(inStream, actualCharset);
         final char[] tmp = new char[DEFAULT_CHAR_BUFFER_SIZE];
         int chReadCount;
-        while ((chReadCount = reader.read(tmp)) != -1) {
+        while ((chReadCount = reader.read(tmp)) != -1 && buf.length() < maxResultLength) {
             buf.append(tmp, 0, chReadCount);
         }
+        buf.setLength(Math.min(buf.length(), maxResultLength));
         return buf;
     }
 
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 d50131c..8d757fa 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
@@ -41,6 +41,7 @@ import java.util.Map;
 import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.ParseException;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.apache.hc.core5.net.URLEncodedUtils;
 import org.junit.Assert;
@@ -262,4 +263,22 @@ public class TestEntityUtils {
         }
     }
 
+    @Test
+    public void testStringMaxResultLength() throws IOException, ParseException {
+        final String allMessage = "Message content";
+        final byte[] allBytes = allMessage.getBytes(StandardCharsets.ISO_8859_1);
+        final Map<Integer, String> testCases = new HashMap<>();
+        testCases.put(7, allMessage.substring(0, 7));
+        testCases.put(allMessage.length(), allMessage);
+        testCases.put(Integer.MAX_VALUE, allMessage);
+
+        for (final Map.Entry<Integer, String> tc : testCases.entrySet()) {
+            final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null);
+            final String string = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1, tc.getKey());
+            final String expectedString = tc.getValue();
+            Assert.assertNotNull(string);
+            Assert.assertEquals(expectedString, string);
+        }
+    }
+
 }