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