MCPcopy
hub / github.com/pallets/werkzeug / Headers

Class Headers

src/werkzeug/datastructures/headers.py:20–583  ·  view source on GitHub ↗

An object that stores some headers. It has a dict-like interface, but is ordered, can store the same key multiple times, and iterating yields ``(key, value)`` pairs instead of only keys. This data structure is useful if you want a nicer way to handle WSGI headers which are stored as

Source from the content-addressed store, hash-verified

18
19
20class Headers:
21 """An object that stores some headers. It has a dict-like interface,
22 but is ordered, can store the same key multiple times, and iterating
23 yields ``(key, value)`` pairs instead of only keys.
24
25 This data structure is useful if you want a nicer way to handle WSGI
26 headers which are stored as tuples in a list.
27
28 From Werkzeug 0.3 onwards, the :exc:`KeyError` raised by this class is
29 also a subclass of the :class:`~exceptions.BadRequest` HTTP exception
30 and will render a page for a ``400 BAD REQUEST`` if caught in a
31 catch-all for HTTP exceptions.
32
33 Headers is mostly compatible with the Python :class:`wsgiref.headers.Headers`
34 class, with the exception of `__getitem__`. :mod:`wsgiref` will return
35 `None` for ``headers['missing']``, whereas :class:`Headers` will raise
36 a :class:`KeyError`.
37
38 To create a new ``Headers`` object, pass it a list, dict, or
39 other ``Headers`` object with default values. These values are
40 validated the same way values added later are.
41
42 :param defaults: The list of default values for the :class:`Headers`.
43
44 .. versionchanged:: 3.1
45 Implement ``|`` and ``|=`` operators.
46
47 .. versionchanged:: 2.1.0
48 Default values are validated the same as values added later.
49
50 .. versionchanged:: 0.9
51 This data structure now stores unicode values similar to how the
52 multi dicts do it. The main difference is that bytes can be set as
53 well which will automatically be latin1 decoded.
54
55 .. versionchanged:: 0.9
56 The :meth:`linked` function was removed without replacement as it
57 was an API that does not support the changes to the encoding model.
58 """
59
60 def __init__(
61 self,
62 defaults: (
63 Headers
64 | MultiDict[str, t.Any]
65 | cabc.Mapping[str, t.Any | list[t.Any] | tuple[t.Any, ...] | set[t.Any]]
66 | cabc.Iterable[tuple[str, t.Any]]
67 | None
68 ) = None,
69 ) -> None:
70 self._list: list[tuple[str, str]] = []
71
72 if defaults is not None:
73 self.extend(defaults)
74
75 @t.overload
76 def __getitem__(self, key: str) -> str: ...
77 @t.overload

Calls

no outgoing calls