Skip to content

fix(iocp): delimit empty streams and drop Content-Length on 204/304 - #639

Open
edsontmarques wants to merge 2 commits into
HashLoad:masterfrom
edsontmarques:fix/iocp-content-length
Open

edsontmarques wants to merge 2 commits into
HashLoad:masterfrom
edsontmarques:fix/iocp-content-length

Conversation

@edsontmarques

Copy link
Copy Markdown

Stacked on #638 (tests/iocp-http and the send changes come from there). Until #638 is merged, this PR's diff also shows its commit. The change here is the last commit.

Problem

TIocpRawResponse set Content-Length wrongly in three cases:

  • Empty stream: SendStreamResponse emitted Content-Length only when the stream had bytes. On a keep-alive connection, an empty stream (for example a Send(TStream) of an empty list) went out with neither Content-Length nor Transfer-Encoding, so the client could not tell where the response ended and waited.
  • Handler's Transfer-Encoding: a non-empty stream got a Content-Length even when the handler had set Transfer-Encoding.
  • 204 and 304: SendResponse put Content-Length: 0 on every response without a body, 204 included. RFC 9110 §8.6 forbids Content-Length on a 204.

Change

  • Empty stream: it now sends Content-Length: 0.
  • Handler headers: a Content-Length or Transfer-Encoding the handler set is kept. Header names are compared without case.
  • 204 and 304: they carry no Content-Length, on either path.

Tests

On Delphi 10.2, tests/iocp-http gains CA5:

  • /vazio answers with Content-Length: 0, read with a 2 s limit, and the same keep-alive connection then serves the next request;
  • /sem-conteudo (204) carries no Content-Length.

CA5 passes with this change. Against the previous source with seam-only.patch it fails, because the empty stream carries no Content-Length. CA1–CA4 and CA6 from #638 still pass.

🤖 Generated with Claude Code

marquesorion and others added 2 commits November 25, 2026 18:02
The IOCP response sent each piece once and ignored how many bytes went
out: WriteV (WSASend), the headers, the stream loop and the body sent
after the headers. A partial count truncated the response. The stream
writer's SendFully looped, but on a failure it left IsConnected true.

Every send now goes through SendAll, which repeats until every byte is
out; WriteV sends the rest of a partial gather through it. A failed
send marks the response, closes the connection through the registry
and is never followed by a keep-alive re-arm; IsConnected reads that
state. A response without keep-alive is also closed through the
registry, so its context no longer stays there until CloseAll.

Bytes that arrive after a streamed response (the error response the
router builds when the handler raises after SendStream) are no longer
appended to it: the connection is closed instead.

The low-level sends are replaceable class functions and the registry
count is readable, so tests/iocp-http forces partial sends and failures
on loopback. seam-only.patch adds just that seam to the previous source
for the negative control.

tests/iocp-http passes on Delphi 10.2 (CA1 to CA4, plus CA6 as a load
measure) and, against the previous source with only the seam applied,
fails CA1 to CA4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stream response emitted Content-Length only when the stream had
bytes, so an empty stream on a keep-alive connection went out with
neither Content-Length nor Transfer-Encoding and the client could not
tell where it ended. A non-empty stream with a Transfer-Encoding set by
the handler also got a Content-Length, and every response without a
body, 204 included, carried Content-Length: 0.

An empty stream now sends Content-Length: 0. A Content-Length or
Transfer-Encoding the handler set is kept, compared without case. 204
and 304 carry no Content-Length (RFC 9110, 8.6). tests/iocp-http adds
the length check (CA5).

CA5 passes on Delphi 10.2 and fails against the previous source with
only the seam applied (no Content-Length on the empty stream).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

2 participants