Skip to content

Commit 7d853d5

Browse files
arturobernalgok2c
authored andcommitted
Fix Response Body Truncation Issue in ContentResponseHandler (#521)
Resolved a bug where the response body was truncated to 256 bytes in all cases, including successful responses.
1 parent 9e3d79b commit 7d853d5

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/ContentResponseHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ public Content handleEntity(final HttpEntity entity) throws IOException {
7272
public Content handleResponse(final ClassicHttpResponse response) throws IOException {
7373
final int statusCode = response.getCode();
7474
final HttpEntity entity = response.getEntity();
75-
final byte[] contentBytes = (entity != null) ? EntityUtils.toByteArray(entity, MAX_MESSAGE_LENGTH) : new byte[0];
7675
final ContentType contentType = (entity != null && entity.getContentType() != null) ? ContentType.parse(entity.getContentType()) : ContentType.DEFAULT_BINARY;
77-
final Content content = new Content(contentBytes, contentType);
7876
if (statusCode >= 300) {
79-
throw new HttpResponseException(statusCode, response.getReasonPhrase(), contentBytes, contentType);
77+
throw new HttpResponseException(statusCode, response.getReasonPhrase(), entity != null ? EntityUtils.toByteArray(entity, MAX_MESSAGE_LENGTH) : null, contentType);
8078
}
81-
return content;
79+
return new Content(entity != null ? EntityUtils.toByteArray(entity) : new byte[] {}, contentType);
8280
}
8381
}

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
import java.nio.charset.StandardCharsets;
3232

3333
import org.apache.hc.client5.http.ClientProtocolException;
34+
import org.apache.hc.client5.http.HttpResponseException;
3435
import org.apache.hc.client5.http.fluent.Content;
3536
import org.apache.hc.client5.http.fluent.Request;
3637
import org.apache.hc.client5.testing.sync.extension.TestClientResources;
3738
import org.apache.hc.core5.http.ContentType;
3839
import org.apache.hc.core5.http.HttpEntity;
3940
import org.apache.hc.core5.http.HttpHost;
41+
import org.apache.hc.core5.http.HttpStatus;
4042
import org.apache.hc.core5.http.URIScheme;
4143
import org.apache.hc.core5.http.io.entity.EntityUtils;
4244
import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -79,6 +81,20 @@ public void setUp() throws Exception {
7981
}
8082
response.setEntity(responseEntity);
8183
});
84+
85+
// Handler for large content large message
86+
server.registerHandler("/large-message", (request, response, context) -> {
87+
final String largeContent = generateLargeString(10000); // Large content string
88+
response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
89+
});
90+
91+
// Handler for large content large message with error
92+
server.registerHandler("/large-message-error", (request, response, context) -> {
93+
final String largeContent = generateLargeString(10000); // Large content string
94+
response.setCode(HttpStatus.SC_REDIRECTION);
95+
response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
96+
});
97+
8298
}
8399

84100
@Test
@@ -156,4 +172,38 @@ public void testConnectionRelease() throws Exception {
156172
}
157173
}
158174

175+
private String generateLargeString(final int size) {
176+
final StringBuilder sb = new StringBuilder(size);
177+
for (int i = 0; i < size; i++) {
178+
sb.append("x");
179+
}
180+
return sb.toString();
181+
}
182+
183+
@Test
184+
public void testLargeResponse() throws Exception {
185+
186+
final HttpHost target = targetHost();
187+
final String baseURL = "http://localhost:" + target.getPort();
188+
189+
final Content content = Request.get(baseURL + "/large-message").execute().returnContent();
190+
Assertions.assertEquals(10000, content.asBytes().length);
191+
}
192+
193+
@Test
194+
public void testLargeResponseError() throws Exception {
195+
final HttpHost target = targetHost();
196+
final String baseURL = "http://localhost:" + target.getPort();
197+
198+
try {
199+
Request.get(baseURL + "/large-message-error").execute().returnContent();
200+
Assertions.fail("Expected an HttpResponseException to be thrown");
201+
} catch (final HttpResponseException e) {
202+
// Check if the content of the exception is less than or equal to 256 bytes
203+
final byte[] contentBytes = e.getContentBytes();
204+
Assertions.assertNotNull(contentBytes, "Content bytes should not be null");
205+
Assertions.assertTrue(contentBytes.length <= 256, "Content length should be less or equal to 256 bytes");
206+
}
207+
}
208+
159209
}

0 commit comments

Comments
 (0)