Code

Ticket #11582: admin-media.2.diff

File admin-media.2.diff, 3.5 KB (added by Alex, 5 years ago)
Line 
1diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
2index 415756c..adffe0f 100644
3--- a/django/core/servers/basehttp.py
4+++ b/django/core/servers/basehttp.py
5@@ -663,7 +663,9 @@ class AdminMediaHandler(object):
6         return safe_join(self.media_dir, relative_path)
7 
8     def __call__(self, environ, start_response):
9-        import os.path
10+        from django.core.handlers.wsgi import STATUS_CODE_TEXT
11+        from django.http import Http404
12+        from django.views.static import serve
13 
14         # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
15         # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
16@@ -671,46 +673,22 @@ class AdminMediaHandler(object):
17             or not environ['PATH_INFO'].startswith(self.media_url):
18             return self.application(environ, start_response)
19 
20-        # Find the admin file and serve it up, if it exists and is readable.
21+        request = self.application.request_class(environ)
22         try:
23-            file_path = self.file_path(environ['PATH_INFO'])
24-        except ValueError: # Resulting file path was not valid.
25+            response = serve(request, environ['PATH_INFO'][len(self.media_url):], document_root=self.media_dir)
26+        except Http404:
27             status = '404 NOT FOUND'
28-            headers = {'Content-type': 'text/plain'}
29-            output = ['Page not found: %s' % environ['PATH_INFO']]
30-            start_response(status, headers.items())
31-            return output
32-        if not os.path.exists(file_path):
33-            status = '404 NOT FOUND'
34-            headers = {'Content-type': 'text/plain'}
35-            output = ['Page not found: %s' % environ['PATH_INFO']]
36-        else:
37-            try:
38-                fp = open(file_path, 'rb')
39-            except IOError:
40-                status = '401 UNAUTHORIZED'
41-                headers = {'Content-type': 'text/plain'}
42-                output = ['Permission denied: %s' % environ['PATH_INFO']]
43-            else:
44-                # This is a very simple implementation of conditional GET with
45-                # the Last-Modified header. It makes media files a bit speedier
46-                # because the files are only read off disk for the first
47-                # request (assuming the browser/client supports conditional
48-                # GET).
49-                mtime = http_date(os.stat(file_path)[stat.ST_MTIME])
50-                headers = {'Last-Modified': mtime}
51-                if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime:
52-                    status = '304 NOT MODIFIED'
53-                    output = []
54-                else:
55-                    status = '200 OK'
56-                    mime_type = mimetypes.guess_type(file_path)[0]
57-                    if mime_type:
58-                        headers['Content-Type'] = mime_type
59-                    output = [fp.read()]
60-                    fp.close()
61-        start_response(status, headers.items())
62-        return output
63+            start_response(status, {'Content-type': 'text/plain'}.items())
64+            return [str('Page not found: %s' % environ['PATH_INFO'])]
65+        status_text = STATUS_CODE_TEXT[response.status_code]
66+        status = '%s %s' % (response.status_code, status_text)
67+        response_headers = [(str(k), str(v)) for k, v in response.items()]
68+        for c in response.cookies.values():
69+            response_headers.append(('Set-Cookie', str(c.output(header=''))))
70+        start_response(status, response_headers)
71+        return response
72+
73+
74 
75 def run(addr, port, wsgi_handler):
76     server_address = (addr, port)