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.

http.py 14KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import cgi
  2. import codecs
  3. import logging
  4. import sys
  5. import tempfile
  6. import traceback
  7. from django import http
  8. from django.conf import settings
  9. from django.core import signals
  10. from django.core.exceptions import RequestDataTooBig
  11. from django.core.handlers import base
  12. from django.http import FileResponse, HttpResponse, HttpResponseServerError
  13. from django.urls import set_script_prefix
  14. from django.utils.functional import cached_property
  15. from asgiref.sync import async_to_sync, sync_to_async
  16. from channels.exceptions import RequestAborted, RequestTimeout
  17. logger = logging.getLogger("django.request")
  18. class AsgiRequest(http.HttpRequest):
  19. """
  20. Custom request subclass that decodes from an ASGI-standard request
  21. dict, and wraps request body handling.
  22. """
  23. # Number of seconds until a Request gives up on trying to read a request
  24. # body and aborts.
  25. body_receive_timeout = 60
  26. def __init__(self, scope, stream):
  27. self.scope = scope
  28. self._content_length = 0
  29. self._post_parse_error = False
  30. self._read_started = False
  31. self.resolver_match = None
  32. self.script_name = self.scope.get("root_path", "")
  33. if self.script_name and scope["path"].startswith(self.script_name):
  34. # TODO: Better is-prefix checking, slash handling?
  35. self.path_info = scope["path"][len(self.script_name) :]
  36. else:
  37. self.path_info = scope["path"]
  38. # django path is different from asgi scope path args, it should combine with script name
  39. if self.script_name:
  40. self.path = "%s/%s" % (
  41. self.script_name.rstrip("/"),
  42. self.path_info.replace("/", "", 1),
  43. )
  44. else:
  45. self.path = scope["path"]
  46. # HTTP basics
  47. self.method = self.scope["method"].upper()
  48. # fix https://github.com/django/channels/issues/622
  49. query_string = self.scope.get("query_string", "")
  50. if isinstance(query_string, bytes):
  51. query_string = query_string.decode("utf-8")
  52. self.META = {
  53. "REQUEST_METHOD": self.method,
  54. "QUERY_STRING": query_string,
  55. "SCRIPT_NAME": self.script_name,
  56. "PATH_INFO": self.path_info,
  57. # Old code will need these for a while
  58. "wsgi.multithread": True,
  59. "wsgi.multiprocess": True,
  60. }
  61. if self.scope.get("client", None):
  62. self.META["REMOTE_ADDR"] = self.scope["client"][0]
  63. self.META["REMOTE_HOST"] = self.META["REMOTE_ADDR"]
  64. self.META["REMOTE_PORT"] = self.scope["client"][1]
  65. if self.scope.get("server", None):
  66. self.META["SERVER_NAME"] = self.scope["server"][0]
  67. self.META["SERVER_PORT"] = str(self.scope["server"][1])
  68. else:
  69. self.META["SERVER_NAME"] = "unknown"
  70. self.META["SERVER_PORT"] = "0"
  71. # Handle old style-headers for a transition period
  72. if "headers" in self.scope and isinstance(self.scope["headers"], dict):
  73. self.scope["headers"] = [
  74. (x.encode("latin1"), y) for x, y in self.scope["headers"].items()
  75. ]
  76. # Headers go into META
  77. for name, value in self.scope.get("headers", []):
  78. name = name.decode("latin1")
  79. if name == "content-length":
  80. corrected_name = "CONTENT_LENGTH"
  81. elif name == "content-type":
  82. corrected_name = "CONTENT_TYPE"
  83. else:
  84. corrected_name = "HTTP_%s" % name.upper().replace("-", "_")
  85. # HTTPbis say only ASCII chars are allowed in headers, but we 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 we find it
  91. if "CONTENT_TYPE" in self.META:
  92. self.content_type, self.content_params = cgi.parse_header(
  93. self.META["CONTENT_TYPE"]
  94. )
  95. if "charset" in self.content_params:
  96. try:
  97. codecs.lookup(self.content_params["charset"])
  98. except LookupError:
  99. pass
  100. else:
  101. self.encoding = self.content_params["charset"]
  102. else:
  103. self.content_type, self.content_params = "", {}
  104. # Pull out content length info
  105. if self.META.get("CONTENT_LENGTH", None):
  106. try:
  107. self._content_length = int(self.META["CONTENT_LENGTH"])
  108. except (ValueError, TypeError):
  109. pass
  110. # Body handling
  111. self._stream = stream
  112. # Other bits
  113. self.resolver_match = None
  114. @cached_property
  115. def GET(self):
  116. return http.QueryDict(self.scope.get("query_string", ""))
  117. def _get_scheme(self):
  118. return self.scope.get("scheme", "http")
  119. def _get_post(self):
  120. if not hasattr(self, "_post"):
  121. self._load_post_and_files()
  122. return self._post
  123. def _set_post(self, post):
  124. self._post = post
  125. def _get_files(self):
  126. if not hasattr(self, "_files"):
  127. self._load_post_and_files()
  128. return self._files
  129. POST = property(_get_post, _set_post)
  130. FILES = property(_get_files)
  131. @cached_property
  132. def COOKIES(self):
  133. return http.parse_cookie(self.META.get("HTTP_COOKIE", ""))
  134. class AsgiHandler(base.BaseHandler):
  135. """
  136. Handler for ASGI requests for the view system only (it will have got here
  137. after traversing the dispatch-by-channel-name system, which decides it's
  138. a HTTP request)
  139. You can also manually construct it with a get_response callback if you
  140. want to run a single Django view yourself. If you do this, though, it will
  141. not do any URL routing or middleware (Channels uses it for staticfiles'
  142. serving code)
  143. """
  144. request_class = AsgiRequest
  145. # Size to chunk response bodies into for multiple response messages
  146. chunk_size = 512 * 1024
  147. def __init__(self, scope):
  148. if scope["type"] != "http":
  149. raise ValueError(
  150. "The AsgiHandler can only handle HTTP connections, not %s"
  151. % scope["type"]
  152. )
  153. super(AsgiHandler, self).__init__()
  154. self.scope = scope
  155. self.load_middleware()
  156. async def __call__(self, receive, send):
  157. """
  158. Async entrypoint - uses the sync_to_async wrapper to run things in a
  159. threadpool.
  160. """
  161. self.send = async_to_sync(send)
  162. # Receive the HTTP request body as a stream object.
  163. try:
  164. body_stream = await self.read_body(receive)
  165. except RequestAborted:
  166. return
  167. # Launch into body handling (and a synchronous subthread).
  168. await self.handle(body_stream)
  169. async def read_body(self, receive):
  170. """Reads a HTTP body from an ASGI connection."""
  171. # Use the tempfile that auto rolls-over to a disk file as it fills up.
  172. body_file = tempfile.SpooledTemporaryFile(
  173. max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode="w+b"
  174. )
  175. while True:
  176. message = await receive()
  177. if message["type"] == "http.disconnect":
  178. # Early client disconnect.
  179. raise RequestAborted()
  180. # Add a body chunk from the message, if provided.
  181. if "body" in message:
  182. body_file.write(message["body"])
  183. # Quit out if that's the end.
  184. if not message.get("more_body", False):
  185. break
  186. body_file.seek(0)
  187. return body_file
  188. @sync_to_async
  189. def handle(self, body):
  190. """
  191. Synchronous message processing.
  192. """
  193. # Set script prefix from message root_path, turning None into empty string
  194. script_prefix = self.scope.get("root_path", "") or ""
  195. if settings.FORCE_SCRIPT_NAME:
  196. script_prefix = settings.FORCE_SCRIPT_NAME
  197. set_script_prefix(script_prefix)
  198. signals.request_started.send(sender=self.__class__, scope=self.scope)
  199. # Run request through view system
  200. try:
  201. request = self.request_class(self.scope, body)
  202. except UnicodeDecodeError:
  203. logger.warning(
  204. "Bad Request (UnicodeDecodeError)",
  205. exc_info=sys.exc_info(),
  206. extra={"status_code": 400},
  207. )
  208. response = http.HttpResponseBadRequest()
  209. except RequestTimeout:
  210. # Parsing the request failed, so the response is a Request Timeout error
  211. response = HttpResponse("408 Request Timeout (upload too slow)", status=408)
  212. except RequestAborted:
  213. # Client closed connection on us mid request. Abort!
  214. return
  215. except RequestDataTooBig:
  216. response = HttpResponse("413 Payload too large", status=413)
  217. else:
  218. response = self.get_response(request)
  219. # Fix chunk size on file responses
  220. if isinstance(response, FileResponse):
  221. response.block_size = 1024 * 512
  222. # Transform response into messages, which we yield back to caller
  223. for response_message in self.encode_response(response):
  224. self.send(response_message)
  225. # Close the response now we're done with it
  226. response.close()
  227. def handle_uncaught_exception(self, request, resolver, exc_info):
  228. """
  229. Last-chance handler for exceptions.
  230. """
  231. # There's no WSGI server to catch the exception further up if this fails,
  232. # so translate it into a plain text response.
  233. try:
  234. return super(AsgiHandler, self).handle_uncaught_exception(
  235. request, resolver, exc_info
  236. )
  237. except Exception:
  238. return HttpResponseServerError(
  239. traceback.format_exc() if settings.DEBUG else "Internal Server Error",
  240. content_type="text/plain",
  241. )
  242. def load_middleware(self):
  243. """
  244. Loads the Django middleware chain and caches it on the class.
  245. """
  246. # Because we create an AsgiHandler on every HTTP request
  247. # we need to preserve the Django middleware chain once we load it.
  248. if (
  249. hasattr(self.__class__, "_middleware_chain")
  250. and self.__class__._middleware_chain
  251. ):
  252. self._middleware_chain = self.__class__._middleware_chain
  253. self._view_middleware = self.__class__._view_middleware
  254. self._template_response_middleware = (
  255. self.__class__._template_response_middleware
  256. )
  257. self._exception_middleware = self.__class__._exception_middleware
  258. # Support additional arguments for Django 1.11 and 2.0.
  259. if hasattr(self.__class__, "_request_middleware"):
  260. self._request_middleware = self.__class__._request_middleware
  261. self._response_middleware = self.__class__._response_middleware
  262. else:
  263. super(AsgiHandler, self).load_middleware()
  264. self.__class__._middleware_chain = self._middleware_chain
  265. self.__class__._view_middleware = self._view_middleware
  266. self.__class__._template_response_middleware = (
  267. self._template_response_middleware
  268. )
  269. self.__class__._exception_middleware = self._exception_middleware
  270. # Support additional arguments for Django 1.11 and 2.0.
  271. if hasattr(self, "_request_middleware"):
  272. self.__class__._request_middleware = self._request_middleware
  273. self.__class__._response_middleware = self._response_middleware
  274. @classmethod
  275. def encode_response(cls, response):
  276. """
  277. Encodes a Django HTTP response into ASGI http.response message(s).
  278. """
  279. # Collect cookies into headers.
  280. # Note that we have to preserve header case as there are some non-RFC
  281. # compliant clients that want things like Content-Type correct. Ugh.
  282. response_headers = []
  283. for header, value in response.items():
  284. if isinstance(header, str):
  285. header = header.encode("ascii")
  286. if isinstance(value, str):
  287. value = value.encode("latin1")
  288. response_headers.append((bytes(header), bytes(value)))
  289. for c in response.cookies.values():
  290. response_headers.append(
  291. (b"Set-Cookie", c.output(header="").encode("ascii").strip())
  292. )
  293. # Make initial response message
  294. yield {
  295. "type": "http.response.start",
  296. "status": response.status_code,
  297. "headers": response_headers,
  298. }
  299. # Streaming responses need to be pinned to their iterator
  300. if response.streaming:
  301. # Access `__iter__` and not `streaming_content` directly in case
  302. # it has been overridden in a subclass.
  303. for part in response:
  304. for chunk, _ in cls.chunk_bytes(part):
  305. yield {
  306. "type": "http.response.body",
  307. "body": chunk,
  308. # We ignore "more" as there may be more parts; instead,
  309. # we use an empty final closing message with False.
  310. "more_body": True,
  311. }
  312. # Final closing message
  313. yield {"type": "http.response.body"}
  314. # Other responses just need chunking
  315. else:
  316. # Yield chunks of response
  317. for chunk, last in cls.chunk_bytes(response.content):
  318. yield {
  319. "type": "http.response.body",
  320. "body": chunk,
  321. "more_body": not last,
  322. }
  323. @classmethod
  324. def chunk_bytes(cls, data):
  325. """
  326. Chunks some data up so it can be sent in reasonable size messages.
  327. Yields (chunk, last_chunk) tuples.
  328. """
  329. position = 0
  330. if not data:
  331. yield data, True
  332. return
  333. while position < len(data):
  334. yield (
  335. data[position : position + cls.chunk_size],
  336. (position + cls.chunk_size) >= len(data),
  337. )
  338. position += cls.chunk_size