Repository navigation
Conversation
orien
force-pushed
the
chunked-multipart-empty-file
branch
2 times, most recently
from
November 27, 2026 13:42
7ed6b0f to
25ba273
Compare
orien
force-pushed
the
chunked-multipart-empty-file
branch
2 times, most recently
from
November 27, 2026 13:48
58d9de0 to
bc1bd40
Compare
With Transfer-Encoding: chunked, a File or StringIO value in set_form was written as one chunk whose size came from #size. An empty file produced a zero-size chunk, which ends the chunked body, so the server read the remaining form parts as the start of a new request. For a value whose read position was past the start, the chunk-size line counted the whole file but only the unread bytes followed. Separately, any other IO whose read returned an empty string before end of stream wrote a zero-size chunk in the same way. Stream every IO value through flush_buffer instead, so each chunk is sized by the bytes actually read. flush_buffer now writes nothing for an empty buffer, so the only zero-size chunk in a multipart body is the final one. It also no longer clears the strings it writes, so a value whose read returns a frozen string is sent rather than raising FrozenError.
orien
force-pushed
the
chunked-multipart-empty-file
branch
from
November 27, 2026 14:18
bc1bd40 to
1b3f354
Compare
orien
marked this pull request as ready for review
November 27, 2026 14:21
Contributor
Author
|
/cc @mame |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Sending a multipart form with
Transfer-Encoding: chunkedcorrupts the request when a file part is empty or already partly read. EachFileorStringIOvalue is written as one chunk sized by#size, and that size can be wrong:0\r\n\r\n, which marks the end of a chunked body. The server stops reading there and treats the remaining form parts as a new request on the same connection.Change
Every
IOvalue is now streamed throughflush_buffer, which sizes each chunk by the bytes it actually read. The special case for values that respond tosizeis removed.flush_bufferskips empty buffers, so the only zero-size chunk is the final one.flush_bufferno longer clears the strings it writes, so a value whosereadreturns a frozen string is sent instead of raisingFrozenError.StringIOand an object whose first read returns"". It checks the decoded body and that nothing follows the last chunk.TODO: test with chunkedis removed.Considerations
IO.copy_stream's 16 KB writes. Each chunk's size line, data and CRLF go out in a single write, asChunker#writedoes.ChunkerwithIO.copy_streamwould restore larger writes, butChunker#writehas the same empty-chunk problem, so it would need the same guard first.