wrappers.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import typing as t
  2. from werkzeug.exceptions import BadRequest
  3. from werkzeug.wrappers import Request as RequestBase
  4. from werkzeug.wrappers import Response as ResponseBase
  5. from . import json
  6. from .globals import current_app
  7. from .helpers import _split_blueprint_path
  8. if t.TYPE_CHECKING:
  9. from werkzeug.routing import Rule
  10. class Request(RequestBase):
  11. """The request object used by default in Flask. Remembers the
  12. matched endpoint and view arguments.
  13. It is what ends up as :class:`~flask.request`. If you want to replace
  14. the request object used you can subclass this and set
  15. :attr:`~flask.Flask.request_class` to your subclass.
  16. The request object is a :class:`~werkzeug.wrappers.Request` subclass and
  17. provides all of the attributes Werkzeug defines plus a few Flask
  18. specific ones.
  19. """
  20. json_module = json
  21. #: The internal URL rule that matched the request. This can be
  22. #: useful to inspect which methods are allowed for the URL from
  23. #: a before/after handler (``request.url_rule.methods``) etc.
  24. #: Though if the request's method was invalid for the URL rule,
  25. #: the valid list is available in ``routing_exception.valid_methods``
  26. #: instead (an attribute of the Werkzeug exception
  27. #: :exc:`~werkzeug.exceptions.MethodNotAllowed`)
  28. #: because the request was never internally bound.
  29. #:
  30. #: .. versionadded:: 0.6
  31. url_rule: t.Optional["Rule"] = None
  32. #: A dict of view arguments that matched the request. If an exception
  33. #: happened when matching, this will be ``None``.
  34. view_args: t.Optional[t.Dict[str, t.Any]] = None
  35. #: If matching the URL failed, this is the exception that will be
  36. #: raised / was raised as part of the request handling. This is
  37. #: usually a :exc:`~werkzeug.exceptions.NotFound` exception or
  38. #: something similar.
  39. routing_exception: t.Optional[Exception] = None
  40. @property
  41. def max_content_length(self) -> t.Optional[int]: # type: ignore
  42. """Read-only view of the ``MAX_CONTENT_LENGTH`` config key."""
  43. if current_app:
  44. return current_app.config["MAX_CONTENT_LENGTH"]
  45. else:
  46. return None
  47. @property
  48. def endpoint(self) -> t.Optional[str]:
  49. """The endpoint that matched the request URL.
  50. This will be ``None`` if matching failed or has not been
  51. performed yet.
  52. This in combination with :attr:`view_args` can be used to
  53. reconstruct the same URL or a modified URL.
  54. """
  55. if self.url_rule is not None:
  56. return self.url_rule.endpoint
  57. return None
  58. @property
  59. def blueprint(self) -> t.Optional[str]:
  60. """The registered name of the current blueprint.
  61. This will be ``None`` if the endpoint is not part of a
  62. blueprint, or if URL matching failed or has not been performed
  63. yet.
  64. This does not necessarily match the name the blueprint was
  65. created with. It may have been nested, or registered with a
  66. different name.
  67. """
  68. endpoint = self.endpoint
  69. if endpoint is not None and "." in endpoint:
  70. return endpoint.rpartition(".")[0]
  71. return None
  72. @property
  73. def blueprints(self) -> t.List[str]:
  74. """The registered names of the current blueprint upwards through
  75. parent blueprints.
  76. This will be an empty list if there is no current blueprint, or
  77. if URL matching failed.
  78. .. versionadded:: 2.0.1
  79. """
  80. name = self.blueprint
  81. if name is None:
  82. return []
  83. return _split_blueprint_path(name)
  84. def _load_form_data(self) -> None:
  85. super()._load_form_data()
  86. # In debug mode we're replacing the files multidict with an ad-hoc
  87. # subclass that raises a different error for key errors.
  88. if (
  89. current_app
  90. and current_app.debug
  91. and self.mimetype != "multipart/form-data"
  92. and not self.files
  93. ):
  94. from .debughelpers import attach_enctype_error_multidict
  95. attach_enctype_error_multidict(self)
  96. def on_json_loading_failed(self, e: t.Optional[ValueError]) -> t.Any:
  97. try:
  98. return super().on_json_loading_failed(e)
  99. except BadRequest as e:
  100. if current_app and current_app.debug:
  101. raise
  102. raise BadRequest() from e
  103. class Response(ResponseBase):
  104. """The response object that is used by default in Flask. Works like the
  105. response object from Werkzeug but is set to have an HTML mimetype by
  106. default. Quite often you don't have to create this object yourself because
  107. :meth:`~flask.Flask.make_response` will take care of that for you.
  108. If you want to replace the response object used you can subclass this and
  109. set :attr:`~flask.Flask.response_class` to your subclass.
  110. .. versionchanged:: 1.0
  111. JSON support is added to the response, like the request. This is useful
  112. when testing to get the test client response data as JSON.
  113. .. versionchanged:: 1.0
  114. Added :attr:`max_cookie_size`.
  115. """
  116. default_mimetype = "text/html"
  117. json_module = json
  118. autocorrect_location_header = False
  119. @property
  120. def max_cookie_size(self) -> int: # type: ignore
  121. """Read-only view of the :data:`MAX_COOKIE_SIZE` config key.
  122. See :attr:`~werkzeug.wrappers.Response.max_cookie_size` in
  123. Werkzeug's docs.
  124. """
  125. if current_app:
  126. return current_app.config["MAX_COOKIE_SIZE"]
  127. # return Werkzeug's default when not in an app context
  128. return super().max_cookie_size