Parse a list header using :func:`parse_list_header`, then parse each item as a ``key=value`` pair. .. code-block:: python parse_dict_header('a=b, c="d, e", f') {"a": "b", "c": "d, e", "f": None} This is the reverse of :func:`dump_header`. If a key does not have a
(value: str)
| 362 | |
| 363 | |
| 364 | def parse_dict_header(value: str) -> dict[str, str | None]: |
| 365 | class="st">"""Parse a list header using :func:`parse_list_header`, then parse each item as a |
| 366 | ``key=value`` pair. |
| 367 | |
| 368 | .. code-block:: python |
| 369 | |
| 370 | parse_dict_header(&class="cm">#x27;a=b, c=class="st">"d, e", f') |
| 371 | {class="st">"a": class="st">"b", class="st">"c": class="st">"d, e", class="st">"f": None} |
| 372 | |
| 373 | This is the reverse of :func:`dump_header`. |
| 374 | |
| 375 | If a key does not have a value, it is ``None``. |
| 376 | |
| 377 | This handles charsets for values as described in |
| 378 | `RFC 2231 <https://www.rfc-editor.org/rfc/rfc2231class="cm">#section-3>`__. Only ASCII, UTF-8, |
| 379 | and ISO-8859-1 charsets are accepted, otherwise the value remains quoted. |
| 380 | |
| 381 | :param value: The header value to parse. |
| 382 | |
| 383 | .. versionchanged:: 3.2 |
| 384 | An empty dict is returned if the value contains an unclosed quoted |
| 385 | string. |
| 386 | |
| 387 | .. versionchanged:: 3.0 |
| 388 | Passing bytes is not supported. |
| 389 | |
| 390 | .. versionchanged:: 3.0 |
| 391 | The ``cls`` argument is removed. |
| 392 | |
| 393 | .. versionchanged:: 2.3 |
| 394 | Added support for ``key*=charset&class="cm">#x27;'value`` encoded items. |
| 395 | |
| 396 | .. versionchanged:: 0.9 |
| 397 | The ``cls`` argument was added. |
| 398 | class="st">""" |
| 399 | result: dict[str, str | None] = {} |
| 400 | |
| 401 | for item in parse_list_header(value): |
| 402 | key, has_value, value = item.partition(class="st">"=") |
| 403 | key = key.strip() |
| 404 | |
| 405 | if not key: |
| 406 | class="cm"># =value is not valid |
| 407 | continue |
| 408 | |
| 409 | if not has_value: |
| 410 | result[key] = None |
| 411 | continue |
| 412 | |
| 413 | value = value.strip() |
| 414 | encoding: str | None = None |
| 415 | |
| 416 | if key[-1] == class="st">"*": |
| 417 | class="cm"># key*=charsetclass="st">''value becomes key=value, where value is percent encoded |
| 418 | class="cm"># adapted from parse_options_header, without the continuation handling |
| 419 | key = key[:-1] |
| 420 | match = _charset_value_re.match(value) |
| 421 |
no test coverage detected