You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

response.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import datetime
  2. import json
  3. import mimetypes
  4. import os
  5. import re
  6. import sys
  7. import time
  8. from email.header import Header
  9. from http.client import responses
  10. from urllib.parse import quote, urlparse
  11. from django.conf import settings
  12. from django.core import signals, signing
  13. from django.core.exceptions import DisallowedRedirect
  14. from django.core.serializers.json import DjangoJSONEncoder
  15. from django.http.cookie import SimpleCookie
  16. from django.utils import timezone
  17. from django.utils.encoding import iri_to_uri
  18. from django.utils.http import http_date
  19. _charset_from_content_type_re = re.compile(r';\s*charset=(?P<charset>[^\s;]+)', re.I)
  20. class BadHeaderError(ValueError):
  21. pass
  22. class HttpResponseBase:
  23. """
  24. An HTTP response base class with dictionary-accessed headers.
  25. This class doesn't handle content. It should not be used directly.
  26. Use the HttpResponse and StreamingHttpResponse subclasses instead.
  27. """
  28. status_code = 200
  29. def __init__(self, content_type=None, status=None, reason=None, charset=None):
  30. # _headers is a mapping of the lowercase name to the original case of
  31. # the header (required for working with legacy systems) and the header
  32. # value. Both the name of the header and its value are ASCII strings.
  33. self._headers = {}
  34. self._closable_objects = []
  35. # This parameter is set by the handler. It's necessary to preserve the
  36. # historical behavior of request_finished.
  37. self._handler_class = None
  38. self.cookies = SimpleCookie()
  39. self.closed = False
  40. if status is not None:
  41. try:
  42. self.status_code = int(status)
  43. except (ValueError, TypeError):
  44. raise TypeError('HTTP status code must be an integer.')
  45. if not 100 <= self.status_code <= 599:
  46. raise ValueError('HTTP status code must be an integer from 100 to 599.')
  47. self._reason_phrase = reason
  48. self._charset = charset
  49. if content_type is None:
  50. content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
  51. self.charset)
  52. self['Content-Type'] = content_type
  53. @property
  54. def reason_phrase(self):
  55. if self._reason_phrase is not None:
  56. return self._reason_phrase
  57. # Leave self._reason_phrase unset in order to use the default
  58. # reason phrase for status code.
  59. return responses.get(self.status_code, 'Unknown Status Code')
  60. @reason_phrase.setter
  61. def reason_phrase(self, value):
  62. self._reason_phrase = value
  63. @property
  64. def charset(self):
  65. if self._charset is not None:
  66. return self._charset
  67. content_type = self.get('Content-Type', '')
  68. matched = _charset_from_content_type_re.search(content_type)
  69. if matched:
  70. # Extract the charset and strip its double quotes
  71. return matched.group('charset').replace('"', '')
  72. return settings.DEFAULT_CHARSET
  73. @charset.setter
  74. def charset(self, value):
  75. self._charset = value
  76. def serialize_headers(self):
  77. """HTTP headers as a bytestring."""
  78. def to_bytes(val, encoding):
  79. return val if isinstance(val, bytes) else val.encode(encoding)
  80. headers = [
  81. (to_bytes(key, 'ascii') + b': ' + to_bytes(value, 'latin-1'))
  82. for key, value in self._headers.values()
  83. ]
  84. return b'\r\n'.join(headers)
  85. __bytes__ = serialize_headers
  86. @property
  87. def _content_type_for_repr(self):
  88. return ', "%s"' % self['Content-Type'] if 'Content-Type' in self else ''
  89. def _convert_to_charset(self, value, charset, mime_encode=False):
  90. """
  91. Convert headers key/value to ascii/latin-1 native strings.
  92. `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and
  93. `value` can't be represented in the given charset, apply MIME-encoding.
  94. """
  95. if not isinstance(value, (bytes, str)):
  96. value = str(value)
  97. if ((isinstance(value, bytes) and (b'\n' in value or b'\r' in value)) or
  98. isinstance(value, str) and ('\n' in value or '\r' in value)):
  99. raise BadHeaderError("Header values can't contain newlines (got %r)" % value)
  100. try:
  101. if isinstance(value, str):
  102. # Ensure string is valid in given charset
  103. value.encode(charset)
  104. else:
  105. # Convert bytestring using given charset
  106. value = value.decode(charset)
  107. except UnicodeError as e:
  108. if mime_encode:
  109. value = Header(value, 'utf-8', maxlinelen=sys.maxsize).encode()
  110. else:
  111. e.reason += ', HTTP response headers must be in %s format' % charset
  112. raise
  113. return value
  114. def __setitem__(self, header, value):
  115. header = self._convert_to_charset(header, 'ascii')
  116. value = self._convert_to_charset(value, 'latin-1', mime_encode=True)
  117. self._headers[header.lower()] = (header, value)
  118. def __delitem__(self, header):
  119. self._headers.pop(header.lower(), False)
  120. def __getitem__(self, header):
  121. return self._headers[header.lower()][1]
  122. def has_header(self, header):
  123. """Case-insensitive check for a header."""
  124. return header.lower() in self._headers
  125. __contains__ = has_header
  126. def items(self):
  127. return self._headers.values()
  128. def get(self, header, alternate=None):
  129. return self._headers.get(header.lower(), (None, alternate))[1]
  130. def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
  131. domain=None, secure=False, httponly=False, samesite=None):
  132. """
  133. Set a cookie.
  134. ``expires`` can be:
  135. - a string in the correct format,
  136. - a naive ``datetime.datetime`` object in UTC,
  137. - an aware ``datetime.datetime`` object in any time zone.
  138. If it is a ``datetime.datetime`` object then calculate ``max_age``.
  139. """
  140. self.cookies[key] = value
  141. if expires is not None:
  142. if isinstance(expires, datetime.datetime):
  143. if timezone.is_aware(expires):
  144. expires = timezone.make_naive(expires, timezone.utc)
  145. delta = expires - expires.utcnow()
  146. # Add one second so the date matches exactly (a fraction of
  147. # time gets lost between converting to a timedelta and
  148. # then the date string).
  149. delta = delta + datetime.timedelta(seconds=1)
  150. # Just set max_age - the max_age logic will set expires.
  151. expires = None
  152. max_age = max(0, delta.days * 86400 + delta.seconds)
  153. else:
  154. self.cookies[key]['expires'] = expires
  155. else:
  156. self.cookies[key]['expires'] = ''
  157. if max_age is not None:
  158. self.cookies[key]['max-age'] = max_age
  159. # IE requires expires, so set it if hasn't been already.
  160. if not expires:
  161. self.cookies[key]['expires'] = http_date(time.time() + max_age)
  162. if path is not None:
  163. self.cookies[key]['path'] = path
  164. if domain is not None:
  165. self.cookies[key]['domain'] = domain
  166. if secure:
  167. self.cookies[key]['secure'] = True
  168. if httponly:
  169. self.cookies[key]['httponly'] = True
  170. if samesite:
  171. if samesite.lower() not in ('lax', 'strict'):
  172. raise ValueError('samesite must be "lax" or "strict".')
  173. self.cookies[key]['samesite'] = samesite
  174. def setdefault(self, key, value):
  175. """Set a header unless it has already been set."""
  176. if key not in self:
  177. self[key] = value
  178. def set_signed_cookie(self, key, value, salt='', **kwargs):
  179. value = signing.get_cookie_signer(salt=key + salt).sign(value)
  180. return self.set_cookie(key, value, **kwargs)
  181. def delete_cookie(self, key, path='/', domain=None):
  182. # Most browsers ignore the Set-Cookie header if the cookie name starts
  183. # with __Host- or __Secure- and the cookie doesn't use the secure flag.
  184. secure = key.startswith(('__Secure-', '__Host-'))
  185. self.set_cookie(
  186. key, max_age=0, path=path, domain=domain, secure=secure,
  187. expires='Thu, 01 Jan 1970 00:00:00 GMT',
  188. )
  189. # Common methods used by subclasses
  190. def make_bytes(self, value):
  191. """Turn a value into a bytestring encoded in the output charset."""
  192. # Per PEP 3333, this response body must be bytes. To avoid returning
  193. # an instance of a subclass, this function returns `bytes(value)`.
  194. # This doesn't make a copy when `value` already contains bytes.
  195. # Handle string types -- we can't rely on force_bytes here because:
  196. # - Python attempts str conversion first
  197. # - when self._charset != 'utf-8' it re-encodes the content
  198. if isinstance(value, bytes):
  199. return bytes(value)
  200. if isinstance(value, str):
  201. return bytes(value.encode(self.charset))
  202. # Handle non-string types.
  203. return str(value).encode(self.charset)
  204. # These methods partially implement the file-like object interface.
  205. # See https://docs.python.org/library/io.html#io.IOBase
  206. # The WSGI server must call this method upon completion of the request.
  207. # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html
  208. def close(self):
  209. for closable in self._closable_objects:
  210. try:
  211. closable.close()
  212. except Exception:
  213. pass
  214. self.closed = True
  215. signals.request_finished.send(sender=self._handler_class)
  216. def write(self, content):
  217. raise IOError("This %s instance is not writable" % self.__class__.__name__)
  218. def flush(self):
  219. pass
  220. def tell(self):
  221. raise IOError("This %s instance cannot tell its position" % self.__class__.__name__)
  222. # These methods partially implement a stream-like object interface.
  223. # See https://docs.python.org/library/io.html#io.IOBase
  224. def readable(self):
  225. return False
  226. def seekable(self):
  227. return False
  228. def writable(self):
  229. return False
  230. def writelines(self, lines):
  231. raise IOError("This %s instance is not writable" % self.__class__.__name__)
  232. class HttpResponse(HttpResponseBase):
  233. """
  234. An HTTP response class with a string as content.
  235. This content that can be read, appended to, or replaced.
  236. """
  237. streaming = False
  238. def __init__(self, content=b'', *args, **kwargs):
  239. super().__init__(*args, **kwargs)
  240. # Content is a bytestring. See the `content` property methods.
  241. self.content = content
  242. def __repr__(self):
  243. return '<%(cls)s status_code=%(status_code)d%(content_type)s>' % {
  244. 'cls': self.__class__.__name__,
  245. 'status_code': self.status_code,
  246. 'content_type': self._content_type_for_repr,
  247. }
  248. def serialize(self):
  249. """Full HTTP message, including headers, as a bytestring."""
  250. return self.serialize_headers() + b'\r\n\r\n' + self.content
  251. __bytes__ = serialize
  252. @property
  253. def content(self):
  254. return b''.join(self._container)
  255. @content.setter
  256. def content(self, value):
  257. # Consume iterators upon assignment to allow repeated iteration.
  258. if hasattr(value, '__iter__') and not isinstance(value, (bytes, str)):
  259. content = b''.join(self.make_bytes(chunk) for chunk in value)
  260. if hasattr(value, 'close'):
  261. try:
  262. value.close()
  263. except Exception:
  264. pass
  265. else:
  266. content = self.make_bytes(value)
  267. # Create a list of properly encoded bytestrings to support write().
  268. self._container = [content]
  269. def __iter__(self):
  270. return iter(self._container)
  271. def write(self, content):
  272. self._container.append(self.make_bytes(content))
  273. def tell(self):
  274. return len(self.content)
  275. def getvalue(self):
  276. return self.content
  277. def writable(self):
  278. return True
  279. def writelines(self, lines):
  280. for line in lines:
  281. self.write(line)
  282. class StreamingHttpResponse(HttpResponseBase):
  283. """
  284. A streaming HTTP response class with an iterator as content.
  285. This should only be iterated once, when the response is streamed to the
  286. client. However, it can be appended to or replaced with a new iterator
  287. that wraps the original content (or yields entirely new content).
  288. """
  289. streaming = True
  290. def __init__(self, streaming_content=(), *args, **kwargs):
  291. super().__init__(*args, **kwargs)
  292. # `streaming_content` should be an iterable of bytestrings.
  293. # See the `streaming_content` property methods.
  294. self.streaming_content = streaming_content
  295. @property
  296. def content(self):
  297. raise AttributeError(
  298. "This %s instance has no `content` attribute. Use "
  299. "`streaming_content` instead." % self.__class__.__name__
  300. )
  301. @property
  302. def streaming_content(self):
  303. return map(self.make_bytes, self._iterator)
  304. @streaming_content.setter
  305. def streaming_content(self, value):
  306. self._set_streaming_content(value)
  307. def _set_streaming_content(self, value):
  308. # Ensure we can never iterate on "value" more than once.
  309. self._iterator = iter(value)
  310. if hasattr(value, 'close'):
  311. self._closable_objects.append(value)
  312. def __iter__(self):
  313. return self.streaming_content
  314. def getvalue(self):
  315. return b''.join(self.streaming_content)
  316. class FileResponse(StreamingHttpResponse):
  317. """
  318. A streaming HTTP response class optimized for files.
  319. """
  320. block_size = 4096
  321. def __init__(self, *args, as_attachment=False, filename='', **kwargs):
  322. self.as_attachment = as_attachment
  323. self.filename = filename
  324. super().__init__(*args, **kwargs)
  325. def _set_streaming_content(self, value):
  326. if not hasattr(value, 'read'):
  327. self.file_to_stream = None
  328. return super()._set_streaming_content(value)
  329. self.file_to_stream = filelike = value
  330. if hasattr(filelike, 'close'):
  331. self._closable_objects.append(filelike)
  332. value = iter(lambda: filelike.read(self.block_size), b'')
  333. self.set_headers(filelike)
  334. super()._set_streaming_content(value)
  335. def set_headers(self, filelike):
  336. """
  337. Set some common response headers (Content-Length, Content-Type, and
  338. Content-Disposition) based on the `filelike` response content.
  339. """
  340. encoding_map = {
  341. 'bzip2': 'application/x-bzip',
  342. 'gzip': 'application/gzip',
  343. 'xz': 'application/x-xz',
  344. }
  345. filename = getattr(filelike, 'name', None)
  346. filename = filename if (isinstance(filename, str) and filename) else self.filename
  347. if os.path.isabs(filename):
  348. self['Content-Length'] = os.path.getsize(filelike.name)
  349. elif hasattr(filelike, 'getbuffer'):
  350. self['Content-Length'] = filelike.getbuffer().nbytes
  351. if self.get('Content-Type', '').startswith(settings.DEFAULT_CONTENT_TYPE):
  352. if filename:
  353. content_type, encoding = mimetypes.guess_type(filename)
  354. # Encoding isn't set to prevent browsers from automatically
  355. # uncompressing files.
  356. content_type = encoding_map.get(encoding, content_type)
  357. self['Content-Type'] = content_type or 'application/octet-stream'
  358. else:
  359. self['Content-Type'] = 'application/octet-stream'
  360. if self.as_attachment:
  361. filename = self.filename or os.path.basename(filename)
  362. if filename:
  363. try:
  364. filename.encode('ascii')
  365. file_expr = 'filename="{}"'.format(filename)
  366. except UnicodeEncodeError:
  367. file_expr = "filename*=utf-8''{}".format(quote(filename))
  368. self['Content-Disposition'] = 'attachment; {}'.format(file_expr)
  369. class HttpResponseRedirectBase(HttpResponse):
  370. allowed_schemes = ['http', 'https', 'ftp']
  371. def __init__(self, redirect_to, *args, **kwargs):
  372. super().__init__(*args, **kwargs)
  373. self['Location'] = iri_to_uri(redirect_to)
  374. parsed = urlparse(str(redirect_to))
  375. if parsed.scheme and parsed.scheme not in self.allowed_schemes:
  376. raise DisallowedRedirect("Unsafe redirect to URL with protocol '%s'" % parsed.scheme)
  377. url = property(lambda self: self['Location'])
  378. def __repr__(self):
  379. return '<%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s">' % {
  380. 'cls': self.__class__.__name__,
  381. 'status_code': self.status_code,
  382. 'content_type': self._content_type_for_repr,
  383. 'url': self.url,
  384. }
  385. class HttpResponseRedirect(HttpResponseRedirectBase):
  386. status_code = 302
  387. class HttpResponsePermanentRedirect(HttpResponseRedirectBase):
  388. status_code = 301
  389. class HttpResponseNotModified(HttpResponse):
  390. status_code = 304
  391. def __init__(self, *args, **kwargs):
  392. super().__init__(*args, **kwargs)
  393. del self['content-type']
  394. @HttpResponse.content.setter
  395. def content(self, value):
  396. if value:
  397. raise AttributeError("You cannot set content to a 304 (Not Modified) response")
  398. self._container = []
  399. class HttpResponseBadRequest(HttpResponse):
  400. status_code = 400
  401. class HttpResponseNotFound(HttpResponse):
  402. status_code = 404
  403. class HttpResponseForbidden(HttpResponse):
  404. status_code = 403
  405. class HttpResponseNotAllowed(HttpResponse):
  406. status_code = 405
  407. def __init__(self, permitted_methods, *args, **kwargs):
  408. super().__init__(*args, **kwargs)
  409. self['Allow'] = ', '.join(permitted_methods)
  410. def __repr__(self):
  411. return '<%(cls)s [%(methods)s] status_code=%(status_code)d%(content_type)s>' % {
  412. 'cls': self.__class__.__name__,
  413. 'status_code': self.status_code,
  414. 'content_type': self._content_type_for_repr,
  415. 'methods': self['Allow'],
  416. }
  417. class HttpResponseGone(HttpResponse):
  418. status_code = 410
  419. class HttpResponseServerError(HttpResponse):
  420. status_code = 500
  421. class Http404(Exception):
  422. pass
  423. class JsonResponse(HttpResponse):
  424. """
  425. An HTTP response class that consumes data to be serialized to JSON.
  426. :param data: Data to be dumped into json. By default only ``dict`` objects
  427. are allowed to be passed due to a security flaw before EcmaScript 5. See
  428. the ``safe`` parameter for more information.
  429. :param encoder: Should be a json encoder class. Defaults to
  430. ``django.core.serializers.json.DjangoJSONEncoder``.
  431. :param safe: Controls if only ``dict`` objects may be serialized. Defaults
  432. to ``True``.
  433. :param json_dumps_params: A dictionary of kwargs passed to json.dumps().
  434. """
  435. def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
  436. json_dumps_params=None, **kwargs):
  437. if safe and not isinstance(data, dict):
  438. raise TypeError(
  439. 'In order to allow non-dict objects to be serialized set the '
  440. 'safe parameter to False.'
  441. )
  442. if json_dumps_params is None:
  443. json_dumps_params = {}
  444. kwargs.setdefault('content_type', 'application/json')
  445. data = json.dumps(data, cls=encoder, **json_dumps_params)
  446. super().__init__(content=data, **kwargs)