Skip to content

drop a passed-through Content-Length when the client reframes the body - #2679

Open
metsw24-max wants to merge 1 commit into
yhirose:masterfrom
metsw24-max:request-content-length-reframe
Open

metsw24-max wants to merge 1 commit into
yhirose:masterfrom
metsw24-max:request-content-length-reframe

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

A stale Content-Length survives when the client reframes a request body

When a caller forwards a request's headers through Post/Put/Patch, the Content-Length it passes through is kept even after the client reframes the body in ClientImpl::send_with_content_provider_and_receiver: with set_compress(true) the buffered body is replaced by its gzip stream, and a content provider without a length is sent as Transfer-Encoding: chunked. prepare_default_headers only adds a Content-Length when none is present, so the forwarded value goes out describing the original body rather than what is on the wire. Passing a Content-Length through is supported usage; ServerTest.NoMultipleHeaders does exactly that. For a compressed body the gzip stream is longer than the plaintext it came from, so a server reading Content-Length bytes takes only the first part and parses the remainder as the next request on a kept-alive connection, which desyncs it; for a chunked provider the client emits both Content-Length and Transfer-Encoding, the ambiguous framing a server rejects. The fix erases Content-Length in that function once the library owns the framing, so write_request emits the length that matches the compressed body, or none for a chunked one; a plain, uncompressed body keeps the caller's value. ClientRequestFramingTest.CompressedBodyReplacesCallerContentLength and ChunkedProviderDropsCallerContentLength fail on the current header and pass with the fix.

This looks like a sibling of #2581, which rejected the same ambiguous framing when reading a response; it seems worth closing on the request-writing side as well.

send_with_content_provider_and_receiver() kept a Content-Length the
caller passed through after the library reframed the body: it compresses
a buffered body under set_compress(true), or streams a provider without a
length as chunked. prepare_default_headers() only adds a Content-Length
when none is present, so the stale value went out describing the original
body. With compression the server reads only that many bytes of the
longer gzip stream and the rest desyncs the next request on a kept-alive
connection; with a chunked provider the client emits both Content-Length
and Transfer-Encoding. Erase it once the library owns the framing so
write_request() emits the matching length, or none for a chunked body.
A plain, uncompressed body keeps the caller's value.

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.

1 participant