jpayne@69: from __future__ import annotations jpayne@69: jpayne@69: import email.utils jpayne@69: import mimetypes jpayne@69: import typing jpayne@69: jpayne@69: _TYPE_FIELD_VALUE = typing.Union[str, bytes] jpayne@69: _TYPE_FIELD_VALUE_TUPLE = typing.Union[ jpayne@69: _TYPE_FIELD_VALUE, jpayne@69: typing.Tuple[str, _TYPE_FIELD_VALUE], jpayne@69: typing.Tuple[str, _TYPE_FIELD_VALUE, str], jpayne@69: ] jpayne@69: jpayne@69: jpayne@69: def guess_content_type( jpayne@69: filename: str | None, default: str = "application/octet-stream" jpayne@69: ) -> str: jpayne@69: """ jpayne@69: Guess the "Content-Type" of a file. jpayne@69: jpayne@69: :param filename: jpayne@69: The filename to guess the "Content-Type" of using :mod:`mimetypes`. jpayne@69: :param default: jpayne@69: If no "Content-Type" can be guessed, default to `default`. jpayne@69: """ jpayne@69: if filename: jpayne@69: return mimetypes.guess_type(filename)[0] or default jpayne@69: return default jpayne@69: jpayne@69: jpayne@69: def format_header_param_rfc2231(name: str, value: _TYPE_FIELD_VALUE) -> str: jpayne@69: """ jpayne@69: Helper function to format and quote a single header parameter using the jpayne@69: strategy defined in RFC 2231. jpayne@69: jpayne@69: Particularly useful for header parameters which might contain jpayne@69: non-ASCII values, like file names. This follows jpayne@69: `RFC 2388 Section 4.4 `_. jpayne@69: jpayne@69: :param name: jpayne@69: The name of the parameter, a string expected to be ASCII only. jpayne@69: :param value: jpayne@69: The value of the parameter, provided as ``bytes`` or `str``. jpayne@69: :returns: jpayne@69: An RFC-2231-formatted unicode string. jpayne@69: jpayne@69: .. deprecated:: 2.0.0 jpayne@69: Will be removed in urllib3 v2.1.0. This is not valid for jpayne@69: ``multipart/form-data`` header parameters. jpayne@69: """ jpayne@69: import warnings jpayne@69: jpayne@69: warnings.warn( jpayne@69: "'format_header_param_rfc2231' is deprecated and will be " jpayne@69: "removed in urllib3 v2.1.0. This is not valid for " jpayne@69: "multipart/form-data header parameters.", jpayne@69: DeprecationWarning, jpayne@69: stacklevel=2, jpayne@69: ) jpayne@69: jpayne@69: if isinstance(value, bytes): jpayne@69: value = value.decode("utf-8") jpayne@69: jpayne@69: if not any(ch in value for ch in '"\\\r\n'): jpayne@69: result = f'{name}="{value}"' jpayne@69: try: jpayne@69: result.encode("ascii") jpayne@69: except (UnicodeEncodeError, UnicodeDecodeError): jpayne@69: pass jpayne@69: else: jpayne@69: return result jpayne@69: jpayne@69: value = email.utils.encode_rfc2231(value, "utf-8") jpayne@69: value = f"{name}*={value}" jpayne@69: jpayne@69: return value jpayne@69: jpayne@69: jpayne@69: def format_multipart_header_param(name: str, value: _TYPE_FIELD_VALUE) -> str: jpayne@69: """ jpayne@69: Format and quote a single multipart header parameter. jpayne@69: jpayne@69: This follows the `WHATWG HTML Standard`_ as of 2021/06/10, matching jpayne@69: the behavior of current browser and curl versions. Values are jpayne@69: assumed to be UTF-8. The ``\\n``, ``\\r``, and ``"`` characters are jpayne@69: percent encoded. jpayne@69: jpayne@69: .. _WHATWG HTML Standard: jpayne@69: https://html.spec.whatwg.org/multipage/ jpayne@69: form-control-infrastructure.html#multipart-form-data jpayne@69: jpayne@69: :param name: jpayne@69: The name of the parameter, an ASCII-only ``str``. jpayne@69: :param value: jpayne@69: The value of the parameter, a ``str`` or UTF-8 encoded jpayne@69: ``bytes``. jpayne@69: :returns: jpayne@69: A string ``name="value"`` with the escaped value. jpayne@69: jpayne@69: .. versionchanged:: 2.0.0 jpayne@69: Matches the WHATWG HTML Standard as of 2021/06/10. Control jpayne@69: characters are no longer percent encoded. jpayne@69: jpayne@69: .. versionchanged:: 2.0.0 jpayne@69: Renamed from ``format_header_param_html5`` and jpayne@69: ``format_header_param``. The old names will be removed in jpayne@69: urllib3 v2.1.0. jpayne@69: """ jpayne@69: if isinstance(value, bytes): jpayne@69: value = value.decode("utf-8") jpayne@69: jpayne@69: # percent encode \n \r " jpayne@69: value = value.translate({10: "%0A", 13: "%0D", 34: "%22"}) jpayne@69: return f'{name}="{value}"' jpayne@69: jpayne@69: jpayne@69: def format_header_param_html5(name: str, value: _TYPE_FIELD_VALUE) -> str: jpayne@69: """ jpayne@69: .. deprecated:: 2.0.0 jpayne@69: Renamed to :func:`format_multipart_header_param`. Will be jpayne@69: removed in urllib3 v2.1.0. jpayne@69: """ jpayne@69: import warnings jpayne@69: jpayne@69: warnings.warn( jpayne@69: "'format_header_param_html5' has been renamed to " jpayne@69: "'format_multipart_header_param'. The old name will be " jpayne@69: "removed in urllib3 v2.1.0.", jpayne@69: DeprecationWarning, jpayne@69: stacklevel=2, jpayne@69: ) jpayne@69: return format_multipart_header_param(name, value) jpayne@69: jpayne@69: jpayne@69: def format_header_param(name: str, value: _TYPE_FIELD_VALUE) -> str: jpayne@69: """ jpayne@69: .. deprecated:: 2.0.0 jpayne@69: Renamed to :func:`format_multipart_header_param`. Will be jpayne@69: removed in urllib3 v2.1.0. jpayne@69: """ jpayne@69: import warnings jpayne@69: jpayne@69: warnings.warn( jpayne@69: "'format_header_param' has been renamed to " jpayne@69: "'format_multipart_header_param'. The old name will be " jpayne@69: "removed in urllib3 v2.1.0.", jpayne@69: DeprecationWarning, jpayne@69: stacklevel=2, jpayne@69: ) jpayne@69: return format_multipart_header_param(name, value) jpayne@69: jpayne@69: jpayne@69: class RequestField: jpayne@69: """ jpayne@69: A data container for request body parameters. jpayne@69: jpayne@69: :param name: jpayne@69: The name of this request field. Must be unicode. jpayne@69: :param data: jpayne@69: The data/value body. jpayne@69: :param filename: jpayne@69: An optional filename of the request field. Must be unicode. jpayne@69: :param headers: jpayne@69: An optional dict-like object of headers to initially use for the field. jpayne@69: jpayne@69: .. versionchanged:: 2.0.0 jpayne@69: The ``header_formatter`` parameter is deprecated and will jpayne@69: be removed in urllib3 v2.1.0. jpayne@69: """ jpayne@69: jpayne@69: def __init__( jpayne@69: self, jpayne@69: name: str, jpayne@69: data: _TYPE_FIELD_VALUE, jpayne@69: filename: str | None = None, jpayne@69: headers: typing.Mapping[str, str] | None = None, jpayne@69: header_formatter: typing.Callable[[str, _TYPE_FIELD_VALUE], str] | None = None, jpayne@69: ): jpayne@69: self._name = name jpayne@69: self._filename = filename jpayne@69: self.data = data jpayne@69: self.headers: dict[str, str | None] = {} jpayne@69: if headers: jpayne@69: self.headers = dict(headers) jpayne@69: jpayne@69: if header_formatter is not None: jpayne@69: import warnings jpayne@69: jpayne@69: warnings.warn( jpayne@69: "The 'header_formatter' parameter is deprecated and " jpayne@69: "will be removed in urllib3 v2.1.0.", jpayne@69: DeprecationWarning, jpayne@69: stacklevel=2, jpayne@69: ) jpayne@69: self.header_formatter = header_formatter jpayne@69: else: jpayne@69: self.header_formatter = format_multipart_header_param jpayne@69: jpayne@69: @classmethod jpayne@69: def from_tuples( jpayne@69: cls, jpayne@69: fieldname: str, jpayne@69: value: _TYPE_FIELD_VALUE_TUPLE, jpayne@69: header_formatter: typing.Callable[[str, _TYPE_FIELD_VALUE], str] | None = None, jpayne@69: ) -> RequestField: jpayne@69: """ jpayne@69: A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters. jpayne@69: jpayne@69: Supports constructing :class:`~urllib3.fields.RequestField` from jpayne@69: parameter of key/value strings AND key/filetuple. A filetuple is a jpayne@69: (filename, data, MIME type) tuple where the MIME type is optional. jpayne@69: For example:: jpayne@69: jpayne@69: 'foo': 'bar', jpayne@69: 'fakefile': ('foofile.txt', 'contents of foofile'), jpayne@69: 'realfile': ('barfile.txt', open('realfile').read()), jpayne@69: 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'), jpayne@69: 'nonamefile': 'contents of nonamefile field', jpayne@69: jpayne@69: Field names and filenames must be unicode. jpayne@69: """ jpayne@69: filename: str | None jpayne@69: content_type: str | None jpayne@69: data: _TYPE_FIELD_VALUE jpayne@69: jpayne@69: if isinstance(value, tuple): jpayne@69: if len(value) == 3: jpayne@69: filename, data, content_type = value jpayne@69: else: jpayne@69: filename, data = value jpayne@69: content_type = guess_content_type(filename) jpayne@69: else: jpayne@69: filename = None jpayne@69: content_type = None jpayne@69: data = value jpayne@69: jpayne@69: request_param = cls( jpayne@69: fieldname, data, filename=filename, header_formatter=header_formatter jpayne@69: ) jpayne@69: request_param.make_multipart(content_type=content_type) jpayne@69: jpayne@69: return request_param jpayne@69: jpayne@69: def _render_part(self, name: str, value: _TYPE_FIELD_VALUE) -> str: jpayne@69: """ jpayne@69: Override this method to change how each multipart header jpayne@69: parameter is formatted. By default, this calls jpayne@69: :func:`format_multipart_header_param`. jpayne@69: jpayne@69: :param name: jpayne@69: The name of the parameter, an ASCII-only ``str``. jpayne@69: :param value: jpayne@69: The value of the parameter, a ``str`` or UTF-8 encoded jpayne@69: ``bytes``. jpayne@69: jpayne@69: :meta public: jpayne@69: """ jpayne@69: return self.header_formatter(name, value) jpayne@69: jpayne@69: def _render_parts( jpayne@69: self, jpayne@69: header_parts: ( jpayne@69: dict[str, _TYPE_FIELD_VALUE | None] jpayne@69: | typing.Sequence[tuple[str, _TYPE_FIELD_VALUE | None]] jpayne@69: ), jpayne@69: ) -> str: jpayne@69: """ jpayne@69: Helper function to format and quote a single header. jpayne@69: jpayne@69: Useful for single headers that are composed of multiple items. E.g., jpayne@69: 'Content-Disposition' fields. jpayne@69: jpayne@69: :param header_parts: jpayne@69: A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format jpayne@69: as `k1="v1"; k2="v2"; ...`. jpayne@69: """ jpayne@69: iterable: typing.Iterable[tuple[str, _TYPE_FIELD_VALUE | None]] jpayne@69: jpayne@69: parts = [] jpayne@69: if isinstance(header_parts, dict): jpayne@69: iterable = header_parts.items() jpayne@69: else: jpayne@69: iterable = header_parts jpayne@69: jpayne@69: for name, value in iterable: jpayne@69: if value is not None: jpayne@69: parts.append(self._render_part(name, value)) jpayne@69: jpayne@69: return "; ".join(parts) jpayne@69: jpayne@69: def render_headers(self) -> str: jpayne@69: """ jpayne@69: Renders the headers for this request field. jpayne@69: """ jpayne@69: lines = [] jpayne@69: jpayne@69: sort_keys = ["Content-Disposition", "Content-Type", "Content-Location"] jpayne@69: for sort_key in sort_keys: jpayne@69: if self.headers.get(sort_key, False): jpayne@69: lines.append(f"{sort_key}: {self.headers[sort_key]}") jpayne@69: jpayne@69: for header_name, header_value in self.headers.items(): jpayne@69: if header_name not in sort_keys: jpayne@69: if header_value: jpayne@69: lines.append(f"{header_name}: {header_value}") jpayne@69: jpayne@69: lines.append("\r\n") jpayne@69: return "\r\n".join(lines) jpayne@69: jpayne@69: def make_multipart( jpayne@69: self, jpayne@69: content_disposition: str | None = None, jpayne@69: content_type: str | None = None, jpayne@69: content_location: str | None = None, jpayne@69: ) -> None: jpayne@69: """ jpayne@69: Makes this request field into a multipart request field. jpayne@69: jpayne@69: This method overrides "Content-Disposition", "Content-Type" and jpayne@69: "Content-Location" headers to the request parameter. jpayne@69: jpayne@69: :param content_disposition: jpayne@69: The 'Content-Disposition' of the request body. Defaults to 'form-data' jpayne@69: :param content_type: jpayne@69: The 'Content-Type' of the request body. jpayne@69: :param content_location: jpayne@69: The 'Content-Location' of the request body. jpayne@69: jpayne@69: """ jpayne@69: content_disposition = (content_disposition or "form-data") + "; ".join( jpayne@69: [ jpayne@69: "", jpayne@69: self._render_parts( jpayne@69: (("name", self._name), ("filename", self._filename)) jpayne@69: ), jpayne@69: ] jpayne@69: ) jpayne@69: jpayne@69: self.headers["Content-Disposition"] = content_disposition jpayne@69: self.headers["Content-Type"] = content_type jpayne@69: self.headers["Content-Location"] = content_location