Ticket #11582: admin-media.diff

File admin-media.diff, 3.5 KB (added by Alex Gaynor, 15 years ago)
  • django/core/servers/basehttp.py

    diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
    index f7c0a77..d6464ba 100644
    a b class AdminMediaHandler(object):  
    642642        return safe_join(self.media_dir, relative_path)
    643643
    644644    def __call__(self, environ, start_response):
    645         import os.path
     645        from django.core.handlers.wsgi import STATUS_CODE_TEXT
     646        from django.http import Http404
     647        from django.views.static import serve
    646648
    647649        # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
    648650        # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
    class AdminMediaHandler(object):  
    650652            or not environ['PATH_INFO'].startswith(self.media_url):
    651653            return self.application(environ, start_response)
    652654
    653         # Find the admin file and serve it up, if it exists and is readable.
     655        request = self.application.request_class(environ)
    654656        try:
    655             file_path = self.file_path(environ['PATH_INFO'])
    656         except ValueError: # Resulting file path was not valid.
     657            response = serve(request, environ['PATH_INFO'][len(self.media_url):], document_root=self.media_dir)
     658        except Http404:
    657659            status = '404 NOT FOUND'
    658             headers = {'Content-type': 'text/plain'}
    659             output = ['Page not found: %s' % environ['PATH_INFO']]
    660             start_response(status, headers.items())
    661             return output
    662         if not os.path.exists(file_path):
    663             status = '404 NOT FOUND'
    664             headers = {'Content-type': 'text/plain'}
    665             output = ['Page not found: %s' % environ['PATH_INFO']]
    666         else:
    667             try:
    668                 fp = open(file_path, 'rb')
    669             except IOError:
    670                 status = '401 UNAUTHORIZED'
    671                 headers = {'Content-type': 'text/plain'}
    672                 output = ['Permission denied: %s' % environ['PATH_INFO']]
    673             else:
    674                 # This is a very simple implementation of conditional GET with
    675                 # the Last-Modified header. It makes media files a bit speedier
    676                 # because the files are only read off disk for the first
    677                 # request (assuming the browser/client supports conditional
    678                 # GET).
    679                 mtime = http_date(os.stat(file_path)[stat.ST_MTIME])
    680                 headers = {'Last-Modified': mtime}
    681                 if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime:
    682                     status = '304 NOT MODIFIED'
    683                     output = []
    684                 else:
    685                     status = '200 OK'
    686                     mime_type = mimetypes.guess_type(file_path)[0]
    687                     if mime_type:
    688                         headers['Content-Type'] = mime_type
    689                     output = [fp.read()]
    690                     fp.close()
    691         start_response(status, headers.items())
    692         return output
     660            start_response(status, {'Content-type': 'text/plain'}.items())
     661            return [str('Page not found: %s' % environ['PATH_INFO'])]
     662        status_text = STATUS_CODE_TEXT[response.status_code]
     663        status = '%s %s' % (response.status_code, status_text)
     664        response_headers = [(str(k), str(v)) for k, v in response.items()]
     665        for c in response.cookies.values():
     666            response_headers.append(('Set-Cookie', str(c.output(header=''))))
     667        start_response(status, response_headers)
     668        return response
     669
     670
    693671
    694672def run(addr, port, wsgi_handler):
    695673    server_address = (addr, port)
Back to Top