annotate urllib3/fields.py @ 7:5eb2d5e3bf22

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