headers.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. from __future__ import annotations
  2. import re
  3. import typing as t
  4. from .._internal import _missing
  5. from ..exceptions import BadRequestKeyError
  6. from .mixins import ImmutableHeadersMixin
  7. from .structures import iter_multi_items
  8. from .structures import MultiDict
  9. class Headers:
  10. """An object that stores some headers. It has a dict-like interface,
  11. but is ordered, can store the same key multiple times, and iterating
  12. yields ``(key, value)`` pairs instead of only keys.
  13. This data structure is useful if you want a nicer way to handle WSGI
  14. headers which are stored as tuples in a list.
  15. From Werkzeug 0.3 onwards, the :exc:`KeyError` raised by this class is
  16. also a subclass of the :class:`~exceptions.BadRequest` HTTP exception
  17. and will render a page for a ``400 BAD REQUEST`` if caught in a
  18. catch-all for HTTP exceptions.
  19. Headers is mostly compatible with the Python :class:`wsgiref.headers.Headers`
  20. class, with the exception of `__getitem__`. :mod:`wsgiref` will return
  21. `None` for ``headers['missing']``, whereas :class:`Headers` will raise
  22. a :class:`KeyError`.
  23. To create a new ``Headers`` object, pass it a list, dict, or
  24. other ``Headers`` object with default values. These values are
  25. validated the same way values added later are.
  26. :param defaults: The list of default values for the :class:`Headers`.
  27. .. versionchanged:: 2.1.0
  28. Default values are validated the same as values added later.
  29. .. versionchanged:: 0.9
  30. This data structure now stores unicode values similar to how the
  31. multi dicts do it. The main difference is that bytes can be set as
  32. well which will automatically be latin1 decoded.
  33. .. versionchanged:: 0.9
  34. The :meth:`linked` function was removed without replacement as it
  35. was an API that does not support the changes to the encoding model.
  36. """
  37. def __init__(self, defaults=None):
  38. self._list = []
  39. if defaults is not None:
  40. self.extend(defaults)
  41. def __getitem__(self, key, _get_mode=False):
  42. if not _get_mode:
  43. if isinstance(key, int):
  44. return self._list[key]
  45. elif isinstance(key, slice):
  46. return self.__class__(self._list[key])
  47. if not isinstance(key, str):
  48. raise BadRequestKeyError(key)
  49. ikey = key.lower()
  50. for k, v in self._list:
  51. if k.lower() == ikey:
  52. return v
  53. # micro optimization: if we are in get mode we will catch that
  54. # exception one stack level down so we can raise a standard
  55. # key error instead of our special one.
  56. if _get_mode:
  57. raise KeyError()
  58. raise BadRequestKeyError(key)
  59. def __eq__(self, other):
  60. def lowered(item):
  61. return (item[0].lower(),) + item[1:]
  62. return other.__class__ is self.__class__ and set(
  63. map(lowered, other._list)
  64. ) == set(map(lowered, self._list))
  65. __hash__ = None
  66. def get(self, key, default=None, type=None):
  67. """Return the default value if the requested data doesn't exist.
  68. If `type` is provided and is a callable it should convert the value,
  69. return it or raise a :exc:`ValueError` if that is not possible. In
  70. this case the function will return the default as if the value was not
  71. found:
  72. >>> d = Headers([('Content-Length', '42')])
  73. >>> d.get('Content-Length', type=int)
  74. 42
  75. :param key: The key to be looked up.
  76. :param default: The default value to be returned if the key can't
  77. be looked up. If not further specified `None` is
  78. returned.
  79. :param type: A callable that is used to cast the value in the
  80. :class:`Headers`. If a :exc:`ValueError` is raised
  81. by this callable the default value is returned.
  82. .. versionchanged:: 3.0
  83. The ``as_bytes`` parameter was removed.
  84. .. versionchanged:: 0.9
  85. The ``as_bytes`` parameter was added.
  86. """
  87. try:
  88. rv = self.__getitem__(key, _get_mode=True)
  89. except KeyError:
  90. return default
  91. if type is None:
  92. return rv
  93. try:
  94. return type(rv)
  95. except ValueError:
  96. return default
  97. def getlist(self, key, type=None):
  98. """Return the list of items for a given key. If that key is not in the
  99. :class:`Headers`, the return value will be an empty list. Just like
  100. :meth:`get`, :meth:`getlist` accepts a `type` parameter. All items will
  101. be converted with the callable defined there.
  102. :param key: The key to be looked up.
  103. :param type: A callable that is used to cast the value in the
  104. :class:`Headers`. If a :exc:`ValueError` is raised
  105. by this callable the value will be removed from the list.
  106. :return: a :class:`list` of all the values for the key.
  107. .. versionchanged:: 3.0
  108. The ``as_bytes`` parameter was removed.
  109. .. versionchanged:: 0.9
  110. The ``as_bytes`` parameter was added.
  111. """
  112. ikey = key.lower()
  113. result = []
  114. for k, v in self:
  115. if k.lower() == ikey:
  116. if type is not None:
  117. try:
  118. v = type(v)
  119. except ValueError:
  120. continue
  121. result.append(v)
  122. return result
  123. def get_all(self, name):
  124. """Return a list of all the values for the named field.
  125. This method is compatible with the :mod:`wsgiref`
  126. :meth:`~wsgiref.headers.Headers.get_all` method.
  127. """
  128. return self.getlist(name)
  129. def items(self, lower=False):
  130. for key, value in self:
  131. if lower:
  132. key = key.lower()
  133. yield key, value
  134. def keys(self, lower=False):
  135. for key, _ in self.items(lower):
  136. yield key
  137. def values(self):
  138. for _, value in self.items():
  139. yield value
  140. def extend(self, *args, **kwargs):
  141. """Extend headers in this object with items from another object
  142. containing header items as well as keyword arguments.
  143. To replace existing keys instead of extending, use
  144. :meth:`update` instead.
  145. If provided, the first argument can be another :class:`Headers`
  146. object, a :class:`MultiDict`, :class:`dict`, or iterable of
  147. pairs.
  148. .. versionchanged:: 1.0
  149. Support :class:`MultiDict`. Allow passing ``kwargs``.
  150. """
  151. if len(args) > 1:
  152. raise TypeError(f"update expected at most 1 arguments, got {len(args)}")
  153. if args:
  154. for key, value in iter_multi_items(args[0]):
  155. self.add(key, value)
  156. for key, value in iter_multi_items(kwargs):
  157. self.add(key, value)
  158. def __delitem__(self, key, _index_operation=True):
  159. if _index_operation and isinstance(key, (int, slice)):
  160. del self._list[key]
  161. return
  162. key = key.lower()
  163. new = []
  164. for k, v in self._list:
  165. if k.lower() != key:
  166. new.append((k, v))
  167. self._list[:] = new
  168. def remove(self, key):
  169. """Remove a key.
  170. :param key: The key to be removed.
  171. """
  172. return self.__delitem__(key, _index_operation=False)
  173. def pop(self, key=None, default=_missing):
  174. """Removes and returns a key or index.
  175. :param key: The key to be popped. If this is an integer the item at
  176. that position is removed, if it's a string the value for
  177. that key is. If the key is omitted or `None` the last
  178. item is removed.
  179. :return: an item.
  180. """
  181. if key is None:
  182. return self._list.pop()
  183. if isinstance(key, int):
  184. return self._list.pop(key)
  185. try:
  186. rv = self[key]
  187. self.remove(key)
  188. except KeyError:
  189. if default is not _missing:
  190. return default
  191. raise
  192. return rv
  193. def popitem(self):
  194. """Removes a key or index and returns a (key, value) item."""
  195. return self.pop()
  196. def __contains__(self, key):
  197. """Check if a key is present."""
  198. try:
  199. self.__getitem__(key, _get_mode=True)
  200. except KeyError:
  201. return False
  202. return True
  203. def __iter__(self):
  204. """Yield ``(key, value)`` tuples."""
  205. return iter(self._list)
  206. def __len__(self):
  207. return len(self._list)
  208. def add(self, _key, _value, **kw):
  209. """Add a new header tuple to the list.
  210. Keyword arguments can specify additional parameters for the header
  211. value, with underscores converted to dashes::
  212. >>> d = Headers()
  213. >>> d.add('Content-Type', 'text/plain')
  214. >>> d.add('Content-Disposition', 'attachment', filename='foo.png')
  215. The keyword argument dumping uses :func:`dump_options_header`
  216. behind the scenes.
  217. .. versionadded:: 0.4.1
  218. keyword arguments were added for :mod:`wsgiref` compatibility.
  219. """
  220. if kw:
  221. _value = _options_header_vkw(_value, kw)
  222. _value = _str_header_value(_value)
  223. self._list.append((_key, _value))
  224. def add_header(self, _key, _value, **_kw):
  225. """Add a new header tuple to the list.
  226. An alias for :meth:`add` for compatibility with the :mod:`wsgiref`
  227. :meth:`~wsgiref.headers.Headers.add_header` method.
  228. """
  229. self.add(_key, _value, **_kw)
  230. def clear(self):
  231. """Clears all headers."""
  232. del self._list[:]
  233. def set(self, _key, _value, **kw):
  234. """Remove all header tuples for `key` and add a new one. The newly
  235. added key either appears at the end of the list if there was no
  236. entry or replaces the first one.
  237. Keyword arguments can specify additional parameters for the header
  238. value, with underscores converted to dashes. See :meth:`add` for
  239. more information.
  240. .. versionchanged:: 0.6.1
  241. :meth:`set` now accepts the same arguments as :meth:`add`.
  242. :param key: The key to be inserted.
  243. :param value: The value to be inserted.
  244. """
  245. if kw:
  246. _value = _options_header_vkw(_value, kw)
  247. _value = _str_header_value(_value)
  248. if not self._list:
  249. self._list.append((_key, _value))
  250. return
  251. listiter = iter(self._list)
  252. ikey = _key.lower()
  253. for idx, (old_key, _old_value) in enumerate(listiter):
  254. if old_key.lower() == ikey:
  255. # replace first occurrence
  256. self._list[idx] = (_key, _value)
  257. break
  258. else:
  259. self._list.append((_key, _value))
  260. return
  261. self._list[idx + 1 :] = [t for t in listiter if t[0].lower() != ikey]
  262. def setlist(self, key, values):
  263. """Remove any existing values for a header and add new ones.
  264. :param key: The header key to set.
  265. :param values: An iterable of values to set for the key.
  266. .. versionadded:: 1.0
  267. """
  268. if values:
  269. values_iter = iter(values)
  270. self.set(key, next(values_iter))
  271. for value in values_iter:
  272. self.add(key, value)
  273. else:
  274. self.remove(key)
  275. def setdefault(self, key, default):
  276. """Return the first value for the key if it is in the headers,
  277. otherwise set the header to the value given by ``default`` and
  278. return that.
  279. :param key: The header key to get.
  280. :param default: The value to set for the key if it is not in the
  281. headers.
  282. """
  283. if key in self:
  284. return self[key]
  285. self.set(key, default)
  286. return default
  287. def setlistdefault(self, key, default):
  288. """Return the list of values for the key if it is in the
  289. headers, otherwise set the header to the list of values given
  290. by ``default`` and return that.
  291. Unlike :meth:`MultiDict.setlistdefault`, modifying the returned
  292. list will not affect the headers.
  293. :param key: The header key to get.
  294. :param default: An iterable of values to set for the key if it
  295. is not in the headers.
  296. .. versionadded:: 1.0
  297. """
  298. if key not in self:
  299. self.setlist(key, default)
  300. return self.getlist(key)
  301. def __setitem__(self, key, value):
  302. """Like :meth:`set` but also supports index/slice based setting."""
  303. if isinstance(key, (slice, int)):
  304. if isinstance(key, int):
  305. value = [value]
  306. value = [(k, _str_header_value(v)) for (k, v) in value]
  307. if isinstance(key, int):
  308. self._list[key] = value[0]
  309. else:
  310. self._list[key] = value
  311. else:
  312. self.set(key, value)
  313. def update(self, *args, **kwargs):
  314. """Replace headers in this object with items from another
  315. headers object and keyword arguments.
  316. To extend existing keys instead of replacing, use :meth:`extend`
  317. instead.
  318. If provided, the first argument can be another :class:`Headers`
  319. object, a :class:`MultiDict`, :class:`dict`, or iterable of
  320. pairs.
  321. .. versionadded:: 1.0
  322. """
  323. if len(args) > 1:
  324. raise TypeError(f"update expected at most 1 arguments, got {len(args)}")
  325. if args:
  326. mapping = args[0]
  327. if isinstance(mapping, (Headers, MultiDict)):
  328. for key in mapping.keys():
  329. self.setlist(key, mapping.getlist(key))
  330. elif isinstance(mapping, dict):
  331. for key, value in mapping.items():
  332. if isinstance(value, (list, tuple)):
  333. self.setlist(key, value)
  334. else:
  335. self.set(key, value)
  336. else:
  337. for key, value in mapping:
  338. self.set(key, value)
  339. for key, value in kwargs.items():
  340. if isinstance(value, (list, tuple)):
  341. self.setlist(key, value)
  342. else:
  343. self.set(key, value)
  344. def to_wsgi_list(self):
  345. """Convert the headers into a list suitable for WSGI.
  346. :return: list
  347. """
  348. return list(self)
  349. def copy(self):
  350. return self.__class__(self._list)
  351. def __copy__(self):
  352. return self.copy()
  353. def __str__(self):
  354. """Returns formatted headers suitable for HTTP transmission."""
  355. strs = []
  356. for key, value in self.to_wsgi_list():
  357. strs.append(f"{key}: {value}")
  358. strs.append("\r\n")
  359. return "\r\n".join(strs)
  360. def __repr__(self):
  361. return f"{type(self).__name__}({list(self)!r})"
  362. def _options_header_vkw(value: str, kw: dict[str, t.Any]):
  363. return http.dump_options_header(
  364. value, {k.replace("_", "-"): v for k, v in kw.items()}
  365. )
  366. _newline_re = re.compile(r"[\r\n]")
  367. def _str_header_value(value: t.Any) -> str:
  368. if not isinstance(value, str):
  369. value = str(value)
  370. if _newline_re.search(value) is not None:
  371. raise ValueError("Header values must not contain newline characters.")
  372. return value
  373. class EnvironHeaders(ImmutableHeadersMixin, Headers):
  374. """Read only version of the headers from a WSGI environment. This
  375. provides the same interface as `Headers` and is constructed from
  376. a WSGI environment.
  377. From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
  378. subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
  379. render a page for a ``400 BAD REQUEST`` if caught in a catch-all for
  380. HTTP exceptions.
  381. """
  382. def __init__(self, environ):
  383. self.environ = environ
  384. def __eq__(self, other):
  385. return self.environ is other.environ
  386. __hash__ = None
  387. def __getitem__(self, key, _get_mode=False):
  388. # _get_mode is a no-op for this class as there is no index but
  389. # used because get() calls it.
  390. if not isinstance(key, str):
  391. raise KeyError(key)
  392. key = key.upper().replace("-", "_")
  393. if key in {"CONTENT_TYPE", "CONTENT_LENGTH"}:
  394. return self.environ[key]
  395. return self.environ[f"HTTP_{key}"]
  396. def __len__(self):
  397. # the iter is necessary because otherwise list calls our
  398. # len which would call list again and so forth.
  399. return len(list(iter(self)))
  400. def __iter__(self):
  401. for key, value in self.environ.items():
  402. if key.startswith("HTTP_") and key not in {
  403. "HTTP_CONTENT_TYPE",
  404. "HTTP_CONTENT_LENGTH",
  405. }:
  406. yield key[5:].replace("_", "-").title(), value
  407. elif key in {"CONTENT_TYPE", "CONTENT_LENGTH"} and value:
  408. yield key.replace("_", "-").title(), value
  409. def copy(self):
  410. raise TypeError(f"cannot create {type(self).__name__!r} copies")
  411. # circular dependencies
  412. from .. import http