Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support nested multipart, strict no-content parts CRLF and better part metadata resolution #380

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
nested multipart with strict CRLF counts
fmigneault committed Sep 24, 2024
commit caa18cbb8e2ba2aba68516ddee531162dc374381
7 changes: 2 additions & 5 deletions requests_toolbelt/multipart/encoder.py
Original file line number Diff line number Diff line change
@@ -201,14 +201,11 @@ def _load(self, amount):
while amount == -1 or amount > 0:
written = 0
if part and not part.bytes_left_to_write():
next_part = self._next_part()
# distinguish no content from empty string
# also, avoid superfluous newlines if multipart is done
# this is mostly relevant when nesting multipart
if not part.body.no_content and next_part is not None:
if not part.body.no_content:
written += self._write(b'\r\n')
written += self._write_boundary()
part = next_part
part = self._next_part()

if not part:
written += self._write_closing_boundary()
12 changes: 6 additions & 6 deletions tests/test_multipart_encoder.py
Original file line number Diff line number Diff line change
@@ -338,29 +338,29 @@ def test_nested_multipart(self):
b'Content-Disposition: form-data; name="file"; filename="filename"',
b'Content-Type: application/json',
b'Extra-Header: file',
b'\r\n',
b'',
b'{"item1": "data", "item2": [1,2,3]}',
top_boundary,
b'Content-Disposition: form-data; name="multi"',
b'Content-Type: multipart/mixed; boundary=' + sub_boundary.strip(b'-'),
b'Extra-Header: multi',
b'\r\n',
b'',
sub_boundary,
b'Content-Disposition: form-data; name="item1"',
b'Content-Type: text/plain',
b'Extra-Header: data',
b'\r\n',
b'',
b'data',
sub_boundary,
b'Content-Disposition: form-data; name="item2"',
b'Content-Type: application/json',
b'Extra-Header: json',
b'\r\n',
b'',
b'[1,2,3]',
sub_boundary + b'--',
b'\r\n',
b'',
top_boundary + b'--',
b'\r\n',
b'',
])
assert content == expect