common_descriptors.py 12 KB

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