cors.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from ..http import dump_header
  2. from ..http import parse_set_header
  3. from ..utils import environ_property
  4. from ..utils import header_property
  5. class CORSRequestMixin(object):
  6. """A mixin for :class:`~werkzeug.wrappers.BaseRequest` subclasses
  7. that adds descriptors for Cross Origin Resource Sharing (CORS)
  8. headers.
  9. .. versionadded:: 1.0
  10. """
  11. origin = environ_property(
  12. "HTTP_ORIGIN",
  13. doc=(
  14. "The host that the request originated from. Set"
  15. " :attr:`~CORSResponseMixin.access_control_allow_origin` on"
  16. " the response to indicate which origins are allowed."
  17. ),
  18. )
  19. access_control_request_headers = environ_property(
  20. "HTTP_ACCESS_CONTROL_REQUEST_HEADERS",
  21. load_func=parse_set_header,
  22. doc=(
  23. "Sent with a preflight request to indicate which headers"
  24. " will be sent with the cross origin request. Set"
  25. " :attr:`~CORSResponseMixin.access_control_allow_headers`"
  26. " on the response to indicate which headers are allowed."
  27. ),
  28. )
  29. access_control_request_method = environ_property(
  30. "HTTP_ACCESS_CONTROL_REQUEST_METHOD",
  31. doc=(
  32. "Sent with a preflight request to indicate which method"
  33. " will be used for the cross origin request. Set"
  34. " :attr:`~CORSResponseMixin.access_control_allow_methods`"
  35. " on the response to indicate which methods are allowed."
  36. ),
  37. )
  38. class CORSResponseMixin(object):
  39. """A mixin for :class:`~werkzeug.wrappers.BaseResponse` subclasses
  40. that adds descriptors for Cross Origin Resource Sharing (CORS)
  41. headers.
  42. .. versionadded:: 1.0
  43. """
  44. @property
  45. def access_control_allow_credentials(self):
  46. """Whether credentials can be shared by the browser to
  47. JavaScript code. As part of the preflight request it indicates
  48. whether credentials can be used on the cross origin request.
  49. """
  50. return "Access-Control-Allow-Credentials" in self.headers
  51. @access_control_allow_credentials.setter
  52. def access_control_allow_credentials(self, value):
  53. if value is True:
  54. self.headers["Access-Control-Allow-Credentials"] = "true"
  55. else:
  56. self.headers.pop("Access-Control-Allow-Credentials", None)
  57. access_control_allow_headers = header_property(
  58. "Access-Control-Allow-Headers",
  59. load_func=parse_set_header,
  60. dump_func=dump_header,
  61. doc="Which headers can be sent with the cross origin request.",
  62. )
  63. access_control_allow_methods = header_property(
  64. "Access-Control-Allow-Methods",
  65. load_func=parse_set_header,
  66. dump_func=dump_header,
  67. doc="Which methods can be used for the cross origin request.",
  68. )
  69. access_control_allow_origin = header_property(
  70. "Access-Control-Allow-Origin",
  71. doc="The origin or '*' for any origin that may make cross origin requests.",
  72. )
  73. access_control_expose_headers = header_property(
  74. "Access-Control-Expose-Headers",
  75. load_func=parse_set_header,
  76. dump_func=dump_header,
  77. doc="Which headers can be shared by the browser to JavaScript code.",
  78. )
  79. access_control_max_age = header_property(
  80. "Access-Control-Max-Age",
  81. load_func=int,
  82. dump_func=str,
  83. doc="The maximum age in seconds the access control settings can be cached for.",
  84. )