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.

handlers.py 2.2KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from urllib.parse import urlparse
  2. from urllib.request import url2pathname
  3. from django.conf import settings
  4. from django.contrib.staticfiles import utils
  5. from django.contrib.staticfiles.views import serve
  6. from django.core.handlers.exception import response_for_exception
  7. from django.core.handlers.wsgi import WSGIHandler, get_path_info
  8. class StaticFilesHandler(WSGIHandler):
  9. """
  10. WSGI middleware that intercepts calls to the static files directory, as
  11. defined by the STATIC_URL setting, and serves those files.
  12. """
  13. # May be used to differentiate between handler types (e.g. in a
  14. # request_finished signal)
  15. handles_files = True
  16. def __init__(self, application):
  17. self.application = application
  18. self.base_url = urlparse(self.get_base_url())
  19. super().__init__()
  20. def load_middleware(self):
  21. # Middleware are already loaded for self.application; no need to reload
  22. # them for self.
  23. pass
  24. def get_base_url(self):
  25. utils.check_settings()
  26. return settings.STATIC_URL
  27. def _should_handle(self, path):
  28. """
  29. Check if the path should be handled. Ignore the path if:
  30. * the host is provided as part of the base_url
  31. * the request's path isn't under the media path (or equal)
  32. """
  33. return path.startswith(self.base_url[2]) and not self.base_url[1]
  34. def file_path(self, url):
  35. """
  36. Return the relative path to the media file on disk for the given URL.
  37. """
  38. relative_url = url[len(self.base_url[2]):]
  39. return url2pathname(relative_url)
  40. def serve(self, request):
  41. """Serve the request path."""
  42. return serve(request, self.file_path(request.path), insecure=True)
  43. def get_response(self, request):
  44. from django.http import Http404
  45. if self._should_handle(request.path):
  46. try:
  47. return self.serve(request)
  48. except Http404 as e:
  49. return response_for_exception(request, e)
  50. return super().get_response(request)
  51. def __call__(self, environ, start_response):
  52. if not self._should_handle(get_path_info(environ)):
  53. return self.application(environ, start_response)
  54. return super().__call__(environ, start_response)