Skip to content

fix(indy): stream over Indy without Content-Length: 0 - #636

Draft
edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/indy-stream-chunked
Draft

edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/indy-stream-chunked

Conversation

@edsontmarques

Copy link
Copy Markdown

Problem and change

On Delphi with an Indy provider (Console, VCL, Daemon), SendStream does not deliver its body. THorseWebBrokerStreamWriter.SendRawHeaders sets FResponseInfo.ContentLength := 0 before WriteHeader, so the response goes out with Content-Length: 0:

  • with text/event-stream, the client ends the response at the header, and the events arrive outside any response or are lost;
  • in chunked mode, the response carries both Content-Length: 0 and Transfer-Encoding: chunked. RFC 9112 §6.2 says a sender MUST NOT send both, and a strict client rejects the response.

Changing 0 to -1 is not enough. With ContentLength = -1 and no non-identity TransferEncoding, Indy's TIdHTTPResponseInfo.WriteHeader computes the length itself: from ContentText, or from ContentStream.Size (0 for the empty stream set here). With neither, it sends its default HTML page. Indy only skips the length when the response declares a non-identity Transfer-Encoding.

This patch makes every stream chunked on Indy, SSE included. EventSource accepts chunked SSE, and the Indy server speaks HTTP/1.1.

  • Chunked decision. It becomes a virtual method, UsesChunkedTransfer, computed once in the writer's constructor. The base keeps the previous rule. THorseWebBrokerStreamWriter returns True only when the raw response is a TIdHTTPAppResponse. Other WebBroker hosts (ISAPI, CGI, Apache) keep the previous rule, so IIS or Apache do not frame the chunks twice.
  • Header. On Indy, the writer sets TransferEncoding := 'chunked' and ContentLength := -1. After MoveCookiesAndCustomHeaders, it removes the Transfer-Encoding custom header added by SendHeaders, because Indy appends custom headers without removing duplicates. The empty ContentStream is no longer set, so Indy writes nothing after the final chunk.
  • Docs. doc/streaming.md (English and Portuguese) gains a note on Indy providers.

No new protected field is touched: the change uses the existing TIdHTTPAppResponseFriend.

Validation performed

tests/indy-stream runs the real Console provider with KeepConnectionAlive on and reads the wire with a raw TCP client. It covers four cases:

  • text/event-stream: three events;
  • text/plain: three parts;
  • an ordinary Send on the same connection after the stream;
  • an ordinary Send on its own.

A streaming response must carry no Content-Length and a single Transfer-Encoding: chunked, and its body must decode from the chunks.

Check Result
tests/indy-stream, Delphi 10.2 Tokyo (compiler 32.0), Indy 10.6.2.5366, Windows, Console PASS
Same check against the previous source (-HorseSource) Fails: /sse: a streaming response must not carry Content-Length (got "0")
FPC regressions in tests.yml (FPC 3.3.1, Linux x86_64), stream-factory included Pass

I ran the Delphi check on our fork, which carries the same change on top of other patches. The diff applied to master without conflicts in this area. Reproduce on Windows:

./tests/indy-stream/run-indy-stream.ps1 -Version 19.0 -Provider Console

Validation still needed

  • Other Delphi and Indy versions: I only had Delphi 10.2.
  • The VCL provider (-Provider VCL) and the Linux Daemon: not run.
  • HTTP/1.0 clients are out of scope. Streaming over Indy assumes an HTTP/1.1 client.

🤖 Generated with Claude Code

THorseWebBrokerStreamWriter sent "Content-Length: 0" on Indy, so the
client ended a SendStream response at the header; in chunked mode the
response carried both Content-Length and Transfer-Encoding. Indy writes
a Content-Length unless the response declares a non-identity
Transfer-Encoding, so on Indy every stream is now chunked, SSE
included: the writer declares chunked to Indy, sends no Content-Length
and drops the duplicate custom Transfer-Encoding header. The chunked
decision became a virtual method computed once in the constructor;
other WebBroker hosts keep the previous rule.

tests/indy-stream passes on Delphi 10.2 (Indy 10.6.2.5366) and fails
against the previous source ("must not carry Content-Length").

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