2017-01-07 19:11:46 +08:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
from urllib.request import url2pathname
|
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.staticfiles import utils
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.contrib.staticfiles.views import serve
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.handlers.wsgi import WSGIHandler, get_path_info
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
class StaticFilesHandler(WSGIHandler):
|
|
|
|
"""
|
|
|
|
WSGI middleware that intercepts calls to the static files directory, as
|
2010-11-17 23:36:26 +08:00
|
|
|
defined by the STATIC_URL setting, and serves those files.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2014-07-05 20:43:12 +08:00
|
|
|
# May be used to differentiate between handler types (e.g. in a
|
|
|
|
# request_finished signal)
|
|
|
|
handles_files = True
|
|
|
|
|
2013-12-02 03:12:48 +08:00
|
|
|
def __init__(self, application):
|
2010-10-20 09:33:24 +08:00
|
|
|
self.application = application
|
2010-11-17 23:36:26 +08:00
|
|
|
self.base_url = urlparse(self.get_base_url())
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2017-08-19 03:18:24 +08:00
|
|
|
def load_middleware(self):
|
|
|
|
# Middleware are already loaded for self.application; no need to reload
|
|
|
|
# them for self.
|
|
|
|
pass
|
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
def get_base_url(self):
|
2011-02-01 22:57:10 +08:00
|
|
|
utils.check_settings()
|
2010-11-17 23:36:26 +08:00
|
|
|
return settings.STATIC_URL
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
def _should_handle(self, path):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Check if the path should be handled. Ignore the path if:
|
2010-11-17 23:36:26 +08:00
|
|
|
* the host is provided as part of the base_url
|
2010-11-12 05:43:49 +08:00
|
|
|
* the request's path isn't under the media path (or equal)
|
|
|
|
"""
|
2011-07-05 00:19:54 +08:00
|
|
|
return path.startswith(self.base_url[2]) and not self.base_url[1]
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
def file_path(self, url):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the relative path to the media file on disk for the given URL.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2010-11-17 23:36:26 +08:00
|
|
|
relative_url = url[len(self.base_url[2]):]
|
2012-07-20 21:36:52 +08:00
|
|
|
return url2pathname(relative_url)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
def serve(self, request):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Serve the request path."""
|
2010-11-12 05:43:49 +08:00
|
|
|
return serve(request, self.file_path(request.path), insecure=True)
|
|
|
|
|
|
|
|
def get_response(self, request):
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
if self._should_handle(request.path):
|
|
|
|
try:
|
|
|
|
return self.serve(request)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Http404 as e:
|
2010-11-12 05:43:49 +08:00
|
|
|
if settings.DEBUG:
|
|
|
|
from django.views import debug
|
|
|
|
return debug.technical_404_response(request, e)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_response(request)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
2012-12-17 17:49:26 +08:00
|
|
|
if not self._should_handle(get_path_info(environ)):
|
2010-10-20 09:33:24 +08:00
|
|
|
return self.application(environ, start_response)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__call__(environ, start_response)
|