request.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. import functools
  2. import json
  3. import typing
  4. import typing as t
  5. import warnings
  6. from io import BytesIO
  7. from .._internal import _wsgi_decoding_dance
  8. from ..datastructures import CombinedMultiDict
  9. from ..datastructures import EnvironHeaders
  10. from ..datastructures import FileStorage
  11. from ..datastructures import ImmutableMultiDict
  12. from ..datastructures import iter_multi_items
  13. from ..datastructures import MultiDict
  14. from ..formparser import default_stream_factory
  15. from ..formparser import FormDataParser
  16. from ..sansio.request import Request as _SansIORequest
  17. from ..utils import cached_property
  18. from ..utils import environ_property
  19. from ..wsgi import _get_server
  20. from ..wsgi import get_input_stream
  21. from werkzeug.exceptions import BadRequest
  22. if t.TYPE_CHECKING:
  23. import typing_extensions as te
  24. from _typeshed.wsgi import WSGIApplication
  25. from _typeshed.wsgi import WSGIEnvironment
  26. class Request(_SansIORequest):
  27. """Represents an incoming WSGI HTTP request, with headers and body
  28. taken from the WSGI environment. Has properties and methods for
  29. using the functionality defined by various HTTP specs. The data in
  30. requests object is read-only.
  31. Text data is assumed to use UTF-8 encoding, which should be true for
  32. the vast majority of modern clients. Using an encoding set by the
  33. client is unsafe in Python due to extra encodings it provides, such
  34. as ``zip``. To change the assumed encoding, subclass and replace
  35. :attr:`charset`.
  36. :param environ: The WSGI environ is generated by the WSGI server and
  37. contains information about the server configuration and client
  38. request.
  39. :param populate_request: Add this request object to the WSGI environ
  40. as ``environ['werkzeug.request']``. Can be useful when
  41. debugging.
  42. :param shallow: Makes reading from :attr:`stream` (and any method
  43. that would read from it) raise a :exc:`RuntimeError`. Useful to
  44. prevent consuming the form data in middleware, which would make
  45. it unavailable to the final application.
  46. .. versionchanged:: 2.0
  47. Combine ``BaseRequest`` and mixins into a single ``Request``
  48. class. Using the old classes is deprecated and will be removed
  49. in Werkzeug 2.1.
  50. .. versionchanged:: 0.5
  51. Read-only mode is enforced with immutable classes for all data.
  52. """
  53. #: the maximum content length. This is forwarded to the form data
  54. #: parsing function (:func:`parse_form_data`). When set and the
  55. #: :attr:`form` or :attr:`files` attribute is accessed and the
  56. #: parsing fails because more than the specified value is transmitted
  57. #: a :exc:`~werkzeug.exceptions.RequestEntityTooLarge` exception is raised.
  58. #:
  59. #: Have a look at :doc:`/request_data` for more details.
  60. #:
  61. #: .. versionadded:: 0.5
  62. max_content_length: t.Optional[int] = None
  63. #: the maximum form field size. This is forwarded to the form data
  64. #: parsing function (:func:`parse_form_data`). When set and the
  65. #: :attr:`form` or :attr:`files` attribute is accessed and the
  66. #: data in memory for post data is longer than the specified value a
  67. #: :exc:`~werkzeug.exceptions.RequestEntityTooLarge` exception is raised.
  68. #:
  69. #: Have a look at :doc:`/request_data` for more details.
  70. #:
  71. #: .. versionadded:: 0.5
  72. max_form_memory_size: t.Optional[int] = None
  73. #: The form data parser that shoud be used. Can be replaced to customize
  74. #: the form date parsing.
  75. form_data_parser_class: t.Type[FormDataParser] = FormDataParser
  76. #: Disable the :attr:`data` property to avoid reading from the input
  77. #: stream.
  78. #:
  79. #: .. deprecated:: 2.0
  80. #: Will be removed in Werkzeug 2.1. Create the request with
  81. #: ``shallow=True`` instead.
  82. #:
  83. #: .. versionadded:: 0.9
  84. disable_data_descriptor: t.Optional[bool] = None
  85. #: The WSGI environment containing HTTP headers and information from
  86. #: the WSGI server.
  87. environ: "WSGIEnvironment"
  88. #: Set when creating the request object. If ``True``, reading from
  89. #: the request body will cause a ``RuntimeException``. Useful to
  90. #: prevent modifying the stream from middleware.
  91. shallow: bool
  92. def __init__(
  93. self,
  94. environ: "WSGIEnvironment",
  95. populate_request: bool = True,
  96. shallow: bool = False,
  97. ) -> None:
  98. super().__init__(
  99. method=environ.get("REQUEST_METHOD", "GET"),
  100. scheme=environ.get("wsgi.url_scheme", "http"),
  101. server=_get_server(environ),
  102. root_path=_wsgi_decoding_dance(
  103. environ.get("SCRIPT_NAME") or "", self.charset, self.encoding_errors
  104. ),
  105. path=_wsgi_decoding_dance(
  106. environ.get("PATH_INFO") or "", self.charset, self.encoding_errors
  107. ),
  108. query_string=environ.get("QUERY_STRING", "").encode("latin1"),
  109. headers=EnvironHeaders(environ),
  110. remote_addr=environ.get("REMOTE_ADDR"),
  111. )
  112. self.environ = environ
  113. if self.disable_data_descriptor is not None:
  114. warnings.warn(
  115. "'disable_data_descriptor' is deprecated and will be"
  116. " removed in Werkzeug 2.1. Create the request with"
  117. " 'shallow=True' instead.",
  118. DeprecationWarning,
  119. stacklevel=2,
  120. )
  121. shallow = shallow or self.disable_data_descriptor
  122. self.shallow = shallow
  123. if populate_request and not shallow:
  124. self.environ["werkzeug.request"] = self
  125. @classmethod
  126. def from_values(cls, *args: t.Any, **kwargs: t.Any) -> "Request":
  127. """Create a new request object based on the values provided. If
  128. environ is given missing values are filled from there. This method is
  129. useful for small scripts when you need to simulate a request from an URL.
  130. Do not use this method for unittesting, there is a full featured client
  131. object (:class:`Client`) that allows to create multipart requests,
  132. support for cookies etc.
  133. This accepts the same options as the
  134. :class:`~werkzeug.test.EnvironBuilder`.
  135. .. versionchanged:: 0.5
  136. This method now accepts the same arguments as
  137. :class:`~werkzeug.test.EnvironBuilder`. Because of this the
  138. `environ` parameter is now called `environ_overrides`.
  139. :return: request object
  140. """
  141. from ..test import EnvironBuilder
  142. charset = kwargs.pop("charset", cls.charset)
  143. kwargs["charset"] = charset
  144. builder = EnvironBuilder(*args, **kwargs)
  145. try:
  146. return builder.get_request(cls)
  147. finally:
  148. builder.close()
  149. @classmethod
  150. def application(
  151. cls, f: t.Callable[["Request"], "WSGIApplication"]
  152. ) -> "WSGIApplication":
  153. """Decorate a function as responder that accepts the request as
  154. the last argument. This works like the :func:`responder`
  155. decorator but the function is passed the request object as the
  156. last argument and the request object will be closed
  157. automatically::
  158. @Request.application
  159. def my_wsgi_app(request):
  160. return Response('Hello World!')
  161. As of Werkzeug 0.14 HTTP exceptions are automatically caught and
  162. converted to responses instead of failing.
  163. :param f: the WSGI callable to decorate
  164. :return: a new WSGI callable
  165. """
  166. #: return a callable that wraps the -2nd argument with the request
  167. #: and calls the function with all the arguments up to that one and
  168. #: the request. The return value is then called with the latest
  169. #: two arguments. This makes it possible to use this decorator for
  170. #: both standalone WSGI functions as well as bound methods and
  171. #: partially applied functions.
  172. from ..exceptions import HTTPException
  173. @functools.wraps(f)
  174. def application(*args): # type: ignore
  175. request = cls(args[-2])
  176. with request:
  177. try:
  178. resp = f(*args[:-2] + (request,))
  179. except HTTPException as e:
  180. resp = e.get_response(args[-2])
  181. return resp(*args[-2:])
  182. return t.cast("WSGIApplication", application)
  183. def _get_file_stream(
  184. self,
  185. total_content_length: t.Optional[int],
  186. content_type: t.Optional[str],
  187. filename: t.Optional[str] = None,
  188. content_length: t.Optional[int] = None,
  189. ) -> t.IO[bytes]:
  190. """Called to get a stream for the file upload.
  191. This must provide a file-like class with `read()`, `readline()`
  192. and `seek()` methods that is both writeable and readable.
  193. The default implementation returns a temporary file if the total
  194. content length is higher than 500KB. Because many browsers do not
  195. provide a content length for the files only the total content
  196. length matters.
  197. :param total_content_length: the total content length of all the
  198. data in the request combined. This value
  199. is guaranteed to be there.
  200. :param content_type: the mimetype of the uploaded file.
  201. :param filename: the filename of the uploaded file. May be `None`.
  202. :param content_length: the length of this file. This value is usually
  203. not provided because webbrowsers do not provide
  204. this value.
  205. """
  206. return default_stream_factory(
  207. total_content_length=total_content_length,
  208. filename=filename,
  209. content_type=content_type,
  210. content_length=content_length,
  211. )
  212. @property
  213. def want_form_data_parsed(self) -> bool:
  214. """``True`` if the request method carries content. By default
  215. this is true if a ``Content-Type`` is sent.
  216. .. versionadded:: 0.8
  217. """
  218. return bool(self.environ.get("CONTENT_TYPE"))
  219. def make_form_data_parser(self) -> FormDataParser:
  220. """Creates the form data parser. Instantiates the
  221. :attr:`form_data_parser_class` with some parameters.
  222. .. versionadded:: 0.8
  223. """
  224. return self.form_data_parser_class(
  225. self._get_file_stream,
  226. self.charset,
  227. self.encoding_errors,
  228. self.max_form_memory_size,
  229. self.max_content_length,
  230. self.parameter_storage_class,
  231. )
  232. def _load_form_data(self) -> None:
  233. """Method used internally to retrieve submitted data. After calling
  234. this sets `form` and `files` on the request object to multi dicts
  235. filled with the incoming form data. As a matter of fact the input
  236. stream will be empty afterwards. You can also call this method to
  237. force the parsing of the form data.
  238. .. versionadded:: 0.8
  239. """
  240. # abort early if we have already consumed the stream
  241. if "form" in self.__dict__:
  242. return
  243. if self.want_form_data_parsed:
  244. parser = self.make_form_data_parser()
  245. data = parser.parse(
  246. self._get_stream_for_parsing(),
  247. self.mimetype,
  248. self.content_length,
  249. self.mimetype_params,
  250. )
  251. else:
  252. data = (
  253. self.stream,
  254. self.parameter_storage_class(),
  255. self.parameter_storage_class(),
  256. )
  257. # inject the values into the instance dict so that we bypass
  258. # our cached_property non-data descriptor.
  259. d = self.__dict__
  260. d["stream"], d["form"], d["files"] = data
  261. def _get_stream_for_parsing(self) -> t.IO[bytes]:
  262. """This is the same as accessing :attr:`stream` with the difference
  263. that if it finds cached data from calling :meth:`get_data` first it
  264. will create a new stream out of the cached data.
  265. .. versionadded:: 0.9.3
  266. """
  267. cached_data = getattr(self, "_cached_data", None)
  268. if cached_data is not None:
  269. return BytesIO(cached_data)
  270. return self.stream
  271. def close(self) -> None:
  272. """Closes associated resources of this request object. This
  273. closes all file handles explicitly. You can also use the request
  274. object in a with statement which will automatically close it.
  275. .. versionadded:: 0.9
  276. """
  277. files = self.__dict__.get("files")
  278. for _key, value in iter_multi_items(files or ()):
  279. value.close()
  280. def __enter__(self) -> "Request":
  281. return self
  282. def __exit__(self, exc_type, exc_value, tb) -> None: # type: ignore
  283. self.close()
  284. @cached_property
  285. def stream(self) -> t.IO[bytes]:
  286. """
  287. If the incoming form data was not encoded with a known mimetype
  288. the data is stored unmodified in this stream for consumption. Most
  289. of the time it is a better idea to use :attr:`data` which will give
  290. you that data as a string. The stream only returns the data once.
  291. Unlike :attr:`input_stream` this stream is properly guarded that you
  292. can't accidentally read past the length of the input. Werkzeug will
  293. internally always refer to this stream to read data which makes it
  294. possible to wrap this object with a stream that does filtering.
  295. .. versionchanged:: 0.9
  296. This stream is now always available but might be consumed by the
  297. form parser later on. Previously the stream was only set if no
  298. parsing happened.
  299. """
  300. if self.shallow:
  301. raise RuntimeError(
  302. "This request was created with 'shallow=True', reading"
  303. " from the input stream is disabled."
  304. )
  305. return get_input_stream(self.environ)
  306. input_stream = environ_property[t.IO[bytes]](
  307. "wsgi.input",
  308. doc="""The WSGI input stream.
  309. In general it's a bad idea to use this one because you can
  310. easily read past the boundary. Use the :attr:`stream`
  311. instead.""",
  312. )
  313. @cached_property
  314. def data(self) -> bytes:
  315. """
  316. Contains the incoming request data as string in case it came with
  317. a mimetype Werkzeug does not handle.
  318. """
  319. return self.get_data(parse_form_data=True)
  320. @typing.overload
  321. def get_data( # type: ignore
  322. self,
  323. cache: bool = True,
  324. as_text: "te.Literal[False]" = False,
  325. parse_form_data: bool = False,
  326. ) -> bytes:
  327. ...
  328. @typing.overload
  329. def get_data(
  330. self,
  331. cache: bool = True,
  332. as_text: "te.Literal[True]" = ...,
  333. parse_form_data: bool = False,
  334. ) -> str:
  335. ...
  336. def get_data(
  337. self, cache: bool = True, as_text: bool = False, parse_form_data: bool = False
  338. ) -> t.Union[bytes, str]:
  339. """This reads the buffered incoming data from the client into one
  340. bytes object. By default this is cached but that behavior can be
  341. changed by setting `cache` to `False`.
  342. Usually it's a bad idea to call this method without checking the
  343. content length first as a client could send dozens of megabytes or more
  344. to cause memory problems on the server.
  345. Note that if the form data was already parsed this method will not
  346. return anything as form data parsing does not cache the data like
  347. this method does. To implicitly invoke form data parsing function
  348. set `parse_form_data` to `True`. When this is done the return value
  349. of this method will be an empty string if the form parser handles
  350. the data. This generally is not necessary as if the whole data is
  351. cached (which is the default) the form parser will used the cached
  352. data to parse the form data. Please be generally aware of checking
  353. the content length first in any case before calling this method
  354. to avoid exhausting server memory.
  355. If `as_text` is set to `True` the return value will be a decoded
  356. string.
  357. .. versionadded:: 0.9
  358. """
  359. rv = getattr(self, "_cached_data", None)
  360. if rv is None:
  361. if parse_form_data:
  362. self._load_form_data()
  363. rv = self.stream.read()
  364. if cache:
  365. self._cached_data = rv
  366. if as_text:
  367. rv = rv.decode(self.charset, self.encoding_errors)
  368. return rv # type: ignore
  369. @cached_property
  370. def form(self) -> "ImmutableMultiDict[str, str]":
  371. """The form parameters. By default an
  372. :class:`~werkzeug.datastructures.ImmutableMultiDict`
  373. is returned from this function. This can be changed by setting
  374. :attr:`parameter_storage_class` to a different type. This might
  375. be necessary if the order of the form data is important.
  376. Please keep in mind that file uploads will not end up here, but instead
  377. in the :attr:`files` attribute.
  378. .. versionchanged:: 0.9
  379. Previous to Werkzeug 0.9 this would only contain form data for POST
  380. and PUT requests.
  381. """
  382. self._load_form_data()
  383. return self.form
  384. @cached_property
  385. def values(self) -> "CombinedMultiDict[str, str]":
  386. """A :class:`werkzeug.datastructures.CombinedMultiDict` that
  387. combines :attr:`args` and :attr:`form`.
  388. For GET requests, only ``args`` are present, not ``form``.
  389. .. versionchanged:: 2.0
  390. For GET requests, only ``args`` are present, not ``form``.
  391. """
  392. sources = [self.args]
  393. if self.method != "GET":
  394. # GET requests can have a body, and some caching proxies
  395. # might not treat that differently than a normal GET
  396. # request, allowing form data to "invisibly" affect the
  397. # cache without indication in the query string / URL.
  398. sources.append(self.form)
  399. args = []
  400. for d in sources:
  401. if not isinstance(d, MultiDict):
  402. d = MultiDict(d)
  403. args.append(d)
  404. return CombinedMultiDict(args)
  405. @cached_property
  406. def files(self) -> "ImmutableMultiDict[str, FileStorage]":
  407. """:class:`~werkzeug.datastructures.MultiDict` object containing
  408. all uploaded files. Each key in :attr:`files` is the name from the
  409. ``<input type="file" name="">``. Each value in :attr:`files` is a
  410. Werkzeug :class:`~werkzeug.datastructures.FileStorage` object.
  411. It basically behaves like a standard file object you know from Python,
  412. with the difference that it also has a
  413. :meth:`~werkzeug.datastructures.FileStorage.save` function that can
  414. store the file on the filesystem.
  415. Note that :attr:`files` will only contain data if the request method was
  416. POST, PUT or PATCH and the ``<form>`` that posted to the request had
  417. ``enctype="multipart/form-data"``. It will be empty otherwise.
  418. See the :class:`~werkzeug.datastructures.MultiDict` /
  419. :class:`~werkzeug.datastructures.FileStorage` documentation for
  420. more details about the used data structure.
  421. """
  422. self._load_form_data()
  423. return self.files
  424. @property
  425. def script_root(self) -> str:
  426. """Alias for :attr:`self.root_path`. ``environ["SCRIPT_ROOT"]``
  427. without a trailing slash.
  428. """
  429. return self.root_path
  430. @cached_property
  431. def url_root(self) -> str:
  432. """Alias for :attr:`root_url`. The URL with scheme, host, and
  433. root path. For example, ``https://example.com/app/``.
  434. """
  435. return self.root_url
  436. remote_user = environ_property[str](
  437. "REMOTE_USER",
  438. doc="""If the server supports user authentication, and the
  439. script is protected, this attribute contains the username the
  440. user has authenticated as.""",
  441. )
  442. is_multithread = environ_property[bool](
  443. "wsgi.multithread",
  444. doc="""boolean that is `True` if the application is served by a
  445. multithreaded WSGI server.""",
  446. )
  447. is_multiprocess = environ_property[bool](
  448. "wsgi.multiprocess",
  449. doc="""boolean that is `True` if the application is served by a
  450. WSGI server that spawns multiple processes.""",
  451. )
  452. is_run_once = environ_property[bool](
  453. "wsgi.run_once",
  454. doc="""boolean that is `True` if the application will be
  455. executed only once in a process lifetime. This is the case for
  456. CGI for example, but it's not guaranteed that the execution only
  457. happens one time.""",
  458. )
  459. # JSON
  460. #: A module or other object that has ``dumps`` and ``loads``
  461. #: functions that match the API of the built-in :mod:`json` module.
  462. json_module = json
  463. @property
  464. def json(self) -> t.Optional[t.Any]:
  465. """The parsed JSON data if :attr:`mimetype` indicates JSON
  466. (:mimetype:`application/json`, see :attr:`is_json`).
  467. Calls :meth:`get_json` with default arguments.
  468. """
  469. return self.get_json()
  470. # Cached values for ``(silent=False, silent=True)``. Initialized
  471. # with sentinel values.
  472. _cached_json: t.Tuple[t.Any, t.Any] = (Ellipsis, Ellipsis)
  473. def get_json(
  474. self, force: bool = False, silent: bool = False, cache: bool = True
  475. ) -> t.Optional[t.Any]:
  476. """Parse :attr:`data` as JSON.
  477. If the mimetype does not indicate JSON
  478. (:mimetype:`application/json`, see :attr:`is_json`), this
  479. returns ``None``.
  480. If parsing fails, :meth:`on_json_loading_failed` is called and
  481. its return value is used as the return value.
  482. :param force: Ignore the mimetype and always try to parse JSON.
  483. :param silent: Silence parsing errors and return ``None``
  484. instead.
  485. :param cache: Store the parsed JSON to return for subsequent
  486. calls.
  487. """
  488. if cache and self._cached_json[silent] is not Ellipsis:
  489. return self._cached_json[silent]
  490. if not (force or self.is_json):
  491. return None
  492. data = self.get_data(cache=cache)
  493. try:
  494. rv = self.json_module.loads(data)
  495. except ValueError as e:
  496. if silent:
  497. rv = None
  498. if cache:
  499. normal_rv, _ = self._cached_json
  500. self._cached_json = (normal_rv, rv)
  501. else:
  502. rv = self.on_json_loading_failed(e)
  503. if cache:
  504. _, silent_rv = self._cached_json
  505. self._cached_json = (rv, silent_rv)
  506. else:
  507. if cache:
  508. self._cached_json = (rv, rv)
  509. return rv
  510. def on_json_loading_failed(self, e: ValueError) -> t.Any:
  511. """Called if :meth:`get_json` parsing fails and isn't silenced.
  512. If this method returns a value, it is used as the return value
  513. for :meth:`get_json`. The default implementation raises
  514. :exc:`~werkzeug.exceptions.BadRequest`.
  515. """
  516. raise BadRequest(f"Failed to decode JSON object: {e}")
  517. class StreamOnlyMixin:
  518. """Mixin to create a ``Request`` that disables the ``data``,
  519. ``form``, and ``files`` properties. Only ``stream`` is available.
  520. .. deprecated:: 2.0
  521. Will be removed in Werkzeug 2.1. Create the request with
  522. ``shallow=True`` instead.
  523. .. versionadded:: 0.9
  524. """
  525. def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
  526. warnings.warn(
  527. "'StreamOnlyMixin' is deprecated and will be removed in"
  528. " Werkzeug 2.1. Create the request with 'shallow=True'"
  529. " instead.",
  530. DeprecationWarning,
  531. stacklevel=2,
  532. )
  533. kwargs["shallow"] = True
  534. super().__init__(*args, **kwargs) # type: ignore
  535. class PlainRequest(StreamOnlyMixin, Request):
  536. """A request object without ``data``, ``form``, and ``files``.
  537. .. deprecated:: 2.0
  538. Will be removed in Werkzeug 2.1. Create the request with
  539. ``shallow=True`` instead.
  540. .. versionadded:: 0.9
  541. """
  542. def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
  543. warnings.warn(
  544. "'PlainRequest' is deprecated and will be removed in"
  545. " Werkzeug 2.1. Create the request with 'shallow=True'"
  546. " instead.",
  547. DeprecationWarning,
  548. stacklevel=2,
  549. )
  550. # Don't show the DeprecationWarning for StreamOnlyMixin.
  551. with warnings.catch_warnings():
  552. warnings.simplefilter("ignore", DeprecationWarning)
  553. super().__init__(*args, **kwargs)