Debian Codegraph @

django/core/handlers/asgi.py

raw
1 import asyncio
2 import logging
3 import sys
4 import tempfile
5 import traceback
6 from collections import defaultdict
7 from contextlib import aclosing
8
9 from asgiref.sync import ThreadSensitiveContext, sync_to_async
10
11 from django.conf import settings
12 from django.core import signals
13 from django.core.exceptions import RequestAborted, RequestDataTooBig
14 from django.core.handlers import base
15 from django.http import (
16 FileResponse,
17 HttpRequest,
18 HttpResponse,
19 HttpResponseBadRequest,
20 HttpResponseServerError,
21 QueryDict,
22 parse_cookie,
23 )
24 from django.urls import set_script_prefix
25 from django.utils.functional import cached_property
26
27 logger = logging.getLogger("django.request")
28
29
30 def get_script_prefix(scope):
31 """
32 Return the script prefix to use from either the scope or a setting.
33 """
34 if settings.FORCE_SCRIPT_NAME:
35 return settings.FORCE_SCRIPT_NAME
36 return scope.get("root_path", "") or ""
37
38
39 class ASGIRequest(HttpRequest):
40 """
41 Custom request subclass that decodes from an ASGI-standard request dict
42 and wraps request body handling.
43 """
44
45 # Number of seconds until a Request gives up on trying to read a request
46 # body and aborts.
47 body_receive_timeout = 60
48
49 def __init__(self, scope, body_file):
50 self.scope = scope
51 self._post_parse_error = False
52 self._read_started = False
53 self.resolver_match = None
54 self.path = scope["path"]
55 self.script_name = get_script_prefix(scope)
56 if self.script_name:
57 # TODO: Better is-prefix checking, slash handling?
58 self.path_info = scope["path"].removeprefix(self.script_name)
59 else:
60 self.path_info = scope["path"]
61 # HTTP basics.
62 self.method = self.scope["method"].upper()
63 # Ensure query string is encoded correctly.
64 query_string = self.scope.get("query_string", "")
65 if isinstance(query_string, bytes):
66 query_string = query_string.decode()
67 self.META = {
68 "REQUEST_METHOD": self.method,
69 "QUERY_STRING": query_string,
70 "SCRIPT_NAME": self.script_name,
71 "PATH_INFO": self.path_info,
72 # WSGI-expecting code will need these for a while
73 "wsgi.multithread": True,
74 "wsgi.multiprocess": True,
75 }
76 if self.scope.get("client"):
77 self.META["REMOTE_ADDR"] = self.scope["client"][0]
78 self.META["REMOTE_HOST"] = self.META["REMOTE_ADDR"]
79 self.META["REMOTE_PORT"] = self.scope["client"][1]
80 if self.scope.get("server"):
81 self.META["SERVER_NAME"] = self.scope["server"][0]
82 self.META["SERVER_PORT"] = str(self.scope["server"][1])
83 else:
84 self.META["SERVER_NAME"] = "unknown"
85 self.META["SERVER_PORT"] = "0"
86 # Headers go into META.
87 _headers = defaultdict(list)
88 for name, value in self.scope.get("headers", []):
89 name = name.decode("latin1")
90 # Prevent spoofing via ambiguity between underscores and hyphens.
91 if "_" in name:
92 continue
93 if name == "content-length":
94 corrected_name = "CONTENT_LENGTH"
95 elif name == "content-type":
96 corrected_name = "CONTENT_TYPE"
97 else:
98 corrected_name = "HTTP_%s" % name.upper().replace("-", "_")
99 # HTTP/2 say only ASCII chars are allowed in headers, but decode
100 # latin1 just in case.
101 value = value.decode("latin1")
102 _headers[corrected_name].append(value)
103 self.META.update({name: ",".join(value) for name, value in _headers.items()})
104 # Pull out request encoding, if provided.
105 self._set_content_type_params(self.META)
106 # Directly assign the body file to be our stream.
107 self._stream = body_file
108 # Other bits.
109 self.resolver_match = None
110
111 @cached_property
112 def GET(self):
113 return QueryDict(self.META["QUERY_STRING"])
114
115 def _get_scheme(self):
116 return self.scope.get("scheme") or super()._get_scheme()
117
118 def _get_post(self):
119 if not hasattr(self, "_post"):
120 self._load_post_and_files()
121 return self._post
122
123 def _set_post(self, post):
124 self._post = post
125
126 def _get_files(self):
127 if not hasattr(self, "_files"):
128 self._load_post_and_files()
129 return self._files
130
131 POST = property(_get_post, _set_post)
132 FILES = property(_get_files)
133
134 @cached_property
135 def COOKIES(self):
136 return parse_cookie(self.META.get("HTTP_COOKIE", ""))
137
138 def close(self):
139 super().close()
140 self._stream.close()
141
142
143 class ASGIHandler(base.BaseHandler):
144 """Handler for ASGI requests."""
145
146 request_class = ASGIRequest
147 # Size to chunk response bodies into for multiple response messages.
148 chunk_size = 2**16
149
150 def __init__(self):
151 super().__init__()
152 self.load_middleware(is_async=True)
153
154 async def __call__(self, scope, receive, send):
155 """
156 Async entrypoint - parses the request and hands off to get_response.
157 """
158 # Serve only HTTP connections.
159 # FIXME: Allow to override this.
160 if scope["type"] != "http":
161 raise ValueError(
162 "Django can only handle ASGI/HTTP connections, not %s." % scope["type"]
163 )
164
165 async with ThreadSensitiveContext():
166 await self.handle(scope, receive, send)
167
168 async def handle(self, scope, receive, send):
169 """
170 Handles the ASGI request. Called via the __call__ method.
171 """
172 # Receive the HTTP request body as a stream object.
173 try:
174 body_file = await self.read_body(receive)
175 except RequestAborted:
176 return
177 # Request is complete and can be served.
178 set_script_prefix(get_script_prefix(scope))
179 await signals.request_started.asend(sender=self.__class__, scope=scope)
180 # Get the request and check for basic issues.
181 request, error_response = self.create_request(scope, body_file)
182 if request is None:
183 body_file.close()
184 await self.send_response(error_response, send)
185 await sync_to_async(error_response.close)()
186 return
187
188 async def process_request(request, send):
189 response = await self.run_get_response(request)
190 try:
191 await self.send_response(response, send)
192 except asyncio.CancelledError:
193 # Client disconnected during send_response (ignore exception).
194 pass
195
196 return response
197
198 # Try to catch a disconnect while getting response.
199 tasks = [
200 # Check the status of these tasks and (optionally) terminate them
201 # in this order. The listen_for_disconnect() task goes first
202 # because it should not raise unexpected errors that would prevent
203 # us from cancelling process_request().
204 asyncio.create_task(self.listen_for_disconnect(receive)),
205 asyncio.create_task(process_request(request, send)),
206 ]
207 await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
208 # Now wait on both tasks (they may have both finished by now).
209 for task in tasks:
210 if task.done():
211 try:
212 task.result()
213 except RequestAborted:
214 # Ignore client disconnects.
215 pass
216 except AssertionError:
217 body_file.close()
218 raise
219 else:
220 # Allow views to handle cancellation.
221 task.cancel()
222 try:
223 await task
224 except asyncio.CancelledError:
225 # Task re-raised the CancelledError as expected.
226 pass
227
228 try:
229 response = tasks[1].result()
230 except asyncio.CancelledError:
231 await signals.request_finished.asend(sender=self.__class__)
232 else:
233 await sync_to_async(response.close)()
234
235 body_file.close()
236
237 async def listen_for_disconnect(self, receive):
238 """Listen for disconnect from the client."""
239 message = await receive()
240 if message["type"] == "http.disconnect":
241 raise RequestAborted()
242 # This should never happen.
243 assert False, "Invalid ASGI message after request body: %s" % message["type"]
244
245 async def run_get_response(self, request):
246 """Get async response."""
247 # Use the async mode of BaseHandler.
248 response = await self.get_response_async(request)
249 response._handler_class = self.__class__
250 # Increase chunk size on file responses (ASGI servers handles low-level
251 # chunking).
252 if isinstance(response, FileResponse):
253 response.block_size = self.chunk_size
254 return response
255
256 async def read_body(self, receive):
257 """Reads an HTTP body from an ASGI connection."""
258 # Use the tempfile that auto rolls-over to a disk file as it fills up.
259 body_file = tempfile.SpooledTemporaryFile(
260 max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode="w+b"
261 )
262 while True:
263 message = await receive()
264 if message["type"] == "http.disconnect":
265 body_file.close()
266 # Early client disconnect.
267 raise RequestAborted()
268 # Add a body chunk from the message, if provided.
269 if "body" in message:
270 body_file.write(message["body"])
271 # Quit out if that's the end.
272 if not message.get("more_body", False):
273 break
274 body_file.seek(0)
275 return body_file
276
277 def create_request(self, scope, body_file):
278 """
279 Create the Request object and returns either (request, None) or
280 (None, response) if there is an error response.
281 """
282 try:
283 return self.request_class(scope, body_file), None
284 except UnicodeDecodeError:
285 logger.warning(
286 "Bad Request (UnicodeDecodeError)",
287 exc_info=sys.exc_info(),
288 extra={"status_code": 400},
289 )
290 return None, HttpResponseBadRequest()
291 except RequestDataTooBig:
292 return None, HttpResponse("413 Payload too large", status=413)
293
294 def handle_uncaught_exception(self, request, resolver, exc_info):
295 """Last-chance handler for exceptions."""
296 # There's no WSGI server to catch the exception further up
297 # if this fails, so translate it into a plain text response.
298 try:
299 return super().handle_uncaught_exception(request, resolver, exc_info)
300 except Exception:
301 return HttpResponseServerError(
302 traceback.format_exc() if settings.DEBUG else "Internal Server Error",
303 content_type="text/plain",
304 )
305
306 async def send_response(self, response, send):
307 """Encode and send a response out over ASGI."""
308 # Collect cookies into headers. Have to preserve header case as there
309 # are some non-RFC compliant clients that require e.g. Content-Type.
310 response_headers = []
311 for header, value in response.items():
312 if isinstance(header, str):
313 header = header.encode("ascii")
314 if isinstance(value, str):
315 value = value.encode("latin1")
316 response_headers.append((bytes(header), bytes(value)))
317 for c in response.cookies.values():
318 response_headers.append(
319 (b"Set-Cookie", c.output(header="").encode("ascii").strip())
320 )
321 # Initial response message.
322 await send(
323 {
324 "type": "http.response.start",
325 "status": response.status_code,
326 "headers": response_headers,
327 }
328 )
329 # Streaming responses need to be pinned to their iterator.
330 if response.streaming:
331 # - Consume via `__aiter__` and not `streaming_content` directly, to
332 # allow mapping of a sync iterator.
333 # - Use aclosing() when consuming aiter.
334 # See https://github.com/python/cpython/commit/6e8dcda
335 async with aclosing(aiter(response)) as content:
336 async for part in content:
337 for chunk, _ in self.chunk_bytes(part):
338 await send(
339 {
340 "type": "http.response.body",
341 "body": chunk,
342 # Ignore "more" as there may be more parts; instead,
343 # use an empty final closing message with False.
344 "more_body": True,
345 }
346 )
347 # Final closing message.
348 await send({"type": "http.response.body"})
349 # Other responses just need chunking.
350 else:
351 # Yield chunks of response.
352 for chunk, last in self.chunk_bytes(response.content):
353 await send(
354 {
355 "type": "http.response.body",
356 "body": chunk,
357 "more_body": not last,
358 }
359 )
360
361 @classmethod
362 def chunk_bytes(cls, data):
363 """
364 Chunks some data up so it can be sent in reasonable size messages.
365 Yields (chunk, last_chunk) tuples.
366 """
367 position = 0
368 if not data:
369 yield data, True
370 return
371 while position < len(data):
372 yield (
373 data[position : position + cls.chunk_size],
374 (position + cls.chunk_size) >= len(data),
375 )
376 position += cls.chunk_size