Debian Codegraph @

django/http/response.py

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