Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 23KB

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