From 8b85d35fd79869766f678cbdc27bfaebe37b5527 Mon Sep 17 00:00:00 2001 From: John Stark Date: Thu, 18 Apr 2024 19:55:54 +0100 Subject: [PATCH] Fix headers being carried between parts. fixes #63 (#135) The contents of the headers dict wasn't cleared when a new part started, If the second part didn't overwrite the header value, it would appear in the wrong part. --- multipart/multipart.py | 4 +++- .../http/mixed_plain_and_base64_encoding.http | 23 +++++++++++++++++++ .../http/mixed_plain_and_base64_encoding.yaml | 18 +++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/test_data/http/mixed_plain_and_base64_encoding.http create mode 100644 tests/test_data/http/mixed_plain_and_base64_encoding.yaml diff --git a/multipart/multipart.py b/multipart/multipart.py index 0505c1b..1ad5011 100644 --- a/multipart/multipart.py +++ b/multipart/multipart.py @@ -1613,7 +1613,9 @@ def _on_end() -> None: is_file = False def on_part_begin(): - pass + # Reset headers in case this isn't the first part. + nonlocal headers + headers = {} def on_part_data(data: bytes, start: int, end: int) -> None: nonlocal writer diff --git a/tests/test_data/http/mixed_plain_and_base64_encoding.http b/tests/test_data/http/mixed_plain_and_base64_encoding.http new file mode 100644 index 0000000..9de1a3b --- /dev/null +++ b/tests/test_data/http/mixed_plain_and_base64_encoding.http @@ -0,0 +1,23 @@ +----boundary +Content-Type: text/plain; charset="UTF-8" +Content-Disposition: form-data; name=field1 +Content-Transfer-Encoding: base64 + +VGVzdCAxMjM= +----boundary +Content-Type: text/plain; charset="UTF-8" +Content-Disposition: form-data; name=field2 + +This is a test. +----boundary +Content-Type: text/plain; charset="UTF-8" +Content-Disposition: form-data; name=field3 +Content-Transfer-Encoding: base64 + +VGVzdCBzdHJpbmcuCg== +----boundary +Content-Type: text/plain; charset="UTF-8" +Content-Disposition: form-data; name=field4 + +This is also a test. +----boundary-- \ No newline at end of file diff --git a/tests/test_data/http/mixed_plain_and_base64_encoding.yaml b/tests/test_data/http/mixed_plain_and_base64_encoding.yaml new file mode 100644 index 0000000..31bfe77 --- /dev/null +++ b/tests/test_data/http/mixed_plain_and_base64_encoding.yaml @@ -0,0 +1,18 @@ +boundary: --boundary +expected: + - name: field1 + type: field + data: !!binary | + VGVzdCAxMjM= + - name: field2 + type: field + data: !!binary | + VGhpcyBpcyBhIHRlc3Qu + - name: field3 + type: field + data: !!binary | + VGVzdCBzdHJpbmcuCg== + - name: field4 + type: field + data: !!binary | + VGhpcyBpcyBhbHNvIGEgdGVzdC4=