common_descriptors.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. from datetime import datetime
  2. from datetime import timedelta
  3. from .._compat import string_types
  4. from ..datastructures import CallbackDict
  5. from ..http import dump_age
  6. from ..http import dump_csp_header
  7. from ..http import dump_header
  8. from ..http import dump_options_header
  9. from ..http import http_date
  10. from ..http import parse_age
  11. from ..http import parse_csp_header
  12. from ..http import parse_date
  13. from ..http import parse_options_header
  14. from ..http import parse_set_header
  15. from ..utils import cached_property
  16. from ..utils import environ_property
  17. from ..utils import get_content_type
  18. from ..utils import header_property
  19. from ..wsgi import get_content_length
  20. class CommonRequestDescriptorsMixin(object):
  21. """A mixin for :class:`BaseRequest` subclasses. Request objects that
  22. mix this class in will automatically get descriptors for a couple of
  23. HTTP headers with automatic type conversion.
  24. .. versionadded:: 0.5
  25. """
  26. content_type = environ_property(
  27. "CONTENT_TYPE",
  28. doc="""The Content-Type entity-header field indicates the media
  29. type of the entity-body sent to the recipient or, in the case of
  30. the HEAD method, the media type that would have been sent had
  31. the request been a GET.""",
  32. )
  33. @cached_property
  34. def content_length(self):
  35. """The Content-Length entity-header field indicates the size of the
  36. entity-body in bytes or, in the case of the HEAD method, the size of
  37. the entity-body that would have been sent had the request been a
  38. GET.
  39. """
  40. return get_content_length(self.environ)
  41. content_encoding = environ_property(
  42. "HTTP_CONTENT_ENCODING",
  43. doc="""The Content-Encoding entity-header field is used as a
  44. modifier to the media-type. When present, its value indicates
  45. what additional content codings have been applied to the
  46. entity-body, and thus what decoding mechanisms must be applied
  47. in order to obtain the media-type referenced by the Content-Type
  48. header field.
  49. .. versionadded:: 0.9""",
  50. )
  51. content_md5 = environ_property(
  52. "HTTP_CONTENT_MD5",
  53. doc="""The Content-MD5 entity-header field, as defined in
  54. RFC 1864, is an MD5 digest of the entity-body for the purpose of
  55. providing an end-to-end message integrity check (MIC) of the
  56. entity-body. (Note: a MIC is good for detecting accidental
  57. modification of the entity-body in transit, but is not proof
  58. against malicious attacks.)
  59. .. versionadded:: 0.9""",
  60. )
  61. referrer = environ_property(
  62. "HTTP_REFERER",
  63. doc="""The Referer[sic] request-header field allows the client
  64. to specify, for the server's benefit, the address (URI) of the
  65. resource from which the Request-URI was obtained (the
  66. "referrer", although the header field is misspelled).""",
  67. )
  68. date = environ_property(
  69. "HTTP_DATE",
  70. None,
  71. parse_date,
  72. doc="""The Date general-header field represents the date and
  73. time at which the message was originated, having the same
  74. semantics as orig-date in RFC 822.""",
  75. )
  76. max_forwards = environ_property(
  77. "HTTP_MAX_FORWARDS",
  78. None,
  79. int,
  80. doc="""The Max-Forwards request-header field provides a
  81. mechanism with the TRACE and OPTIONS methods to limit the number
  82. of proxies or gateways that can forward the request to the next
  83. inbound server.""",
  84. )
  85. def _parse_content_type(self):
  86. if not hasattr(self, "_parsed_content_type"):
  87. self._parsed_content_type = parse_options_header(
  88. self.environ.get("CONTENT_TYPE", "")
  89. )
  90. @property
  91. def mimetype(self):
  92. """Like :attr:`content_type`, but without parameters (eg, without
  93. charset, type etc.) and always lowercase. For example if the content
  94. type is ``text/HTML; charset=utf-8`` the mimetype would be
  95. ``'text/html'``.
  96. """
  97. self._parse_content_type()
  98. return self._parsed_content_type[0].lower()
  99. @property
  100. def mimetype_params(self):
  101. """The mimetype parameters as dict. For example if the content
  102. type is ``text/html; charset=utf-8`` the params would be
  103. ``{'charset': 'utf-8'}``.
  104. """
  105. self._parse_content_type()
  106. return self._parsed_content_type[1]
  107. @cached_property
  108. def pragma(self):
  109. """The Pragma general-header field is used to include
  110. implementation-specific directives that might apply to any recipient
  111. along the request/response chain. All pragma directives specify
  112. optional behavior from the viewpoint of the protocol; however, some
  113. systems MAY require that behavior be consistent with the directives.
  114. """
  115. return parse_set_header(self.environ.get("HTTP_PRAGMA", ""))
  116. class CommonResponseDescriptorsMixin(object):
  117. """A mixin for :class:`BaseResponse` subclasses. Response objects that
  118. mix this class in will automatically get descriptors for a couple of
  119. HTTP headers with automatic type conversion.
  120. """
  121. @property
  122. def mimetype(self):
  123. """The mimetype (content type without charset etc.)"""
  124. ct = self.headers.get("content-type")
  125. if ct:
  126. return ct.split(";")[0].strip()
  127. @mimetype.setter
  128. def mimetype(self, value):
  129. self.headers["Content-Type"] = get_content_type(value, self.charset)
  130. @property
  131. def mimetype_params(self):
  132. """The mimetype parameters as dict. For example if the
  133. content type is ``text/html; charset=utf-8`` the params would be
  134. ``{'charset': 'utf-8'}``.
  135. .. versionadded:: 0.5
  136. """
  137. def on_update(d):
  138. self.headers["Content-Type"] = dump_options_header(self.mimetype, d)
  139. d = parse_options_header(self.headers.get("content-type", ""))[1]
  140. return CallbackDict(d, on_update)
  141. location = header_property(
  142. "Location",
  143. doc="""The Location response-header field is used to redirect
  144. the recipient to a location other than the Request-URI for
  145. completion of the request or identification of a new
  146. resource.""",
  147. )
  148. age = header_property(
  149. "Age",
  150. None,
  151. parse_age,
  152. dump_age,
  153. doc="""The Age response-header field conveys the sender's
  154. estimate of the amount of time since the response (or its
  155. revalidation) was generated at the origin server.
  156. Age values are non-negative decimal integers, representing time
  157. in seconds.""",
  158. )
  159. content_type = header_property(
  160. "Content-Type",
  161. doc="""The Content-Type entity-header field indicates the media
  162. type of the entity-body sent to the recipient or, in the case of
  163. the HEAD method, the media type that would have been sent had
  164. the request been a GET.""",
  165. )
  166. content_length = header_property(
  167. "Content-Length",
  168. None,
  169. int,
  170. str,
  171. doc="""The Content-Length entity-header field indicates the size
  172. of the entity-body, in decimal number of OCTETs, sent to the
  173. recipient or, in the case of the HEAD method, the size of the
  174. entity-body that would have been sent had the request been a
  175. GET.""",
  176. )
  177. content_location = header_property(
  178. "Content-Location",
  179. doc="""The Content-Location entity-header field MAY be used to
  180. supply the resource location for the entity enclosed in the
  181. message when that entity is accessible from a location separate
  182. from the requested resource's URI.""",
  183. )
  184. content_encoding = header_property(
  185. "Content-Encoding",
  186. doc="""The Content-Encoding entity-header field is used as a
  187. modifier to the media-type. When present, its value indicates
  188. what additional content codings have been applied to the
  189. entity-body, and thus what decoding mechanisms must be applied
  190. in order to obtain the media-type referenced by the Content-Type
  191. header field.""",
  192. )
  193. content_md5 = header_property(
  194. "Content-MD5",
  195. doc="""The Content-MD5 entity-header field, as defined in
  196. RFC 1864, is an MD5 digest of the entity-body for the purpose of
  197. providing an end-to-end message integrity check (MIC) of the
  198. entity-body. (Note: a MIC is good for detecting accidental
  199. modification of the entity-body in transit, but is not proof
  200. against malicious attacks.)""",
  201. )
  202. content_security_policy = header_property(
  203. "Content-Security-Policy",
  204. None,
  205. parse_csp_header,
  206. dump_csp_header,
  207. doc="""The Content-Security-Policy header adds an additional layer of
  208. security to help detect and mitigate certain types of attacks.""",
  209. )
  210. content_security_policy_report_only = header_property(
  211. "Content-Security-Policy-Report-Only",
  212. None,
  213. parse_csp_header,
  214. dump_csp_header,
  215. doc="""The Content-Security-Policy-Report-Only header adds a csp policy
  216. that is not enforced but is reported thereby helping detect
  217. certain types of attacks.""",
  218. )
  219. date = header_property(
  220. "Date",
  221. None,
  222. parse_date,
  223. http_date,
  224. doc="""The Date general-header field represents the date and
  225. time at which the message was originated, having the same
  226. semantics as orig-date in RFC 822.""",
  227. )
  228. expires = header_property(
  229. "Expires",
  230. None,
  231. parse_date,
  232. http_date,
  233. doc="""The Expires entity-header field gives the date/time after
  234. which the response is considered stale. A stale cache entry may
  235. not normally be returned by a cache.""",
  236. )
  237. last_modified = header_property(
  238. "Last-Modified",
  239. None,
  240. parse_date,
  241. http_date,
  242. doc="""The Last-Modified entity-header field indicates the date
  243. and time at which the origin server believes the variant was
  244. last modified.""",
  245. )
  246. @property
  247. def retry_after(self):
  248. """The Retry-After response-header field can be used with a
  249. 503 (Service Unavailable) response to indicate how long the
  250. service is expected to be unavailable to the requesting client.
  251. Time in seconds until expiration or date.
  252. """
  253. value = self.headers.get("retry-after")
  254. if value is None:
  255. return
  256. elif value.isdigit():
  257. return datetime.utcnow() + timedelta(seconds=int(value))
  258. return parse_date(value)
  259. @retry_after.setter
  260. def retry_after(self, value):
  261. if value is None:
  262. if "retry-after" in self.headers:
  263. del self.headers["retry-after"]
  264. return
  265. elif isinstance(value, datetime):
  266. value = http_date(value)
  267. else:
  268. value = str(value)
  269. self.headers["Retry-After"] = value
  270. def _set_property(name, doc=None): # noqa: B902
  271. def fget(self):
  272. def on_update(header_set):
  273. if not header_set and name in self.headers:
  274. del self.headers[name]
  275. elif header_set:
  276. self.headers[name] = header_set.to_header()
  277. return parse_set_header(self.headers.get(name), on_update)
  278. def fset(self, value):
  279. if not value:
  280. del self.headers[name]
  281. elif isinstance(value, string_types):
  282. self.headers[name] = value
  283. else:
  284. self.headers[name] = dump_header(value)
  285. return property(fget, fset, doc=doc)
  286. vary = _set_property(
  287. "Vary",
  288. doc="""The Vary field value indicates the set of request-header
  289. fields that fully determines, while the response is fresh,
  290. whether a cache is permitted to use the response to reply to a
  291. subsequent request without revalidation.""",
  292. )
  293. content_language = _set_property(
  294. "Content-Language",
  295. doc="""The Content-Language entity-header field describes the
  296. natural language(s) of the intended audience for the enclosed
  297. entity. Note that this might not be equivalent to all the
  298. languages used within the entity-body.""",
  299. )
  300. allow = _set_property(
  301. "Allow",
  302. doc="""The Allow entity-header field lists the set of methods
  303. supported by the resource identified by the Request-URI. The
  304. purpose of this field is strictly to inform the recipient of
  305. valid methods associated with the resource. An Allow header
  306. field MUST be present in a 405 (Method Not Allowed)
  307. response.""",
  308. )
  309. del _set_property