summaryrefslogtreecommitdiff
path: root/django/contrib
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2019-04-12 06:15:18 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-20 12:29:43 +0200
commita415ce70bef6d91036b00dd2c8544aed7aeeaaed (patch)
tree3583cef22e9b56d2ed52456ab586d9c47620bc51 /django/contrib
parentcce47ff65a4dd3786c049ec14ee889e128ca7de9 (diff)
Fixed #30451 -- Added ASGI handler and coroutine-safety.
This adds an ASGI handler, asgi.py file for the default project layout, a few async utilities and adds async-safety to many parts of Django.
Diffstat (limited to 'django/contrib')
-rw-r--r--django/contrib/staticfiles/handlers.py41
1 files changed, 33 insertions, 8 deletions
diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py
index b0aa66036d..711d8864ad 100644
--- a/django/contrib/staticfiles/handlers.py
+++ b/django/contrib/staticfiles/handlers.py
@@ -4,25 +4,20 @@ from urllib.request import url2pathname
from django.conf import settings
from django.contrib.staticfiles import utils
from django.contrib.staticfiles.views import serve
+from django.core.handlers.asgi import ASGIHandler
from django.core.handlers.exception import response_for_exception
from django.core.handlers.wsgi import WSGIHandler, get_path_info
from django.http import Http404
-class StaticFilesHandler(WSGIHandler):
+class StaticFilesHandlerMixin:
"""
- WSGI middleware that intercepts calls to the static files directory, as
- defined by the STATIC_URL setting, and serves those files.
+ Common methods used by WSGI and ASGI handlers.
"""
# May be used to differentiate between handler types (e.g. in a
# request_finished signal)
handles_files = True
- def __init__(self, application):
- self.application = application
- self.base_url = urlparse(self.get_base_url())
- super().__init__()
-
def load_middleware(self):
# Middleware are already loaded for self.application; no need to reload
# them for self.
@@ -57,7 +52,37 @@ class StaticFilesHandler(WSGIHandler):
except Http404 as e:
return response_for_exception(request, e)
+
+class StaticFilesHandler(StaticFilesHandlerMixin, WSGIHandler):
+ """
+ WSGI middleware that intercepts calls to the static files directory, as
+ defined by the STATIC_URL setting, and serves those files.
+ """
+ def __init__(self, application):
+ self.application = application
+ self.base_url = urlparse(self.get_base_url())
+ super().__init__()
+
def __call__(self, environ, start_response):
if not self._should_handle(get_path_info(environ)):
return self.application(environ, start_response)
return super().__call__(environ, start_response)
+
+
+class ASGIStaticFilesHandler(StaticFilesHandlerMixin, ASGIHandler):
+ """
+ ASGI application which wraps another and intercepts requests for static
+ files, passing them off to Django's static file serving.
+ """
+ def __init__(self, application):
+ self.application = application
+ self.base_url = urlparse(self.get_base_url())
+
+ async def __call__(self, scope, receive, send):
+ # Only even look at HTTP requests
+ if scope['type'] == 'http' and self._should_handle(scope['path']):
+ # Serve static content
+ # (the one thing super() doesn't do is __call__, apparently)
+ return await super().__call__(scope, receive, send)
+ # Hand off to the main app
+ return await self.application(scope, receive, send)