Skip to content

Fix chunked multipart bodies with empty or partly read files - #387

Draft
mame wants to merge 4 commits into
ruby:masterfrom
mame:multipart-chunker
Draft

mame wants to merge 4 commits into
ruby:masterfrom
mame:multipart-chunker

Conversation

@mame

@mame mame commented Nov 30, 2026

Copy link
Copy Markdown
Member

This combines and replaces #330 and #373, and also makes chunked multipart uploads faster by reading file parts in larger blocks.

When a multipart form is sent with Transfer-Encoding: chunked, a file part that responds to #size is written as a single chunk of that size. An empty file then writes a zero-size chunk, which ends the body, and a partly read file declares more bytes than it sends.

Changes

Performance

A 300 MB chunked multipart upload over loopback (median of 60 runs):

HTTP HTTPS
master 197 ms 331 ms
this PR 123 ms 326 ms

With 4 KB blocks, the same upload took about 1.4 s over HTTP.

mame and others added 4 commits November 30, 2026 17:10
The chunked multipart encoder wrote a file part that responds to #size as
one chunk of that size, so an empty file wrote the last chunk and a
partly read file declared more bytes than it sent. Remove that branch so
that every file part goes through the loop that sizes each chunk by the
bytes actually read.

Co-authored-by: Orien Madgwick <vcs@orien.io>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pass a Chunker to encode_multipart_form_data instead of framing chunks
there, as ruby#330 does for file parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A zero-size chunk marks the end of a chunked body, so Chunker#write must
not emit one for an empty string. IO.copy_stream passes an empty string
when the source's read returns one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each block read from a file part is written as one chunk when the body
is chunked, so larger blocks reduce the number of chunks and writes.

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.

1 participant