annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/urllib3/fields.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 from __future__ import annotations
jpayne@69 2
jpayne@69 3 import email.utils
jpayne@69 4 import mimetypes
jpayne@69 5 import typing
jpayne@69 6
jpayne@69 7 _TYPE_FIELD_VALUE = typing.Union[str, bytes]
jpayne@69 8 _TYPE_FIELD_VALUE_TUPLE = typing.Union[
jpayne@69 9 _TYPE_FIELD_VALUE,
jpayne@69 10 typing.Tuple[str, _TYPE_FIELD_VALUE],
jpayne@69 11 typing.Tuple[str, _TYPE_FIELD_VALUE, str],
jpayne@69 12 ]
jpayne@69 13
jpayne@69 14
jpayne@69 15 def guess_content_type(
jpayne@69 16 filename: str | None, default: str = "application/octet-stream"
jpayne@69 17 ) -> str:
jpayne@69 18 """
jpayne@69 19 Guess the "Content-Type" of a file.
jpayne@69 20
jpayne@69 21 :param filename:
jpayne@69 22 The filename to guess the "Content-Type" of using :mod:`mimetypes`.
jpayne@69 23 :param default:
jpayne@69 24 If no "Content-Type" can be guessed, default to `default`.
jpayne@69 25 """
jpayne@69 26 if filename:
jpayne@69 27 return mimetypes.guess_type(filename)[0] or default
jpayne@69 28 return default
jpayne@69 29
jpayne@69 30
jpayne@69 31 def format_header_param_rfc2231(name: str, value: _TYPE_FIELD_VALUE) -> str:
jpayne@69 32 """
jpayne@69 33 Helper function to format and quote a single header parameter using the
jpayne@69 34 strategy defined in RFC 2231.
jpayne@69 35
jpayne@69 36 Particularly useful for header parameters which might contain
jpayne@69 37 non-ASCII values, like file names. This follows
jpayne@69 38 `RFC 2388 Section 4.4 <https://tools.ietf.org/html/rfc2388#section-4.4>`_.
jpayne@69 39
jpayne@69 40 :param name:
jpayne@69 41 The name of the parameter, a string expected to be ASCII only.
jpayne@69 42 :param value:
jpayne@69 43 The value of the parameter, provided as ``bytes`` or `str``.
jpayne@69 44 :returns:
jpayne@69 45 An RFC-2231-formatted unicode string.
jpayne@69 46
jpayne@69 47 .. deprecated:: 2.0.0
jpayne@69 48 Will be removed in urllib3 v2.1.0. This is not valid for
jpayne@69 49 ``multipart/form-data`` header parameters.
jpayne@69 50 """
jpayne@69 51 import warnings
jpayne@69 52
jpayne@69 53 warnings.warn(
jpayne@69 54 "'format_header_param_rfc2231' is deprecated and will be "
jpayne@69 55 "removed in urllib3 v2.1.0. This is not valid for "
jpayne@69 56 "multipart/form-data header parameters.",
jpayne@69 57 DeprecationWarning,
jpayne@69 58 stacklevel=2,
jpayne@69 59 )
jpayne@69 60
jpayne@69 61 if isinstance(value, bytes):
jpayne@69 62 value = value.decode("utf-8")
jpayne@69 63
jpayne@69 64 if not any(ch in value for ch in '"\\\r\n'):
jpayne@69 65 result = f'{name}="{value}"'
jpayne@69 66 try:
jpayne@69 67 result.encode("ascii")
jpayne@69 68 except (UnicodeEncodeError, UnicodeDecodeError):
jpayne@69 69 pass
jpayne@69 70 else:
jpayne@69 71 return result
jpayne@69 72
jpayne@69 73 value = email.utils.encode_rfc2231(value, "utf-8")
jpayne@69 74 value = f"{name}*={value}"
jpayne@69 75
jpayne@69 76 return value
jpayne@69 77
jpayne@69 78
jpayne@69 79 def format_multipart_header_param(name: str, value: _TYPE_FIELD_VALUE) -> str:
jpayne@69 80 """
jpayne@69 81 Format and quote a single multipart header parameter.
jpayne@69 82
jpayne@69 83 This follows the `WHATWG HTML Standard`_ as of 2021/06/10, matching
jpayne@69 84 the behavior of current browser and curl versions. Values are
jpayne@69 85 assumed to be UTF-8. The ``\\n``, ``\\r``, and ``"`` characters are
jpayne@69 86 percent encoded.
jpayne@69 87
jpayne@69 88 .. _WHATWG HTML Standard:
jpayne@69 89 https://html.spec.whatwg.org/multipage/
jpayne@69 90 form-control-infrastructure.html#multipart-form-data
jpayne@69 91
jpayne@69 92 :param name:
jpayne@69 93 The name of the parameter, an ASCII-only ``str``.
jpayne@69 94 :param value:
jpayne@69 95 The value of the parameter, a ``str`` or UTF-8 encoded
jpayne@69 96 ``bytes``.
jpayne@69 97 :returns:
jpayne@69 98 A string ``name="value"`` with the escaped value.
jpayne@69 99
jpayne@69 100 .. versionchanged:: 2.0.0
jpayne@69 101 Matches the WHATWG HTML Standard as of 2021/06/10. Control
jpayne@69 102 characters are no longer percent encoded.
jpayne@69 103
jpayne@69 104 .. versionchanged:: 2.0.0
jpayne@69 105 Renamed from ``format_header_param_html5`` and
jpayne@69 106 ``format_header_param``. The old names will be removed in
jpayne@69 107 urllib3 v2.1.0.
jpayne@69 108 """
jpayne@69 109 if isinstance(value, bytes):
jpayne@69 110 value = value.decode("utf-8")
jpayne@69 111
jpayne@69 112 # percent encode \n \r "
jpayne@69 113 value = value.translate({10: "%0A", 13: "%0D", 34: "%22"})
jpayne@69 114 return f'{name}="{value}"'
jpayne@69 115
jpayne@69 116
jpayne@69 117 def format_header_param_html5(name: str, value: _TYPE_FIELD_VALUE) -> str:
jpayne@69 118 """
jpayne@69 119 .. deprecated:: 2.0.0
jpayne@69 120 Renamed to :func:`format_multipart_header_param`. Will be
jpayne@69 121 removed in urllib3 v2.1.0.
jpayne@69 122 """
jpayne@69 123 import warnings
jpayne@69 124
jpayne@69 125 warnings.warn(
jpayne@69 126 "'format_header_param_html5' has been renamed to "
jpayne@69 127 "'format_multipart_header_param'. The old name will be "
jpayne@69 128 "removed in urllib3 v2.1.0.",
jpayne@69 129 DeprecationWarning,
jpayne@69 130 stacklevel=2,
jpayne@69 131 )
jpayne@69 132 return format_multipart_header_param(name, value)
jpayne@69 133
jpayne@69 134
jpayne@69 135 def format_header_param(name: str, value: _TYPE_FIELD_VALUE) -> str:
jpayne@69 136 """
jpayne@69 137 .. deprecated:: 2.0.0
jpayne@69 138 Renamed to :func:`format_multipart_header_param`. Will be
jpayne@69 139 removed in urllib3 v2.1.0.
jpayne@69 140 """
jpayne@69 141 import warnings
jpayne@69 142
jpayne@69 143 warnings.warn(
jpayne@69 144 "'format_header_param' has been renamed to "
jpayne@69 145 "'format_multipart_header_param'. The old name will be "
jpayne@69 146 "removed in urllib3 v2.1.0.",
jpayne@69 147 DeprecationWarning,
jpayne@69 148 stacklevel=2,
jpayne@69 149 )
jpayne@69 150 return format_multipart_header_param(name, value)
jpayne@69 151
jpayne@69 152
jpayne@69 153 class RequestField:
jpayne@69 154 """
jpayne@69 155 A data container for request body parameters.
jpayne@69 156
jpayne@69 157 :param name:
jpayne@69 158 The name of this request field. Must be unicode.
jpayne@69 159 :param data:
jpayne@69 160 The data/value body.
jpayne@69 161 :param filename:
jpayne@69 162 An optional filename of the request field. Must be unicode.
jpayne@69 163 :param headers:
jpayne@69 164 An optional dict-like object of headers to initially use for the field.
jpayne@69 165
jpayne@69 166 .. versionchanged:: 2.0.0
jpayne@69 167 The ``header_formatter`` parameter is deprecated and will
jpayne@69 168 be removed in urllib3 v2.1.0.
jpayne@69 169 """
jpayne@69 170
jpayne@69 171 def __init__(
jpayne@69 172 self,
jpayne@69 173 name: str,
jpayne@69 174 data: _TYPE_FIELD_VALUE,
jpayne@69 175 filename: str | None = None,
jpayne@69 176 headers: typing.Mapping[str, str] | None = None,
jpayne@69 177 header_formatter: typing.Callable[[str, _TYPE_FIELD_VALUE], str] | None = None,
jpayne@69 178 ):
jpayne@69 179 self._name = name
jpayne@69 180 self._filename = filename
jpayne@69 181 self.data = data
jpayne@69 182 self.headers: dict[str, str | None] = {}
jpayne@69 183 if headers:
jpayne@69 184 self.headers = dict(headers)
jpayne@69 185
jpayne@69 186 if header_formatter is not None:
jpayne@69 187 import warnings
jpayne@69 188
jpayne@69 189 warnings.warn(
jpayne@69 190 "The 'header_formatter' parameter is deprecated and "
jpayne@69 191 "will be removed in urllib3 v2.1.0.",
jpayne@69 192 DeprecationWarning,
jpayne@69 193 stacklevel=2,
jpayne@69 194 )
jpayne@69 195 self.header_formatter = header_formatter
jpayne@69 196 else:
jpayne@69 197 self.header_formatter = format_multipart_header_param
jpayne@69 198
jpayne@69 199 @classmethod
jpayne@69 200 def from_tuples(
jpayne@69 201 cls,
jpayne@69 202 fieldname: str,
jpayne@69 203 value: _TYPE_FIELD_VALUE_TUPLE,
jpayne@69 204 header_formatter: typing.Callable[[str, _TYPE_FIELD_VALUE], str] | None = None,
jpayne@69 205 ) -> RequestField:
jpayne@69 206 """
jpayne@69 207 A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters.
jpayne@69 208
jpayne@69 209 Supports constructing :class:`~urllib3.fields.RequestField` from
jpayne@69 210 parameter of key/value strings AND key/filetuple. A filetuple is a
jpayne@69 211 (filename, data, MIME type) tuple where the MIME type is optional.
jpayne@69 212 For example::
jpayne@69 213
jpayne@69 214 'foo': 'bar',
jpayne@69 215 'fakefile': ('foofile.txt', 'contents of foofile'),
jpayne@69 216 'realfile': ('barfile.txt', open('realfile').read()),
jpayne@69 217 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
jpayne@69 218 'nonamefile': 'contents of nonamefile field',
jpayne@69 219
jpayne@69 220 Field names and filenames must be unicode.
jpayne@69 221 """
jpayne@69 222 filename: str | None
jpayne@69 223 content_type: str | None
jpayne@69 224 data: _TYPE_FIELD_VALUE
jpayne@69 225
jpayne@69 226 if isinstance(value, tuple):
jpayne@69 227 if len(value) == 3:
jpayne@69 228 filename, data, content_type = value
jpayne@69 229 else:
jpayne@69 230 filename, data = value
jpayne@69 231 content_type = guess_content_type(filename)
jpayne@69 232 else:
jpayne@69 233 filename = None
jpayne@69 234 content_type = None
jpayne@69 235 data = value
jpayne@69 236
jpayne@69 237 request_param = cls(
jpayne@69 238 fieldname, data, filename=filename, header_formatter=header_formatter
jpayne@69 239 )
jpayne@69 240 request_param.make_multipart(content_type=content_type)
jpayne@69 241
jpayne@69 242 return request_param
jpayne@69 243
jpayne@69 244 def _render_part(self, name: str, value: _TYPE_FIELD_VALUE) -> str:
jpayne@69 245 """
jpayne@69 246 Override this method to change how each multipart header
jpayne@69 247 parameter is formatted. By default, this calls
jpayne@69 248 :func:`format_multipart_header_param`.
jpayne@69 249
jpayne@69 250 :param name:
jpayne@69 251 The name of the parameter, an ASCII-only ``str``.
jpayne@69 252 :param value:
jpayne@69 253 The value of the parameter, a ``str`` or UTF-8 encoded
jpayne@69 254 ``bytes``.
jpayne@69 255
jpayne@69 256 :meta public:
jpayne@69 257 """
jpayne@69 258 return self.header_formatter(name, value)
jpayne@69 259
jpayne@69 260 def _render_parts(
jpayne@69 261 self,
jpayne@69 262 header_parts: (
jpayne@69 263 dict[str, _TYPE_FIELD_VALUE | None]
jpayne@69 264 | typing.Sequence[tuple[str, _TYPE_FIELD_VALUE | None]]
jpayne@69 265 ),
jpayne@69 266 ) -> str:
jpayne@69 267 """
jpayne@69 268 Helper function to format and quote a single header.
jpayne@69 269
jpayne@69 270 Useful for single headers that are composed of multiple items. E.g.,
jpayne@69 271 'Content-Disposition' fields.
jpayne@69 272
jpayne@69 273 :param header_parts:
jpayne@69 274 A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format
jpayne@69 275 as `k1="v1"; k2="v2"; ...`.
jpayne@69 276 """
jpayne@69 277 iterable: typing.Iterable[tuple[str, _TYPE_FIELD_VALUE | None]]
jpayne@69 278
jpayne@69 279 parts = []
jpayne@69 280 if isinstance(header_parts, dict):
jpayne@69 281 iterable = header_parts.items()
jpayne@69 282 else:
jpayne@69 283 iterable = header_parts
jpayne@69 284
jpayne@69 285 for name, value in iterable:
jpayne@69 286 if value is not None:
jpayne@69 287 parts.append(self._render_part(name, value))
jpayne@69 288
jpayne@69 289 return "; ".join(parts)
jpayne@69 290
jpayne@69 291 def render_headers(self) -> str:
jpayne@69 292 """
jpayne@69 293 Renders the headers for this request field.
jpayne@69 294 """
jpayne@69 295 lines = []
jpayne@69 296
jpayne@69 297 sort_keys = ["Content-Disposition", "Content-Type", "Content-Location"]
jpayne@69 298 for sort_key in sort_keys:
jpayne@69 299 if self.headers.get(sort_key, False):
jpayne@69 300 lines.append(f"{sort_key}: {self.headers[sort_key]}")
jpayne@69 301
jpayne@69 302 for header_name, header_value in self.headers.items():
jpayne@69 303 if header_name not in sort_keys:
jpayne@69 304 if header_value:
jpayne@69 305 lines.append(f"{header_name}: {header_value}")
jpayne@69 306
jpayne@69 307 lines.append("\r\n")
jpayne@69 308 return "\r\n".join(lines)
jpayne@69 309
jpayne@69 310 def make_multipart(
jpayne@69 311 self,
jpayne@69 312 content_disposition: str | None = None,
jpayne@69 313 content_type: str | None = None,
jpayne@69 314 content_location: str | None = None,
jpayne@69 315 ) -> None:
jpayne@69 316 """
jpayne@69 317 Makes this request field into a multipart request field.
jpayne@69 318
jpayne@69 319 This method overrides "Content-Disposition", "Content-Type" and
jpayne@69 320 "Content-Location" headers to the request parameter.
jpayne@69 321
jpayne@69 322 :param content_disposition:
jpayne@69 323 The 'Content-Disposition' of the request body. Defaults to 'form-data'
jpayne@69 324 :param content_type:
jpayne@69 325 The 'Content-Type' of the request body.
jpayne@69 326 :param content_location:
jpayne@69 327 The 'Content-Location' of the request body.
jpayne@69 328
jpayne@69 329 """
jpayne@69 330 content_disposition = (content_disposition or "form-data") + "; ".join(
jpayne@69 331 [
jpayne@69 332 "",
jpayne@69 333 self._render_parts(
jpayne@69 334 (("name", self._name), ("filename", self._filename))
jpayne@69 335 ),
jpayne@69 336 ]
jpayne@69 337 )
jpayne@69 338
jpayne@69 339 self.headers["Content-Disposition"] = content_disposition
jpayne@69 340 self.headers["Content-Type"] = content_type
jpayne@69 341 self.headers["Content-Location"] = content_location