Parse a header that consists of a value with ``key=value`` parameters separated by semicolons ``;``. For example, the ``Content-Type`` header. .. code-block:: python parse_options_header("text/html; charset=UTF-8") ('text/html', {'charset': 'UTF-8'}) parse_options_
(value: str | None)
| 453 | |
| 454 | |
| 455 | def parse_options_header(value: str | None) -> tuple[str, dict[str, str]]: |
| 456 | """Parse a header that consists of a value with ``key=value`` parameters separated |
| 457 | by semicolons ``;``. For example, the ``Content-Type`` header. |
| 458 | |
| 459 | .. code-block:: python |
| 460 | |
| 461 | parse_options_header("text/html; charset=UTF-8") |
| 462 | ('text/html', {'charset': 'UTF-8'}) |
| 463 | |
| 464 | parse_options_header("") |
| 465 | ("", {}) |
| 466 | |
| 467 | This is the reverse of :func:`dump_options_header`. |
| 468 | |
| 469 | This parses valid parameter parts as described in |
| 470 | `RFC 9110 <https://httpwg.org/specs/rfc9110.html#parameter>`__. Invalid parts are |
| 471 | skipped. |
| 472 | |
| 473 | This handles continuations and charsets as described in |
| 474 | `RFC 2231 <https://www.rfc-editor.org/rfc/rfc2231#section-3>`__, although not as |
| 475 | strictly as the RFC. Only ASCII, UTF-8, and ISO-8859-1 charsets are accepted, |
| 476 | otherwise the value remains quoted. |
| 477 | |
| 478 | Clients may not be consistent in how they handle a quote character within a quoted |
| 479 | value. The `HTML Standard <https://html.spec.whatwg.org/#multipart-form-data>`__ |
| 480 | replaces it with ``%22`` in multipart form data. |
| 481 | `RFC 9110 <https://httpwg.org/specs/rfc9110.html#quoted.strings>`__ uses backslash |
| 482 | escapes in HTTP headers. Both are decoded to the ``"`` character. |
| 483 | |
| 484 | Clients may not be consistent in how they handle non-ASCII characters. HTML |
| 485 | documents must declare ``<meta charset=UTF-8>``, otherwise browsers may replace with |
| 486 | HTML character references, which can be decoded using :func:`html.unescape`. |
| 487 | |
| 488 | :param value: The header value to parse. |
| 489 | :return: ``(value, options)``, where ``options`` is a dict |
| 490 | |
| 491 | .. versionchanged:: 2.3 |
| 492 | Invalid parts, such as keys with no value, quoted keys, and incorrectly quoted |
| 493 | values, are discarded instead of treating as ``None``. |
| 494 | |
| 495 | .. versionchanged:: 2.3 |
| 496 | Only ASCII, UTF-8, and ISO-8859-1 are accepted for charset values. |
| 497 | |
| 498 | .. versionchanged:: 2.3 |
| 499 | Escaped quotes in quoted values, like ``%22`` and ``\\"``, are handled. |
| 500 | |
| 501 | .. versionchanged:: 2.2 |
| 502 | Option names are always converted to lowercase. |
| 503 | |
| 504 | .. versionchanged:: 2.2 |
| 505 | The ``multiple`` parameter was removed. |
| 506 | |
| 507 | .. versionchanged:: 0.15 |
| 508 | :rfc:`2231` parameter continuations are handled. |
| 509 | |
| 510 | .. versionadded:: 0.5 |
| 511 | """ |
| 512 | if value is None: |