Skip to content
Myr

Fixed multipart header params encoding (#167)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nik · committed by Seth Michael Larson · parent 44beaa1 · Browse files · 2 files+47 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+47 added−14 removed

  • tests:+18 added−0 removed
  • other:+29 added−14 removed

2 files+47 added−14 removed

modified:httpx/multipart.py+29 added-14 removed
1import binascii2import mimetypes3import os4import re5import typing6from io import BytesIO6from urllib.parse import quote78_HTML5_FORM_ENCODING_REPLACEMENTS = {'"': "%22", "\\": "\\\\"}9_HTML5_FORM_ENCODING_REPLACEMENTS.update(10 {chr(c): "%{:02X}".format(c) for c in range(0x00, 0x1F + 1) if c != 0x1B}11)12_HTML5_FORM_ENCODING_RE = re.compile(13 r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()])14)151617class Field:18 def render_headers(self) -> bytes:19 raise NotImplementedError() # pragma: nocover20
10 unmodified lines
31 self.name = name32 self.value = value3334 def render_headers(self) -> bytes:27 name = quote(self.name, encoding="utf-8").encode("ascii")28 return b"".join(29 [b'Content-Disposition: form-data; name="', name, b'"\r\n' b"\r\n"]30 )35 name = _format_param("name", self.name)36 return b"".join([b"Content-Disposition: form-data; ", name, b"\r\n\r\n"])3738 def render_data(self) -> bytes:39 return (40 self.value if isinstance(self.value, bytes) else self.value.encode("utf-8")
19 unmodified lines
60 def guess_content_type(self) -> str:61 return mimetypes.guess_type(self.filename)[0] or "application/octet-stream"6263 def render_headers(self) -> bytes:58 name = quote(self.name, encoding="utf-8").encode("ascii")59 filename = quote(self.filename, encoding="utf-8").encode("ascii")60 content_type = self.content_type.encode("ascii")64 name = _format_param("name", self.name)65 filename = _format_param("filename", self.filename)66 content_type = self.content_type.encode()67 return b"".join(68 [63 b'Content-Disposition: form-data; name="',69 b"Content-Disposition: form-data; ",70 name,65 b'"; filename="',71 b"; ",72 filename,67 b'"\r\n',68 b"Content-Type: ",73 b"\r\nContent-Type: ",74 content_type,70 b"\r\n",71 b"\r\n",75 b"\r\n\r\n",76 ]77 )7879 def render_data(self) -> bytes:
27 unmodified lines
107108 content_type = "multipart/form-data; boundary=%s" % boundary.decode("ascii")109110 return body.getvalue(), content_type111112113def _format_param(name: str, value: typing.Union[str, bytes]) -> bytes:114 if isinstance(value, bytes):115 value = value.decode()116 117 def replacer(match: typing.Match[str]) -> str:118 return _HTML5_FORM_ENCODING_REPLACEMENTS[match.group(0)]119120 value = _HTML5_FORM_ENCODING_RE.sub(replacer, value)121 return f'{name}="{value}"'.encode()
modified:tests/test_multipart.py+18 added
121 unmodified lines
122 "Content-Type: text/plain\r\n\r\n<file content>\r\n"123 "--{0}--\r\n"124 "".format(boundary).encode("ascii")125 )126127128class TestHeaderParamHTML5Formatting:129 def test_unicode(self):130 param = multipart._format_param("filename", "n\u00e4me")131 assert param == b'filename="n\xc3\xa4me"'132133 def test_ascii(self):134 param = multipart._format_param("filename", b"name")135 assert param == b'filename="name"'136137 def test_unicode_escape(self):138 param = multipart._format_param("filename", "hello\\world\u0022")139 assert param == b'filename="hello\\\\world%22"'140141 def test_unicode_with_control_character(self):142 param = multipart._format_param("filename", "hello\x1A\x1B\x1C")143 assert param == b'filename="hello%1A\x1B%1C"'