asgi.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import logging
  2. import sys
  3. import tempfile
  4. import traceback
  5. from asgiref.sync import ThreadSensitiveContext, sync_to_async
  6. from django.conf import settings
  7. from django.core import signals
  8. from django.core.exceptions import RequestAborted, RequestDataTooBig
  9. from django.core.handlers import base
  10. from django.http import (
  11. FileResponse,
  12. HttpRequest,
  13. HttpResponse,
  14. HttpResponseBadRequest,
  15. HttpResponseServerError,
  16. QueryDict,
  17. parse_cookie,
  18. )
  19. from django.urls import set_script_prefix
  20. from django.utils.functional import cached_property
  21. logger = logging.getLogger("django.request")
  22. class ASGIRequest(HttpRequest):
  23. """
  24. Custom request subclass that decodes from an ASGI-standard request dict
  25. and wraps request body handling.
  26. """
  27. # Number of seconds until a Request gives up on trying to read a request
  28. # body and aborts.
  29. body_receive_timeout = 60
  30. def __init__(self, scope, body_file):
  31. self.scope = scope
  32. self._post_parse_error = False
  33. self._read_started = False
  34. self.resolver_match = None
  35. self.script_name = self.scope.get("root_path", "")
  36. if self.script_name and scope["path"].startswith(self.script_name):
  37. # TODO: Better is-prefix checking, slash handling?
  38. self.path_info = scope["path"][len(self.script_name) :]
  39. else:
  40. self.path_info = scope["path"]
  41. # The Django path is different from ASGI scope path args, it should
  42. # combine with script name.
  43. if self.script_name:
  44. self.path = "%s/%s" % (
  45. self.script_name.rstrip("/"),
  46. self.path_info.replace("/", "", 1),
  47. )
  48. else:
  49. self.path = scope["path"]
  50. # HTTP basics.
  51. self.method = self.scope["method"].upper()
  52. # Ensure query string is encoded correctly.
  53. query_string = self.scope.get("query_string", "")
  54. if isinstance(query_string, bytes):
  55. query_string = query_string.decode()
  56. self.META = {
  57. "REQUEST_METHOD": self.method,
  58. "QUERY_STRING": query_string,
  59. "SCRIPT_NAME": self.script_name,
  60. "PATH_INFO": self.path_info,
  61. # WSGI-expecting code will need these for a while
  62. "wsgi.multithread": True,
  63. "wsgi.multiprocess": True,
  64. }
  65. if self.scope.get("client"):
  66. self.META["REMOTE_ADDR"] = self.scope["client"][0]
  67. self.META["REMOTE_HOST"] = self.META["REMOTE_ADDR"]
  68. self.META["REMOTE_PORT"] = self.scope["client"][1]
  69. if self.scope.get("server"):
  70. self.META["SERVER_NAME"] = self.scope["server"][0]
  71. self.META["SERVER_PORT"] = str(self.scope["server"][1])
  72. else:
  73. self.META["SERVER_NAME"] = "unknown"
  74. self.META["SERVER_PORT"] = "0"
  75. # Headers go into META.
  76. for name, value in self.scope.get("headers", []):
  77. name = name.decode("latin1")
  78. if name == "content-length":
  79. corrected_name = "CONTENT_LENGTH"
  80. elif name == "content-type":
  81. corrected_name = "CONTENT_TYPE"
  82. else:
  83. corrected_name = "HTTP_%s" % name.upper().replace("-", "_")
  84. # HTTP/2 say only ASCII chars are allowed in headers, but decode
  85. # latin1 just in case.
  86. value = value.decode("latin1")
  87. if corrected_name in self.META:
  88. value = self.META[corrected_name] + "," + value
  89. self.META[corrected_name] = value
  90. # Pull out request encoding, if provided.
  91. self._set_content_type_params(self.META)
  92. # Directly assign the body file to be our stream.
  93. self._stream = body_file
  94. # Other bits.
  95. self.resolver_match = None
  96. @cached_property
  97. def GET(self):
  98. return QueryDict(self.META["QUERY_STRING"])
  99. def _get_scheme(self):
  100. return self.scope.get("scheme") or super()._get_scheme()
  101. def _get_post(self):
  102. if not hasattr(self, "_post"):
  103. self._load_post_and_files()
  104. return self._post
  105. def _set_post(self, post):
  106. self._post = post
  107. def _get_files(self):
  108. if not hasattr(self, "_files"):
  109. self._load_post_and_files()
  110. return self._files
  111. POST = property(_get_post, _set_post)
  112. FILES = property(_get_files)
  113. @cached_property
  114. def COOKIES(self):
  115. return parse_cookie(self.META.get("HTTP_COOKIE", ""))
  116. class ASGIHandler(base.BaseHandler):
  117. """Handler for ASGI requests."""
  118. request_class = ASGIRequest
  119. # Size to chunk response bodies into for multiple response messages.
  120. chunk_size = 2**16
  121. def __init__(self):
  122. super().__init__()
  123. self.load_middleware(is_async=True)
  124. async def __call__(self, scope, receive, send):
  125. """
  126. Async entrypoint - parses the request and hands off to get_response.
  127. """
  128. # Serve only HTTP connections.
  129. # FIXME: Allow to override this.
  130. if scope["type"] != "http":
  131. raise ValueError(
  132. "Django can only handle ASGI/HTTP connections, not %s." % scope["type"]
  133. )
  134. async with ThreadSensitiveContext():
  135. await self.handle(scope, receive, send)
  136. async def handle(self, scope, receive, send):
  137. """
  138. Handles the ASGI request. Called via the __call__ method.
  139. """
  140. # Receive the HTTP request body as a stream object.
  141. try:
  142. body_file = await self.read_body(receive)
  143. except RequestAborted:
  144. return
  145. # Request is complete and can be served.
  146. try:
  147. set_script_prefix(self.get_script_prefix(scope))
  148. await sync_to_async(signals.request_started.send, thread_sensitive=True)(
  149. sender=self.__class__, scope=scope
  150. )
  151. # Get the request and check for basic issues.
  152. request, error_response = self.create_request(scope, body_file)
  153. if request is None:
  154. await self.send_response(error_response, send)
  155. return
  156. # Get the response, using the async mode of BaseHandler.
  157. response = await self.get_response_async(request)
  158. response._handler_class = self.__class__
  159. finally:
  160. body_file.close()
  161. # Increase chunk size on file responses (ASGI servers handles low-level
  162. # chunking).
  163. if isinstance(response, FileResponse):
  164. response.block_size = self.chunk_size
  165. # Send the response.
  166. await self.send_response(response, send)
  167. async def read_body(self, receive):
  168. """Reads an HTTP body from an ASGI connection."""
  169. # Use the tempfile that auto rolls-over to a disk file as it fills up.
  170. body_file = tempfile.SpooledTemporaryFile(
  171. max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode="w+b"
  172. )
  173. while True:
  174. message = await receive()
  175. if message["type"] == "http.disconnect":
  176. body_file.close()
  177. # Early client disconnect.
  178. raise RequestAborted()
  179. # Add a body chunk from the message, if provided.
  180. if "body" in message:
  181. body_file.write(message["body"])
  182. # Quit out if that's the end.
  183. if not message.get("more_body", False):
  184. break
  185. body_file.seek(0)
  186. return body_file
  187. def create_request(self, scope, body_file):
  188. """
  189. Create the Request object and returns either (request, None) or
  190. (None, response) if there is an error response.
  191. """
  192. try:
  193. return self.request_class(scope, body_file), None
  194. except UnicodeDecodeError:
  195. logger.warning(
  196. "Bad Request (UnicodeDecodeError)",
  197. exc_info=sys.exc_info(),
  198. extra={"status_code": 400},
  199. )
  200. return None, HttpResponseBadRequest()
  201. except RequestDataTooBig:
  202. return None, HttpResponse("413 Payload too large", status=413)
  203. def handle_uncaught_exception(self, request, resolver, exc_info):
  204. """Last-chance handler for exceptions."""
  205. # There's no WSGI server to catch the exception further up
  206. # if this fails, so translate it into a plain text response.
  207. try:
  208. return super().handle_uncaught_exception(request, resolver, exc_info)
  209. except Exception:
  210. return HttpResponseServerError(
  211. traceback.format_exc() if settings.DEBUG else "Internal Server Error",
  212. content_type="text/plain",
  213. )
  214. async def send_response(self, response, send):
  215. """Encode and send a response out over ASGI."""
  216. # Collect cookies into headers. Have to preserve header case as there
  217. # are some non-RFC compliant clients that require e.g. Content-Type.
  218. response_headers = []
  219. for header, value in response.items():
  220. if isinstance(header, str):
  221. header = header.encode("ascii")
  222. if isinstance(value, str):
  223. value = value.encode("latin1")
  224. response_headers.append((bytes(header), bytes(value)))
  225. for c in response.cookies.values():
  226. response_headers.append(
  227. (b"Set-Cookie", c.output(header="").encode("ascii").strip())
  228. )
  229. # Initial response message.
  230. await send(
  231. {
  232. "type": "http.response.start",
  233. "status": response.status_code,
  234. "headers": response_headers,
  235. }
  236. )
  237. # Streaming responses need to be pinned to their iterator.
  238. if response.streaming:
  239. # Access `__iter__` and not `streaming_content` directly in case
  240. # it has been overridden in a subclass.
  241. for part in response:
  242. for chunk, _ in self.chunk_bytes(part):
  243. await send(
  244. {
  245. "type": "http.response.body",
  246. "body": chunk,
  247. # Ignore "more" as there may be more parts; instead,
  248. # use an empty final closing message with False.
  249. "more_body": True,
  250. }
  251. )
  252. # Final closing message.
  253. await send({"type": "http.response.body"})
  254. # Other responses just need chunking.
  255. else:
  256. # Yield chunks of response.
  257. for chunk, last in self.chunk_bytes(response.content):
  258. await send(
  259. {
  260. "type": "http.response.body",
  261. "body": chunk,
  262. "more_body": not last,
  263. }
  264. )
  265. await sync_to_async(response.close, thread_sensitive=True)()
  266. @classmethod
  267. def chunk_bytes(cls, data):
  268. """
  269. Chunks some data up so it can be sent in reasonable size messages.
  270. Yields (chunk, last_chunk) tuples.
  271. """
  272. position = 0
  273. if not data:
  274. yield data, True
  275. return
  276. while position < len(data):
  277. yield (
  278. data[position : position + cls.chunk_size],
  279. (position + cls.chunk_size) >= len(data),
  280. )
  281. position += cls.chunk_size
  282. def get_script_prefix(self, scope):
  283. """
  284. Return the script prefix to use from either the scope or a setting.
  285. """
  286. if settings.FORCE_SCRIPT_NAME:
  287. return settings.FORCE_SCRIPT_NAME
  288. return scope.get("root_path", "") or ""