From 94e7f4b5c1b6555656609a34f0e2c447b52e53b5 Mon Sep 17 00:00:00 2001 From: Francis Charette Migneault Date: Tue, 24 Sep 2024 03:22:44 -0400 Subject: [PATCH] fix multipart decoder to allow no-contents part (fixes https://github.com/requests/toolbelt/issues/352) --- requests_toolbelt/multipart/decoder.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/requests_toolbelt/multipart/decoder.py b/requests_toolbelt/multipart/decoder.py index 2a0d1c46..c39c5db5 100644 --- a/requests_toolbelt/multipart/decoder.py +++ b/requests_toolbelt/multipart/decoder.py @@ -59,7 +59,18 @@ def __init__(self, content, encoding): first, self.content = _split_on_find(content, b'\r\n\r\n') if first != b'': headers = _header_parser(first.lstrip(), encoding) + # because pre-split by boundary was done, + # having only a single newline and the '--' + # means that this is a 'no content' part + elif content.endswith(b'\r\n') and not content.endswith(b'\r\n\r\n'): + self.content = None + headers = _header_parser(content.strip(), encoding) + if not headers: + raise ImproperBodyPartContentException( + 'No contents part without any header is invalid.' + ) else: + print("==" + content.decode() + "===") raise ImproperBodyPartContentException( 'content does not contain CR-LF-CR-LF' ) @@ -68,6 +79,8 @@ def __init__(self, content, encoding): @property def text(self): """Content of the ``BodyPart`` in unicode.""" + if self.content is None: + return None return self.content.decode(self.encoding)