Repository navigation
Description
In Apache HttpCore 5.5, a caller copying a response body with response.getEntity().writeTo(output) can receive a normal return even when the copy failed. If some bytes have already been written, the caller has no indication from writeTo that the output is incomplete.
This report is based on the 5.5 source. HttpRequester.execute wraps the response entity and delegates the copy to the wrapped entity. If the copy throws IOException, the wrapper catches it, calls abortConnection() to discard the connection, and reaches the end of writeTo without propagating the exception.
HttpEntity.writeTo documents IOException for an I/O error. The wrapper should discard the failed connection and propagate the original exception to the caller. A regression test could use an output stream that throws a known IOException and check that the caller receives that exception and that the connection is discarded.
Response wrapper in HttpCore 5.5
Lines 397 to 407 in 4edc2fb
| @Override | |
| public void writeTo(final OutputStream outStream) throws IOException { | |
| try { | |
| if (outStream != null) { | |
| super.writeTo(outStream); | |
| } | |
| close(); | |
| } catch (final IOException | RuntimeException ex) { | |
| abortConnection(); | |
| } | |
| } |
HttpEntity.writeTo contract in the same release
httpcomponents-core/httpcore5/src/main/java/org/apache/hc/core5/http/HttpEntity.java
Lines 115 to 123 in 4edc2fb
| * all allocated resources are properly deallocated when this method | |
| * returns. | |
| * </p> | |
| * | |
| * @param outStream the output stream to write entity content to | |
| * | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| void writeTo(OutputStream outStream) throws IOException; |
This report was drafted with AI assistance.
Activity
ok2c commentedon Dec 2, 2026
@AlwaysPS Please see the proposed fix #1551
AlwaysPS commentedon Dec 3, 2026
Thanks, @ok2c. I verified #1551 at f512349 with a local HTTP server and an output stream that fails after writing three bytes. On 5.5 and on the parent commit, the response entity’s writeTo returned normally with only those three bytes copied. With the patch, the caller received the original IOException. I also checked the runtime exception case and got the original exception there as well.
In both failure cases the connection was discarded, and the next request succeeded. Normal writes still copied the full response body. This fixes the behavior I reported in #1549.
Issue #1549: HttpRequester: #execute fails to propagate exceptions th…