Skip to content

fix(Net): Detect incomplete chunked transfer encoding#5112

Merged
matejk merged 1 commit intomainfrom
5032-incomplete-chunked-transfer
Dec 18, 2025
Merged

fix(Net): Detect incomplete chunked transfer encoding#5112
matejk merged 1 commit intomainfrom
5032-incomplete-chunked-transfer

Conversation

@matejk
Copy link
Copy Markdown
Contributor

@matejk matejk commented Dec 17, 2025

Summary

When receiving data with chunked transfer encoding, if the connection is closed by the peer without sending the terminating 0\r\n\r\n, or if a chunk is not sent completely, HTTPChunkedInputStream now sets a MessageException that can be retrieved via session.networkException().

Problem

Previously, when a server closed the connection gracefully (FIN) before completing the chunked response:

  • The stream would return EOF as if the data was delivered correctly
  • session.networkException() returned nullptr
  • There was no way to detect that the data was incomplete

This only occurred with graceful close (FIN). Connection reset (RST) already raised an exception properly.

Solution

Modified HTTPChunkedStreamBuf::readFromDevice() to detect premature connection closure in two scenarios:

  1. Incomplete chunk header: When chunk size parsing fails due to unexpected EOF
  2. Incomplete chunk data: When read() returns 0 or less while expecting more data (_chunk > 0)

In both cases, a MessageException("Incomplete chunked transfer encoding") is now set via _session.setException().

Testing

The fix can be tested using the reproduction steps from issue #5032. After the fix, session.networkException() will return the MessageException when the connection is closed before receiving the complete chunked response.

Fixes #5032

@matejk matejk merged commit 752343a into main Dec 18, 2025
86 checks passed
@matejk matejk deleted the 5032-incomplete-chunked-transfer branch December 18, 2025 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTPChunkedInputStream does not raise an error when the stream ends prematurely

1 participant