Skip to content

fix(pipelineclient): send Content-Length with request bodies - #568

Merged
Mzack9999 merged 1 commit into
mainfrom
dwisiswant0/fix/pipelineclient/send-content-length-with-request-bodies
Nov 21, 2026
Merged

Mzack9999 merged 1 commit into
mainfrom
dwisiswant0/fix/pipelineclient/send-content-length-with-request-bodies

Conversation

@dwisiswant0

Copy link
Copy Markdown
Member

The pipeline client wrote a request body with neither Content-Length nor
Transfer-Encoding. A server frames such a request with no body (RFC 9112
section 6.3), so it read the body as the start of the next request on
the shared connection and answered 400. Post with a strings.Reader and
Dor with a retryablehttp request both hit this: Dor passed req.Body and
dropped req.ContentLength, and nothing set AutomaticContentLength.

Fix it where every entry point meets, in do. A request with a body gets
one Content-Length, taken from the first source that knows it: the
caller's ContentLength when it is above 0 (Do and Dor), the length of a
bytes.Buffer, bytes.Reader or strings.Reader, or else the body read into
memory with io.ReadAll. A body read for its length is sent from that
buffer, and a read error is returned before anything is queued.

A caller that sets Content-Length, in any letter case, has it sent once,
as given. A caller that sets Transfer-Encoding keeps its hand-chunked
body as is, since a Content-Length next to it would give the message two
lengths. An empty body gets no Content-Length, as before.

The new call pushed do over the cyclomatic-complexity limit, so the
request-path code moves out of it into requestPath, unchanged.

I did not route this through AutomaticContentLength. That path only
knows two body types, and the header line it writes lacks the closing
CRLF. The commented-out chunked branch in clientpipeline stays as it is.

A raw GET or HEAD whose body has no length now carries a Content-Length
too, where the body used to run into the next request. An opaque body of
unknown length is held in memory until its request is written.

Fixes: #567

The pipeline client wrote a request body with neither Content-Length nor
Transfer-Encoding. A server frames such a request with no body (RFC 9112
section 6.3), so it read the body as the start of the next request on
the shared connection and answered 400. Post with a strings.Reader and
Dor with a retryablehttp request both hit this: Dor passed req.Body and
dropped req.ContentLength, and nothing set AutomaticContentLength.

Fix it where every entry point meets, in do. A request with a body gets
one Content-Length, taken from the first source that knows it: the
caller's ContentLength when it is above 0 (Do and Dor), the length of a
bytes.Buffer, bytes.Reader or strings.Reader, or else the body read into
memory with io.ReadAll. A body read for its length is sent from that
buffer, and a read error is returned before anything is queued.

A caller that sets Content-Length, in any letter case, has it sent once,
as given. A caller that sets Transfer-Encoding keeps its hand-chunked
body as is, since a Content-Length next to it would give the message two
lengths. An empty body gets no Content-Length, as before.

The new call pushed do over the cyclomatic-complexity limit, so the
request-path code moves out of it into requestPath, unchanged.

I did not route this through AutomaticContentLength. That path only
knows two body types, and the header line it writes lacks the closing
CRLF. The commented-out chunked branch in clientpipeline stays as it is.

A raw GET or HEAD whose body has no length now carries a Content-Length
too, where the body used to run into the next request. An opaque body of
unknown length is held in memory until its request is written.

Fixes: #567
Signed-off-by: Dwi Siswanto <[email protected]>
@Mzack9999
Mzack9999 merged commit d180085 into main Nov 21, 2026
6 checks passed
@Mzack9999
Mzack9999 deleted the dwisiswant0/fix/pipelineclient/send-content-length-with-request-bodies branch November 21, 2026 16:46
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.

Pipeline client sends request bodies without Content-Length

2 participants