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 2026/07/23 09:14:02 UTC
(httpcomponents-core) branch master updated: Deprecate EntityUtils#toByteArray / EntityUtils#toString methods with no maxResultLength constraint
This is an automated email from the ASF dual-hosted git repository.
ok2c 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 8f0308f17 Deprecate EntityUtils#toByteArray / EntityUtils#toString methods with no maxResultLength constraint
8f0308f17 is described below
commit 8f0308f1736e38928e7fdaa7aa0d73eb04e240e8
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Jul 21 13:41:07 2026 +0200
Deprecate EntityUtils#toByteArray / EntityUtils#toString methods with no maxResultLength constraint
---
.../framework/ClassicTestClientAdapter.java | 2 +-
.../framework/TestingFrameworkRequestHandler.java | 2 +-
.../testing/classic/ClassicAuthenticationTest.java | 14 ++++----
.../classic/ClassicHttpCoreTransportTest.java | 15 ++++----
.../testing/classic/ClassicIntegrationTest.java | 42 +++++++++++-----------
.../testing/classic/ClassicTLSIntegrationTest.java | 2 +-
.../hc/core5/testing/classic/EchoHandler.java | 2 +-
.../classic/ClassicHttpCompatTest.java | 4 +--
.../framework/TestClassicTestClientAdapter.java | 2 +-
.../TestTestingFrameworkRequestHandler.java | 2 +-
.../nio/ClassicToAsyncHttp1TransportTest.java | 2 +-
.../hc/core5/http/io/entity/EntityUtils.java | 16 ++++++++-
.../http/examples/ClassicFileServerExample.java | 4 +--
.../http/examples/ClassicGetExecutionExample.java | 2 +-
.../http/examples/ClassicPostExecutionExample.java | 2 +-
.../hc/core5/http/io/entity/TestEntityUtils.java | 22 ++++++------
.../hc/core5/http/io/entity/TestStringEntity.java | 4 +--
.../http/io/support/ClassicRequestBuilderTest.java | 2 +-
18 files changed, 78 insertions(+), 63 deletions(-)
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
index 9c83e8010..8695f3e62 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
@@ -143,7 +143,7 @@ public Map<String, Object> execute(final String defaultURI, final Map<String, Ob
try (final ClassicHttpResponse response = client.execute(host, httpRequest, context)) {
// Prepare the response. It will contain status, body, headers, and contentType.
final HttpEntity entity = response.getEntity();
- final String body = entity == null ? null : EntityUtils.toString(entity);
+ final String body = entity == null ? null : EntityUtils.toString(entity, Integer.MAX_VALUE);
final String contentType = entity == null ? null : entity.getContentType();
// prepare the returned information
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
index 3ad934b09..9a811e348 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFrameworkRequestHandler.java
@@ -188,7 +188,7 @@ public void handle(final ClassicHttpRequest request, final ClassicHttpResponse r
final String expectedBody = (String) requestExpectations.get(BODY);
if (expectedBody != null) {
final HttpEntity entity = request.getEntity();
- final String data = EntityUtils.toString(entity);
+ final String data = EntityUtils.toString(entity, 1024);
if (! data.equals(expectedBody)) {
throw new TestingFrameworkException("Expected body not found. " +
" Body=" + data + "; expected=" + expectedBody);
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
index 0c9086f49..93a962e49 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
@@ -28,7 +28,6 @@
package org.apache.hc.core5.testing.classic;
-import org.junit.jupiter.api.Assertions;
import java.nio.charset.StandardCharsets;
import java.util.Random;
@@ -61,6 +60,7 @@
import org.apache.hc.core5.testing.extension.classic.HttpRequesterResource;
import org.apache.hc.core5.testing.extension.classic.HttpServerResource;
import org.apache.hc.core5.util.Timeout;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -135,14 +135,14 @@ void testGetRequestAuthentication() throws Exception {
final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.GET, "/stuff");
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("You shall not pass!!!", body1);
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.GET, "/stuff");
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response2.getCode());
- final String body1 = EntityUtils.toString(response2.getEntity());
+ final String body1 = EntityUtils.toString(response2.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("", body1);
}
}
@@ -163,7 +163,7 @@ void testPostRequestAuthentication() throws Exception {
request1.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("You shall not pass!!!", body1);
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
@@ -171,7 +171,7 @@ void testPostRequestAuthentication() throws Exception {
request2.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response2.getCode());
- final String body1 = EntityUtils.toString(response2.getEntity());
+ final String body1 = EntityUtils.toString(response2.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(new String(stuff, StandardCharsets.US_ASCII), body1);
}
}
@@ -193,7 +193,7 @@ void testPostRequestAuthenticationNoExpectContinue() throws Exception {
request1.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("You shall not pass!!!", body1);
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
@@ -202,7 +202,7 @@ void testPostRequestAuthenticationNoExpectContinue() throws Exception {
request2.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response2.getCode());
- final String body1 = EntityUtils.toString(response2.getEntity());
+ final String body1 = EntityUtils.toString(response2.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(new String(stuff, StandardCharsets.US_ASCII), body1);
}
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicHttpCoreTransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicHttpCoreTransportTest.java
index 5327f9bd3..9d0681183 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicHttpCoreTransportTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicHttpCoreTransportTest.java
@@ -78,21 +78,21 @@ void testSequentialRequests() throws Exception {
request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some stuff", body1);
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/other-stuff");
request2.setEntity(new StringEntity("some other stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response2.getCode());
- final String body2 = EntityUtils.toString(response2.getEntity());
+ final String body2 = EntityUtils.toString(response2.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some other stuff", body2);
}
final ClassicHttpRequest request3 = new BasicClassicHttpRequest(Method.POST, "/more-stuff");
request3.setEntity(new StringEntity("some more stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response3 = requester.execute(target, request3, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response3.getCode());
- final String body3 = EntityUtils.toString(response3.getEntity());
+ final String body3 = EntityUtils.toString(response3.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some more stuff", body3);
}
}
@@ -108,21 +108,21 @@ void testSequentialRequestsNonPersistentConnection() throws Exception {
request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some stuff", body1);
}
final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/other-stuff");
request2.setEntity(new StringEntity("some other stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response2.getCode());
- final String body2 = EntityUtils.toString(response2.getEntity());
+ final String body2 = EntityUtils.toString(response2.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some other stuff", body2);
}
final ClassicHttpRequest request3 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/more-stuff");
request3.setEntity(new StringEntity("some more stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response3 = requester.execute(target, request3, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response3.getCode());
- final String body3 = EntityUtils.toString(response3.getEntity());
+ final String body3 = EntityUtils.toString(response3.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some more stuff", body3);
}
}
@@ -149,7 +149,8 @@ void testMultiThreadedRequests() throws Exception {
request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
requester.execute(target, request1, TIMEOUT, context, response -> {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- Assertions.assertEquals("some stuff", EntityUtils.toString(response.getEntity()));
+ Assertions.assertEquals("some stuff",
+ EntityUtils.toString(response.getEntity(), 1024));
return null;
});
} catch (final Exception ex) {
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
index aecc6b83f..b9c2f7f5b 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicIntegrationTest.java
@@ -43,6 +43,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
+import javax.net.ssl.SSLSocket;
+
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
@@ -87,8 +89,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
-import javax.net.ssl.SSLSocket;
-
abstract class ClassicIntegrationTest {
private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
@@ -146,7 +146,7 @@ void testSimpleBasicHttpRequests() throws Exception {
for (int r = 0; r < reqNo; r++) {
final BasicClassicHttpRequest get = new BasicClassicHttpRequest(Method.GET, "/?" + r);
try (final ClassicHttpResponse response = client.execute(host, get, context)) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
@@ -184,7 +184,7 @@ void testSimpleHttpPostsWithContentLength() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null));
}
});
@@ -201,7 +201,7 @@ void testSimpleHttpPostsWithContentLength() throws Exception {
post.setEntity(new ByteArrayEntity(data, null));
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
@@ -239,7 +239,7 @@ void testSimpleHttpPostsChunked() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null, true));
}
});
@@ -256,7 +256,7 @@ void testSimpleHttpPostsChunked() throws Exception {
post.setEntity(new ByteArrayEntity(data, null, true));
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
@@ -293,7 +293,7 @@ void testSimpleHttpPostsHTTP10() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null));
}
});
@@ -312,7 +312,7 @@ void testSimpleHttpPostsHTTP10() throws Exception {
post.setEntity(new ByteArrayEntity(data, null));
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
@@ -376,7 +376,7 @@ void testHttpPostsWithExpectContinue() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null, true));
}
});
@@ -393,7 +393,7 @@ void testHttpPostsWithExpectContinue() throws Exception {
post.setEntity(new ByteArrayEntity(data, null, true));
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
final byte[] expected = testData.get(r);
Assertions.assertEquals(expected.length, received.length);
@@ -565,7 +565,7 @@ void testHttpContent() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final String line = EntityUtils.toString(entity);
+ final String line = EntityUtils.toString(entity, Integer.MAX_VALUE);
final ContentType contentType = ContentType.parse(entity.getContentType());
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.UTF_8);
response.setEntity(new RepeatingEntity(line, charset, n, n % 2 == 0));
@@ -614,7 +614,7 @@ void testHttpPostNoEntity() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null));
}
});
@@ -630,7 +630,7 @@ void testHttpPostNoEntity() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(0, received.length);
}
}
@@ -644,7 +644,7 @@ void testHttpPostNoContentLength() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null));
}
});
@@ -666,7 +666,7 @@ void testHttpPostNoContentLength() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(0, received.length);
}
}
@@ -680,7 +680,7 @@ void testHttpPostIdentity() throws Exception {
final HttpEntity entity = request.getEntity();
if (entity != null) {
- final byte[] data = EntityUtils.toByteArray(entity);
+ final byte[] data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, null));
}
});
@@ -748,7 +748,7 @@ void testEchoNoEntityRequest() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
if (response.getEntity() != null) {
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(0, received.length);
}
}
@@ -772,7 +772,7 @@ void testEchoEmptyEntity() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(0, received.length);
}
}
@@ -800,7 +800,7 @@ void testEchoLargeEntity() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertArrayEquals(data, received);
}
}
@@ -825,7 +825,7 @@ void testEchoManySmallRequests() throws Exception {
try (final ClassicHttpResponse response = client.execute(host, post, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final byte[] received = EntityUtils.toByteArray(response.getEntity());
+ final byte[] received = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertArrayEquals(data, received);
}
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
index c63145a30..393ddd8c1 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
@@ -137,7 +137,7 @@ void testTLSSuccess() throws Exception {
request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
Assertions.assertEquals(HttpStatus.SC_OK, response1.getCode());
- final String body1 = EntityUtils.toString(response1.getEntity());
+ final String body1 = EntityUtils.toString(response1.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals("some stuff", body1);
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/EchoHandler.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/EchoHandler.java
index 85b49a1d6..efffc3b3d 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/EchoHandler.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/EchoHandler.java
@@ -52,7 +52,7 @@ public void handle(
final HttpEntity entity = request.getEntity();
final byte[] data;
if (entity != null) {
- data = EntityUtils.toByteArray(entity);
+ data = EntityUtils.toByteArray(entity, Integer.MAX_VALUE);
response.setEntity(new ByteArrayEntity(data, ContentType.parse(entity.getContentType())));
}
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/classic/ClassicHttpCompatTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/classic/ClassicHttpCompatTest.java
index 31d49dd1f..afa4b1d3a 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/classic/ClassicHttpCompatTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/compatibility/classic/ClassicHttpCompatTest.java
@@ -93,7 +93,7 @@ void test_sequential_requests() throws Exception {
.build();
requester.execute(target, request, TIMEOUT, context, response -> {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
- final String body1 = EntityUtils.toString(response.getEntity());
+ final String body1 = EntityUtils.toString(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertEquals(ContainerImages.AAA, body1);
return null;
});
@@ -123,7 +123,7 @@ void test_multi_threaded_requests() throws Exception {
resultQueue.add(new Result<>(
request,
response,
- EntityUtils.toString(response.getEntity())));
+ EntityUtils.toString(response.getEntity(), Integer.MAX_VALUE)));
return null;
});
} catch (final Exception ex) {
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java
index c895f08c4..a3910fe67 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestClassicTestClientAdapter.java
@@ -94,7 +94,7 @@ void executeBuildsRequestAndParsesResponse() throws Exception {
: null);
if (request.getEntity() != null) {
captured.put("content-type", request.getEntity().getContentType());
- captured.put("body", EntityUtils.toString(request.getEntity()));
+ captured.put("body", EntityUtils.toString(request.getEntity(), Integer.MAX_VALUE));
} else {
captured.put("content-type", null);
captured.put("body", null);
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java
index 39f5a60a8..f50a34ef7 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/framework/TestTestingFrameworkRequestHandler.java
@@ -126,7 +126,7 @@ void handleValidatesRequestAndBuildsResponse() throws Exception {
handler.assertNothingThrown();
Assertions.assertEquals(201, response.getCode());
- Assertions.assertEquals("pong", EntityUtils.toString(response.getEntity()));
+ Assertions.assertEquals("pong", EntityUtils.toString(response.getEntity(), 1024));
Assertions.assertTrue(response.getEntity().getContentType().contains("text/plain"));
Assertions.assertEquals("ok", response.getFirstHeader("X-Reply").getValue());
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java
index 5e312e44c..16b78b54c 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/ClassicToAsyncHttp1TransportTest.java
@@ -92,7 +92,7 @@ void test_request_handling_no_keep_alive(final int contentSize) throws Exception
try (ClassicHttpResponse response = responseConsumer.blockWaiting()) {
Assertions.assertEquals(200, response.getCode());
- final byte[] bytes = EntityUtils.toByteArray(response.getEntity());
+ final byte[] bytes = EntityUtils.toByteArray(response.getEntity(), Integer.MAX_VALUE);
Assertions.assertNotNull(bytes);
Assertions.assertArrayEquals(temp, bytes);
}
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 e4c3c000f..019acc4bd 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
@@ -58,7 +58,6 @@
*/
public final class EntityUtils {
- // TODO Consider using a sane value, but what is sane? 1 GB? 100 MB? 10 MB?
private static final int DEFAULT_ENTITY_RETURN_MAX_LENGTH = Integer.MAX_VALUE;
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private static final int DEFAULT_CHAR_BUFFER_SIZE = 1024;
@@ -128,7 +127,10 @@ static long checkContentLength(final EntityDetails entityDetails) {
* {@link HttpEntity#getContent()} is null.
* @throws IOException if an error occurs reading the input stream
* @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
+ *
+ * @deprecated Use {@link #toByteArray(HttpEntity, int)}
*/
+ @Deprecated
public static byte[] toByteArray(final HttpEntity entity) throws IOException {
Args.notNull(entity, "HttpEntity");
final int contentLength = toContentLength((int) checkContentLength(entity));
@@ -247,7 +249,10 @@ private static String toString(final HttpEntity entity, final ContentType conten
* @throws IOException if an error occurs reading the input stream
* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
* this instance of the Java virtual machine and no defaultCharset is provided.
+ *
+ * @deprecated Use {@link #toString(HttpEntity, Charset, int)}.
*/
+ @Deprecated
public static String toString(
final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
return toString(entity, defaultCharset, DEFAULT_ENTITY_RETURN_MAX_LENGTH);
@@ -328,7 +333,10 @@ public static String toString(
* @throws IOException if an error occurs reading the input stream
* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in
* this instance of the Java virtual machine
+ *
+ * @deprecated Use {@link #toString(HttpEntity, Charset, int)}.
*/
+ @Deprecated
public static String toString(
final HttpEntity entity, final String defaultCharset, final int maxResultLength) throws IOException, ParseException {
return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null, maxResultLength);
@@ -346,7 +354,10 @@ public static String toString(
* @throws IOException if an error occurs reading the input stream
* @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in
* this instance of the Java virtual machine
+ *
+ * @deprecated Use {@link #toString(HttpEntity, Charset, int)}.
*/
+ @Deprecated
public static String toString(final HttpEntity entity) throws IOException, ParseException {
return toString(entity, DEFAULT_ENTITY_RETURN_MAX_LENGTH);
}
@@ -383,7 +394,10 @@ public static String toString(final HttpEntity entity, final int maxResultLength
* @return a list of {@link NameValuePair} as built from the URI's query portion.
* @throws IOException
* If there was an exception getting the entity's data.
+ *
+ * @deprecated Use {@link #parse(HttpEntity, int)}
*/
+ @Deprecated
public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
return parse(entity, DEFAULT_ENTITY_RETURN_MAX_LENGTH);
}
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicFileServerExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
index 9b4cea8ec..31c90e455 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
@@ -155,8 +155,8 @@ public void handle(
final HttpEntity incomingEntity = request.getEntity();
if (incomingEntity != null) {
- final byte[] entityContent = EntityUtils.toByteArray(incomingEntity);
- System.out.println("Incoming incomingEntity content (bytes): " + entityContent.length);
+ EntityUtils.consume(incomingEntity);
+ System.out.println("Incoming incomingEntity content (bytes): " + incomingEntity.getContentLength());
}
final File file = new File(this.docRoot, URLDecoder.decode(path, "UTF-8"));
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
index ab7de44b1..eb13fd79a 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
@@ -93,7 +93,7 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke
.build();
try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), context)) {
System.out.println(requestUri + "->" + response.getCode());
- System.out.println(EntityUtils.toString(response.getEntity()));
+ System.out.println(EntityUtils.toString(response.getEntity(), 1024));
System.out.println("==============");
}
}
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
index 49644d104..81089cec4 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
@@ -113,7 +113,7 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke
request.setEntity(requestBodies[i]);
try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), context)) {
System.out.println(requestUri + "->" + response.getCode());
- System.out.println(EntityUtils.toString(response.getEntity()));
+ System.out.println(EntityUtils.toString(response.getEntity(), 1024));
System.out.println("==============");
}
}
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 3e566895b..2c26d2b14 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
@@ -56,7 +56,7 @@ class TestEntityUtils {
@Test
void testNullEntityToByteArray() {
Assertions.assertThrows(NullPointerException.class, () ->
- EntityUtils.toByteArray(null));
+ EntityUtils.toByteArray(null, 1024));
}
@Test
@@ -65,14 +65,14 @@ void testMaxIntContentToByteArray() {
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content),
Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.US_ASCII));
Assertions.assertThrows(IllegalArgumentException.class, () ->
- EntityUtils.toByteArray(entity));
+ EntityUtils.toByteArray(entity, 1024));
}
@Test
void testUnknownLengthContentToByteArray() throws Exception {
final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null);
- final byte[] bytes2 = EntityUtils.toByteArray(entity);
+ final byte[] bytes2 = EntityUtils.toByteArray(entity, 1024);
Assertions.assertNotNull(bytes2);
Assertions.assertEquals(bytes.length, bytes2.length);
for (int i = 0; i < bytes.length; i++) {
@@ -84,7 +84,7 @@ void testUnknownLengthContentToByteArray() throws Exception {
void testKnownLengthContentToByteArray() throws Exception {
final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null);
- final byte[] bytes2 = EntityUtils.toByteArray(entity);
+ final byte[] bytes2 = EntityUtils.toByteArray(entity, 1024);
Assertions.assertNotNull(bytes2);
Assertions.assertEquals(bytes.length, bytes2.length);
for (int i = 0; i < bytes.length; i++) {
@@ -94,7 +94,7 @@ void testKnownLengthContentToByteArray() throws Exception {
@Test
void testNullEntityToString() {
- Assertions.assertThrows(NullPointerException.class, () -> EntityUtils.toString(null));
+ Assertions.assertThrows(NullPointerException.class, () -> EntityUtils.toString(null, 1024));
}
@Test
@@ -119,7 +119,7 @@ void testKnownLengthContentToString() throws Exception {
final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length,
ContentType.TEXT_PLAIN.withCharset(StandardCharsets.US_ASCII));
- final String s = EntityUtils.toString(entity, StandardCharsets.US_ASCII);
+ final String s = EntityUtils.toString(entity, StandardCharsets.US_ASCII, 1024);
Assertions.assertEquals("Message content", s);
}
@@ -147,7 +147,7 @@ void testNoCharsetContentToString() {
final String content = constructString(SWISS_GERMAN_HELLO);
final byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN);
- Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity));
+ Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entity, 1024));
}
@Test
@@ -156,7 +156,7 @@ void testDefaultCharsetContentToString() throws Exception {
final byte[] bytes = content.getBytes(Charset.forName("KOI8-R"));
final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes),
ContentType.parse("text/plain"));
- final String s = EntityUtils.toString(entity, Charset.forName("KOI8-R"));
+ final String s = EntityUtils.toString(entity, Charset.forName("KOI8-R"), 1024);
Assertions.assertEquals(content, s);
}
@@ -211,12 +211,12 @@ private static void assertNameValuePair (
@Test
void testParseEntity() throws Exception {
final StringEntity entity1 = new StringEntity("Name1=Value1", ContentType.APPLICATION_FORM_URLENCODED);
- final List<NameValuePair> result = EntityUtils.parse(entity1);
+ final List<NameValuePair> result = EntityUtils.parse(entity1, 1024);
Assertions.assertEquals(1, result.size());
assertNameValuePair(result.get(0), "Name1", "Value1");
final StringEntity entity2 = new StringEntity("Name1=Value1", ContentType.parse("text/test"));
- Assertions.assertTrue(EntityUtils.parse(entity2).isEmpty());
+ Assertions.assertTrue(EntityUtils.parse(entity2, 1024).isEmpty());
}
@Test
@@ -233,7 +233,7 @@ void testParseUTF8Entity() throws Exception {
"&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
final StringEntity entity = new StringEntity(s,
ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
- final List<NameValuePair> result = EntityUtils.parse(entity);
+ final List<NameValuePair> result = EntityUtils.parse(entity, 1024);
Assertions.assertEquals(2, result.size());
assertNameValuePair(result.get(0), "russian", ru_hello);
assertNameValuePair(result.get(1), "swiss", ch_hello);
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestStringEntity.java b/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestStringEntity.java
index f6e370a91..665945ebc 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestStringEntity.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/io/entity/TestStringEntity.java
@@ -89,11 +89,11 @@ void testNullCharset() throws Exception {
StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
Assertions.assertNotNull(httpentity.getContentType());
Assertions.assertEquals("text/plain", httpentity.getContentType());
- Assertions.assertEquals(s, EntityUtils.toString(httpentity));
+ Assertions.assertEquals(s, EntityUtils.toString(httpentity, 1024));
httpentity = new StringEntity(s, (Charset) null);
Assertions.assertNotNull(httpentity.getContentType());
Assertions.assertEquals("text/plain", httpentity.getContentType());
- Assertions.assertEquals(s, EntityUtils.toString(httpentity));
+ Assertions.assertEquals(s, EntityUtils.toString(httpentity, 1024));
}
@Test
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/io/support/ClassicRequestBuilderTest.java b/httpcore5/src/test/java/org/apache/hc/core5/http/io/support/ClassicRequestBuilderTest.java
index c5331f084..3fc2dd3a5 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/io/support/ClassicRequestBuilderTest.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/io/support/ClassicRequestBuilderTest.java
@@ -266,7 +266,7 @@ void builderQuery() throws IOException {
() -> assertEquals("/theUri", classicHttpRequest.getPath()),
() -> assertNotNull(classicHttpRequest.getEntity()),
() -> assertEquals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType(), ContentType.parse(classicHttpRequest.getEntity().getContentType()).getMimeType()),
- () -> assertEquals("param1=value1¶m2=value2", EntityUtils.toString(classicHttpRequest.getEntity()))
+ () -> assertEquals("param1=value1¶m2=value2", EntityUtils.toString(classicHttpRequest.getEntity(), 1024))
);
}