wsgi.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. from __future__ import annotations
  2. import io
  3. import typing as t
  4. from functools import partial
  5. from functools import update_wrapper
  6. from .exceptions import ClientDisconnected
  7. from .exceptions import RequestEntityTooLarge
  8. from .sansio import utils as _sansio_utils
  9. from .sansio.utils import host_is_trusted # noqa: F401 # Imported as part of API
  10. if t.TYPE_CHECKING:
  11. from _typeshed.wsgi import WSGIApplication
  12. from _typeshed.wsgi import WSGIEnvironment
  13. def responder(f: t.Callable[..., WSGIApplication]) -> WSGIApplication:
  14. """Marks a function as responder. Decorate a function with it and it
  15. will automatically call the return value as WSGI application.
  16. Example::
  17. @responder
  18. def application(environ, start_response):
  19. return Response('Hello World!')
  20. """
  21. return update_wrapper(lambda *a: f(*a)(*a[-2:]), f)
  22. def get_current_url(
  23. environ: WSGIEnvironment,
  24. root_only: bool = False,
  25. strip_querystring: bool = False,
  26. host_only: bool = False,
  27. trusted_hosts: t.Collection[str] | None = None,
  28. ) -> str:
  29. """Recreate the URL for a request from the parts in a WSGI
  30. environment.
  31. The URL is an IRI, not a URI, so it may contain Unicode characters.
  32. Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII.
  33. :param environ: The WSGI environment to get the URL parts from.
  34. :param root_only: Only build the root path, don't include the
  35. remaining path or query string.
  36. :param strip_querystring: Don't include the query string.
  37. :param host_only: Only build the scheme and host.
  38. :param trusted_hosts: A list of trusted host names to validate the
  39. host against.
  40. """
  41. parts = {
  42. "scheme": environ["wsgi.url_scheme"],
  43. "host": get_host(environ, trusted_hosts),
  44. }
  45. if not host_only:
  46. parts["root_path"] = environ.get("SCRIPT_NAME", "")
  47. if not root_only:
  48. parts["path"] = environ.get("PATH_INFO", "")
  49. if not strip_querystring:
  50. parts["query_string"] = environ.get("QUERY_STRING", "").encode("latin1")
  51. return _sansio_utils.get_current_url(**parts)
  52. def _get_server(
  53. environ: WSGIEnvironment,
  54. ) -> tuple[str, int | None] | None:
  55. name = environ.get("SERVER_NAME")
  56. if name is None:
  57. return None
  58. try:
  59. port: int | None = int(environ.get("SERVER_PORT", None)) # type: ignore[arg-type]
  60. except (TypeError, ValueError):
  61. # unix socket
  62. port = None
  63. return name, port
  64. def get_host(
  65. environ: WSGIEnvironment, trusted_hosts: t.Collection[str] | None = None
  66. ) -> str:
  67. """Get and validate a request's ``host:port`` based on the values in the
  68. given WSGI environ.
  69. The ``Host`` header sent by the client is preferred. Otherwise, the server's
  70. configured address is used. If the server address is a Unix socket, it is
  71. ignored. The port is omitted if it matches the standard HTTP or HTTPS ports.
  72. The value is passed through :func:`host_is_trusted`. The host must be made
  73. up of valid characters, but this does not check validity beyond that. If a
  74. list of trusted domains is given, the domain must match one.
  75. :param environ: The WSGI environ.
  76. :param trusted_hosts: A list of trusted domains to match. These should
  77. already be IDNA encoded, but will be encoded if needed. The port is
  78. ignored for this check. If a name starts with a dot it will match as a
  79. suffix, accepting all subdomains. If empty or ``None``, all domains are
  80. allowed.
  81. :return: Host, with port if necessary.
  82. :raise .SecurityError: If the host is not trusted.
  83. .. versionchanged:: 3.2
  84. The characters of the host value are validated. The empty string is no
  85. longer allowed if no header value is available.
  86. .. versionchanged:: 3.2
  87. When using the server address, Unix sockets are ignored.
  88. .. versionchanged:: 3.1.3
  89. If ``SERVER_NAME`` is IPv6, it is wrapped in ``[]``.
  90. """
  91. return _sansio_utils.get_host(
  92. environ["wsgi.url_scheme"],
  93. environ.get("HTTP_HOST"),
  94. _get_server(environ),
  95. trusted_hosts,
  96. )
  97. def get_content_length(environ: WSGIEnvironment) -> int | None:
  98. """Return the ``Content-Length`` header value as an int. If the header is not given
  99. or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
  100. a streaming request. If the value is not an integer, or negative, 0 is returned.
  101. :param environ: The WSGI environ to get the content length from.
  102. .. versionadded:: 0.9
  103. """
  104. return _sansio_utils.get_content_length(
  105. http_content_length=environ.get("CONTENT_LENGTH"),
  106. http_transfer_encoding=environ.get("HTTP_TRANSFER_ENCODING"),
  107. )
  108. def get_input_stream(
  109. environ: WSGIEnvironment,
  110. safe_fallback: bool = True,
  111. max_content_length: int | None = None,
  112. ) -> t.IO[bytes]:
  113. """Return the WSGI input stream, wrapped so that it may be read safely without going
  114. past the ``Content-Length`` header value or ``max_content_length``.
  115. If ``Content-Length`` exceeds ``max_content_length``, a
  116. :exc:`RequestEntityTooLarge`` ``413 Content Too Large`` error is raised.
  117. If the WSGI server sets ``environ["wsgi.input_terminated"]``, it indicates that the
  118. server handles terminating the stream, so it is safe to read directly. For example,
  119. a server that knows how to handle chunked requests safely would set this.
  120. If ``max_content_length`` is set, it can be enforced on streams if
  121. ``wsgi.input_terminated`` is set. Otherwise, an empty stream is returned unless the
  122. user explicitly disables this safe fallback.
  123. If the limit is reached before the underlying stream is exhausted (such as a file
  124. that is too large, or an infinite stream), the remaining contents of the stream
  125. cannot be read safely. Depending on how the server handles this, clients may show a
  126. "connection reset" failure instead of seeing the 413 response.
  127. :param environ: The WSGI environ containing the stream.
  128. :param safe_fallback: Return an empty stream when ``Content-Length`` is not set.
  129. Disabling this allows infinite streams, which can be a denial-of-service risk.
  130. :param max_content_length: The maximum length that content-length or streaming
  131. requests may not exceed.
  132. .. versionchanged:: 2.3.2
  133. ``max_content_length`` is only applied to streaming requests if the server sets
  134. ``wsgi.input_terminated``.
  135. .. versionchanged:: 2.3
  136. Check ``max_content_length`` and raise an error if it is exceeded.
  137. .. versionadded:: 0.9
  138. """
  139. stream = t.cast(t.IO[bytes], environ["wsgi.input"])
  140. content_length = get_content_length(environ)
  141. if content_length is not None and max_content_length is not None:
  142. if content_length > max_content_length:
  143. raise RequestEntityTooLarge()
  144. # A WSGI server can set this to indicate that it terminates the input stream. In
  145. # that case the stream is safe without wrapping, or can enforce a max length.
  146. if "wsgi.input_terminated" in environ:
  147. if max_content_length is not None:
  148. # If this is moved above, it can cause the stream to hang if a read attempt
  149. # is made when the client sends no data. For example, the development server
  150. # does not handle buffering except for chunked encoding.
  151. return t.cast(
  152. t.IO[bytes], LimitedStream(stream, max_content_length, is_max=True)
  153. )
  154. return stream
  155. # No limit given, return an empty stream unless the user explicitly allows the
  156. # potentially infinite stream. An infinite stream is dangerous if it's not expected,
  157. # as it can tie up a worker indefinitely.
  158. if content_length is None:
  159. return io.BytesIO() if safe_fallback else stream
  160. return t.cast(t.IO[bytes], LimitedStream(stream, content_length))
  161. def get_path_info(environ: WSGIEnvironment) -> str:
  162. """Return ``PATH_INFO`` from the WSGI environment.
  163. :param environ: WSGI environment to get the path from.
  164. .. versionchanged:: 3.0
  165. The ``charset`` and ``errors`` parameters were removed.
  166. .. versionadded:: 0.9
  167. """
  168. path: bytes = environ.get("PATH_INFO", "").encode("latin1")
  169. return path.decode(errors="replace")
  170. class ClosingIterator:
  171. """The WSGI specification requires that all middlewares and gateways
  172. respect the `close` callback of the iterable returned by the application.
  173. Because it is useful to add another close action to a returned iterable
  174. and adding a custom iterable is a boring task this class can be used for
  175. that::
  176. return ClosingIterator(app(environ, start_response), [cleanup_session,
  177. cleanup_locals])
  178. If there is just one close function it can be passed instead of the list.
  179. A closing iterator is not needed if the application uses response objects
  180. and finishes the processing if the response is started::
  181. try:
  182. return response(environ, start_response)
  183. finally:
  184. cleanup_session()
  185. cleanup_locals()
  186. """
  187. def __init__(
  188. self,
  189. iterable: t.Iterable[bytes],
  190. callbacks: None
  191. | (t.Callable[[], None] | t.Iterable[t.Callable[[], None]]) = None,
  192. ) -> None:
  193. iterator = iter(iterable)
  194. self._next = t.cast(t.Callable[[], bytes], partial(next, iterator))
  195. if callbacks is None:
  196. callbacks = []
  197. elif callable(callbacks):
  198. callbacks = [callbacks]
  199. else:
  200. callbacks = list(callbacks)
  201. iterable_close = getattr(iterable, "close", None)
  202. if iterable_close:
  203. callbacks.insert(0, iterable_close)
  204. self._callbacks = callbacks
  205. def __iter__(self) -> ClosingIterator:
  206. return self
  207. def __next__(self) -> bytes:
  208. return self._next()
  209. def close(self) -> None:
  210. for callback in self._callbacks:
  211. callback()
  212. def wrap_file(
  213. environ: WSGIEnvironment, file: t.IO[bytes], buffer_size: int = 8192
  214. ) -> t.Iterable[bytes]:
  215. """Wraps a file. This uses the WSGI server's file wrapper if available
  216. or otherwise the generic :class:`FileWrapper`.
  217. .. versionadded:: 0.5
  218. If the file wrapper from the WSGI server is used it's important to not
  219. iterate over it from inside the application but to pass it through
  220. unchanged. If you want to pass out a file wrapper inside a response
  221. object you have to set :attr:`Response.direct_passthrough` to `True`.
  222. More information about file wrappers are available in :pep:`333`.
  223. :param file: a :class:`file`-like object with a :meth:`~file.read` method.
  224. :param buffer_size: number of bytes for one iteration.
  225. """
  226. return environ.get("wsgi.file_wrapper", FileWrapper)( # type: ignore
  227. file, buffer_size
  228. )
  229. class FileWrapper:
  230. """This class can be used to convert a :class:`file`-like object into
  231. an iterable. It yields `buffer_size` blocks until the file is fully
  232. read.
  233. You should not use this class directly but rather use the
  234. :func:`wrap_file` function that uses the WSGI server's file wrapper
  235. support if it's available.
  236. .. versionadded:: 0.5
  237. If you're using this object together with a :class:`Response` you have
  238. to use the `direct_passthrough` mode.
  239. :param file: a :class:`file`-like object with a :meth:`~file.read` method.
  240. :param buffer_size: number of bytes for one iteration.
  241. """
  242. def __init__(self, file: t.IO[bytes], buffer_size: int = 8192) -> None:
  243. self.file = file
  244. self.buffer_size = buffer_size
  245. def close(self) -> None:
  246. if hasattr(self.file, "close"):
  247. self.file.close()
  248. def seekable(self) -> bool:
  249. if hasattr(self.file, "seekable"):
  250. return self.file.seekable()
  251. if hasattr(self.file, "seek"):
  252. return True
  253. return False
  254. def seek(self, *args: t.Any) -> None:
  255. if hasattr(self.file, "seek"):
  256. self.file.seek(*args)
  257. def tell(self) -> int | None:
  258. if hasattr(self.file, "tell"):
  259. return self.file.tell()
  260. return None
  261. def __iter__(self) -> FileWrapper:
  262. return self
  263. def __next__(self) -> bytes:
  264. data = self.file.read(self.buffer_size)
  265. if data:
  266. return data
  267. raise StopIteration()
  268. class _RangeWrapper:
  269. # private for now, but should we make it public in the future ?
  270. """This class can be used to convert an iterable object into
  271. an iterable that will only yield a piece of the underlying content.
  272. It yields blocks until the underlying stream range is fully read.
  273. The yielded blocks will have a size that can't exceed the original
  274. iterator defined block size, but that can be smaller.
  275. If you're using this object together with a :class:`Response` you have
  276. to use the `direct_passthrough` mode.
  277. :param iterable: an iterable object with a :meth:`__next__` method.
  278. :param start_byte: byte from which read will start.
  279. :param byte_range: how many bytes to read.
  280. """
  281. def __init__(
  282. self,
  283. iterable: t.Iterable[bytes] | t.IO[bytes],
  284. start_byte: int = 0,
  285. byte_range: int | None = None,
  286. ):
  287. self.iterable = iter(iterable)
  288. self.byte_range = byte_range
  289. self.start_byte = start_byte
  290. self.end_byte = None
  291. if byte_range is not None:
  292. self.end_byte = start_byte + byte_range
  293. self.read_length = 0
  294. self.seekable = hasattr(iterable, "seekable") and iterable.seekable()
  295. self.end_reached = False
  296. def __iter__(self) -> _RangeWrapper:
  297. return self
  298. def _next_chunk(self) -> bytes:
  299. try:
  300. chunk = next(self.iterable)
  301. self.read_length += len(chunk)
  302. return chunk
  303. except StopIteration:
  304. self.end_reached = True
  305. raise
  306. def _first_iteration(self) -> tuple[bytes | None, int]:
  307. chunk = None
  308. if self.seekable:
  309. self.iterable.seek(self.start_byte) # type: ignore
  310. self.read_length = self.iterable.tell() # type: ignore
  311. contextual_read_length = self.read_length
  312. else:
  313. while self.read_length <= self.start_byte:
  314. chunk = self._next_chunk()
  315. if chunk is not None:
  316. chunk = chunk[self.start_byte - self.read_length :]
  317. contextual_read_length = self.start_byte
  318. return chunk, contextual_read_length
  319. def _next(self) -> bytes:
  320. if self.end_reached:
  321. raise StopIteration()
  322. chunk = None
  323. contextual_read_length = self.read_length
  324. if self.read_length == 0:
  325. chunk, contextual_read_length = self._first_iteration()
  326. if chunk is None:
  327. chunk = self._next_chunk()
  328. if self.end_byte is not None and self.read_length >= self.end_byte:
  329. self.end_reached = True
  330. return chunk[: self.end_byte - contextual_read_length]
  331. return chunk
  332. def __next__(self) -> bytes:
  333. chunk = self._next()
  334. if chunk:
  335. return chunk
  336. self.end_reached = True
  337. raise StopIteration()
  338. def close(self) -> None:
  339. if hasattr(self.iterable, "close"):
  340. self.iterable.close()
  341. class LimitedStream(io.RawIOBase):
  342. """Wrap a stream so that it doesn't read more than a given limit. This is used to
  343. limit ``wsgi.input`` to the ``Content-Length`` header value or
  344. :attr:`.Request.max_content_length`.
  345. When attempting to read after the limit has been reached, :meth:`on_exhausted` is
  346. called. When the limit is a maximum, this raises :exc:`.RequestEntityTooLarge`.
  347. If reading from the stream returns zero bytes or raises an error,
  348. :meth:`on_disconnect` is called, which raises :exc:`.ClientDisconnected`. When the
  349. limit is a maximum and zero bytes were read, no error is raised, since it may be the
  350. end of the stream.
  351. If the limit is reached before the underlying stream is exhausted (such as a file
  352. that is too large, or an infinite stream), the remaining contents of the stream
  353. cannot be read safely. Depending on how the server handles this, clients may show a
  354. "connection reset" failure instead of seeing the 413 response.
  355. :param stream: The stream to read from. Must be a readable binary IO object.
  356. :param limit: The limit in bytes to not read past. Should be either the
  357. ``Content-Length`` header value or ``request.max_content_length``.
  358. :param is_max: Whether the given ``limit`` is ``request.max_content_length`` instead
  359. of the ``Content-Length`` header value. This changes how exhausted and
  360. disconnect events are handled.
  361. .. versionchanged:: 2.3
  362. Handle ``max_content_length`` differently than ``Content-Length``.
  363. .. versionchanged:: 2.3
  364. Implements ``io.RawIOBase`` rather than ``io.IOBase``.
  365. """
  366. def __init__(self, stream: t.IO[bytes], limit: int, is_max: bool = False) -> None:
  367. self._stream = stream
  368. self._pos = 0
  369. self.limit = limit
  370. self._limit_is_max = is_max
  371. @property
  372. def is_exhausted(self) -> bool:
  373. """Whether the current stream position has reached the limit."""
  374. return self._pos >= self.limit
  375. def on_exhausted(self) -> None:
  376. """Called when attempting to read after the limit has been reached.
  377. The default behavior is to do nothing, unless the limit is a maximum, in which
  378. case it raises :exc:`.RequestEntityTooLarge`.
  379. .. versionchanged:: 2.3
  380. Raises ``RequestEntityTooLarge`` if the limit is a maximum.
  381. .. versionchanged:: 2.3
  382. Any return value is ignored.
  383. """
  384. if self._limit_is_max:
  385. raise RequestEntityTooLarge()
  386. def on_disconnect(self, error: Exception | None = None) -> None:
  387. """Called when an attempted read receives zero bytes before the limit was
  388. reached. This indicates that the client disconnected before sending the full
  389. request body.
  390. The default behavior is to raise :exc:`.ClientDisconnected`, unless the limit is
  391. a maximum and no error was raised.
  392. .. versionchanged:: 2.3
  393. Added the ``error`` parameter. Do nothing if the limit is a maximum and no
  394. error was raised.
  395. .. versionchanged:: 2.3
  396. Any return value is ignored.
  397. """
  398. if not self._limit_is_max or error is not None:
  399. raise ClientDisconnected()
  400. # If the limit is a maximum, then we may have read zero bytes because the
  401. # streaming body is complete. There's no way to distinguish that from the
  402. # client disconnecting early.
  403. def exhaust(self) -> bytes:
  404. """Exhaust the stream by reading until the limit is reached or the client
  405. disconnects, returning the remaining data.
  406. .. versionchanged:: 2.3
  407. Return the remaining data.
  408. .. versionchanged:: 2.2.3
  409. Handle case where wrapped stream returns fewer bytes than requested.
  410. """
  411. if not self.is_exhausted:
  412. return self.readall()
  413. return b""
  414. def readinto(self, b: bytearray) -> int | None: # type: ignore[override]
  415. size = len(b)
  416. remaining = self.limit - self._pos
  417. if remaining <= 0:
  418. self.on_exhausted()
  419. return 0
  420. if hasattr(self._stream, "readinto"):
  421. # Use stream.readinto if it's available.
  422. if size <= remaining:
  423. # The size fits in the remaining limit, use the buffer directly.
  424. try:
  425. out_size: int | None = self._stream.readinto(b)
  426. except (OSError, ValueError) as e:
  427. self.on_disconnect(error=e)
  428. return 0
  429. else:
  430. # Use a temp buffer with the remaining limit as the size.
  431. temp_b = bytearray(remaining)
  432. try:
  433. out_size = self._stream.readinto(temp_b)
  434. except (OSError, ValueError) as e:
  435. self.on_disconnect(error=e)
  436. return 0
  437. if out_size:
  438. b[:out_size] = temp_b
  439. else:
  440. # WSGI requires that stream.read is available.
  441. try:
  442. data = self._stream.read(min(size, remaining))
  443. except (OSError, ValueError) as e:
  444. self.on_disconnect(error=e)
  445. return 0
  446. out_size = len(data)
  447. b[:out_size] = data
  448. if not out_size:
  449. # Read zero bytes from the stream.
  450. self.on_disconnect()
  451. return 0
  452. self._pos += out_size
  453. return out_size
  454. def readall(self) -> bytes:
  455. if self.is_exhausted:
  456. self.on_exhausted()
  457. return b""
  458. out = bytearray()
  459. # The parent implementation uses "while True", which results in an extra read.
  460. while not self.is_exhausted:
  461. data = self.read(1024 * 64)
  462. # Stream may return empty before a max limit is reached.
  463. if not data:
  464. break
  465. out.extend(data)
  466. return bytes(out)
  467. def tell(self) -> int:
  468. """Return the current stream position.
  469. .. versionadded:: 0.9
  470. """
  471. return self._pos
  472. def readable(self) -> bool:
  473. return True