Skip to content

Stop a body stream at the declared Content-Length - #382

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/body-stream-content-length
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/body-stream-content-length

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #376.

send_request_with_body_stream wrote the Content-Length header and then IO.copy_stream of the whole IO. Bytes past the declared length were written on the socket. A stream shorter than the declared length was written with no error.

The copy now stops at Content-Length. If fewer bytes were available, or the stream already has another byte, this raises ArgumentError. A pipe that has not produced another byte yet is left alone, because reading it would block, and those bytes were not written. Chunked requests are unchanged: the chunk framing covers the whole stream.

Before the change, a 5-byte Content-Length and a longer stream wrote AAAAA plus the rest ("AAAAAGET /x HTTP/1.1\r\n\r\n"). After it, the same request raises ArgumentError and the socket has only AAAAA.

ruby -Ilib:test/lib -rhelper test/net/http/test_http_request.rb: 11 tests, 44 assertions, 0 failures.

A stream longer than the declared length used to be written in full.
A shorter stream was written without an error.
IO.copy_stream wrote the whole stream after the header had announced a
shorter length. Copy only that many bytes, and raise ArgumentError when
the stream is shorter or already has more.

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.

Content-Length is not enforced for body_stream, so a longer stream smuggles a second request

1 participant