_multipart.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. from __future__ import annotations
  2. import secrets
  3. from typing import TYPE_CHECKING, cast, final
  4. from ._pyqwest import Headers
  5. if TYPE_CHECKING:
  6. from collections.abc import AsyncIterator, Iterable, Iterator, Mapping
  7. from ._pyqwest import HTTPHeaderName
  8. _PartHeaders = (
  9. Headers
  10. | Mapping[str | HTTPHeaderName, str]
  11. | Iterable[tuple[str | HTTPHeaderName, str]]
  12. )
  13. @final
  14. class Part:
  15. """A single part of a multipart form, for use with Multipart."""
  16. __slots__ = ("_content", "_filename", "_headers")
  17. def __init__(
  18. self,
  19. content: bytes | str | AsyncIterator[bytes],
  20. *,
  21. filename: str | None = None,
  22. headers: _PartHeaders | None = None,
  23. ) -> None:
  24. """Creates a new Part object.
  25. Args:
  26. content: The content of the part. A str will be encoded as UTF-8.
  27. An async iterator of bytes will be streamed.
  28. filename: The filename to send in the part's content-disposition header.
  29. headers: Additional headers to send with the part, for example
  30. content-type.
  31. Raises:
  32. ValueError: If a header name or value is invalid.
  33. """
  34. self._content = content.encode() if isinstance(content, str) else content
  35. self._filename = filename
  36. self._headers = headers if isinstance(headers, Headers) else Headers(headers)
  37. @property
  38. def content(self) -> bytes | AsyncIterator[bytes]:
  39. """Returns the content of the part."""
  40. return self._content
  41. @property
  42. def filename(self) -> str | None:
  43. """Returns the filename of the part."""
  44. return self._filename
  45. @property
  46. def headers(self) -> Headers:
  47. """Returns the headers of the part."""
  48. return self._headers
  49. @final
  50. class Multipart:
  51. """Multipart form request content for asynchronous requests. For
  52. synchronous requests, use SyncMultipart.
  53. Passing a Multipart object as request content encodes it into the request
  54. content as a multipart/form-data request. The multipart boundary is
  55. generated when constructing the request, and the request uses a copy of
  56. the provided headers with the content-type header set to match the
  57. boundary. The provided headers must not have a content-type other than
  58. multipart/form-data.
  59. """
  60. __slots__ = ("_parts",)
  61. def __init__(
  62. self,
  63. parts: Mapping[str, Part | bytes | str]
  64. | Iterable[tuple[str, Part | bytes | str]],
  65. ) -> None:
  66. """Creates a new Multipart object.
  67. Args:
  68. parts: The named parts of the form. bytes or str values are
  69. converted to parts without a filename or headers.
  70. """
  71. items = (
  72. cast("Mapping[str, Part | bytes | str]", parts).items()
  73. if hasattr(parts, "items")
  74. else cast("Iterable[tuple[str, Part | bytes | str]]", parts)
  75. )
  76. self._parts = [
  77. (name, part if isinstance(part, Part) else Part(part))
  78. for name, part in items
  79. ]
  80. @property
  81. def parts(self) -> list[tuple[str, Part]]:
  82. """Returns the named parts of the form."""
  83. return list(self._parts)
  84. @final
  85. class SyncPart:
  86. """A single part of a multipart form, for use with SyncMultipart."""
  87. __slots__ = ("_content", "_filename", "_headers")
  88. def __init__(
  89. self,
  90. content: bytes | str | Iterable[bytes],
  91. *,
  92. filename: str | None = None,
  93. headers: _PartHeaders | None = None,
  94. ) -> None:
  95. """Creates a new SyncPart object.
  96. Args:
  97. content: The content of the part. A str will be encoded as UTF-8.
  98. An iterable of bytes will be streamed.
  99. filename: The filename to send in the part's content-disposition header.
  100. headers: Additional headers to send with the part, for example
  101. content-type.
  102. Raises:
  103. ValueError: If a header name or value is invalid.
  104. """
  105. self._content = content.encode() if isinstance(content, str) else content
  106. self._filename = filename
  107. self._headers = headers if isinstance(headers, Headers) else Headers(headers)
  108. @property
  109. def content(self) -> bytes | Iterable[bytes]:
  110. """Returns the content of the part."""
  111. return self._content
  112. @property
  113. def filename(self) -> str | None:
  114. """Returns the filename of the part."""
  115. return self._filename
  116. @property
  117. def headers(self) -> Headers:
  118. """Returns the headers of the part."""
  119. return self._headers
  120. @final
  121. class SyncMultipart:
  122. """Multipart form request content for synchronous requests. For
  123. asynchronous requests, use Multipart.
  124. Passing a SyncMultipart object as request content encodes it into the
  125. request content as a multipart/form-data request. The multipart boundary
  126. is generated when constructing the request, and the request uses a copy of
  127. the provided headers with the content-type header set to match the
  128. boundary. The provided headers must not have a content-type other than
  129. multipart/form-data.
  130. """
  131. __slots__ = ("_parts",)
  132. def __init__(
  133. self,
  134. parts: Mapping[str, SyncPart | bytes | str]
  135. | Iterable[tuple[str, SyncPart | bytes | str]],
  136. ) -> None:
  137. """Creates a new SyncMultipart object.
  138. Args:
  139. parts: The named parts of the form. bytes or str values are
  140. converted to parts without a filename or headers.
  141. """
  142. items = (
  143. cast("Mapping[str, SyncPart | bytes | str]", parts).items()
  144. if hasattr(parts, "items")
  145. else cast("Iterable[tuple[str, SyncPart | bytes | str]]", parts)
  146. )
  147. self._parts = [
  148. (name, part if isinstance(part, SyncPart) else SyncPart(part))
  149. for name, part in items
  150. ]
  151. @property
  152. def parts(self) -> list[tuple[str, SyncPart]]:
  153. """Returns the named parts of the form."""
  154. return list(self._parts)
  155. def multipart_boundary() -> str:
  156. return secrets.token_hex(16)
  157. def multipart_content_type(boundary: str) -> str:
  158. return f"multipart/form-data; boundary={boundary}"
  159. # The escaped characters match reqwest's percent-encoding of part names and
  160. # filenames (the WHATWG path-segment set), so that requests put the same bytes
  161. # on the wire regardless of transport. Notably, this keeps CR/LF and quotes
  162. # out of the part headers.
  163. _ESCAPE_CHARS = frozenset(' "<>`#?{}/%' + "".join(map(chr, range(0x20))) + "\x7f")
  164. def _escape(value: str) -> str:
  165. if not any(c in _ESCAPE_CHARS for c in value):
  166. return value
  167. return "".join(f"%{ord(c):02X}" if c in _ESCAPE_CHARS else c for c in value)
  168. def _part_header(boundary: str, part_name: str, part: Part | SyncPart) -> bytes:
  169. lines = [f"--{boundary}"]
  170. disposition = f'content-disposition: form-data; name="{_escape(part_name)}"'
  171. if part.filename is not None:
  172. disposition += f'; filename="{_escape(part.filename)}"'
  173. lines.append(disposition)
  174. lines.extend(f"{name}: {value}" for name, value in part.headers.items())
  175. lines.extend(["", ""])
  176. return "\r\n".join(lines).encode()
  177. def encode_multipart_sync(multipart: SyncMultipart, boundary: str) -> Iterator[bytes]:
  178. for part_name, part in multipart.parts:
  179. yield _part_header(boundary, part_name, part)
  180. content = part.content
  181. if isinstance(content, bytes):
  182. yield content
  183. else:
  184. try:
  185. yield from content
  186. finally:
  187. close = getattr(content, "close", None)
  188. if close is not None:
  189. close()
  190. yield b"\r\n"
  191. yield f"--{boundary}--\r\n".encode()
  192. async def encode_multipart(multipart: Multipart, boundary: str) -> AsyncIterator[bytes]:
  193. for part_name, part in multipart.parts:
  194. yield _part_header(boundary, part_name, part)
  195. content = part.content
  196. if isinstance(content, bytes):
  197. yield content
  198. else:
  199. try:
  200. async for chunk in content:
  201. yield chunk
  202. finally:
  203. aclose = getattr(content, "aclose", None)
  204. if aclose is not None:
  205. await aclose()
  206. yield b"\r\n"
  207. yield f"--{boundary}--\r\n".encode()