Ticket #11582: admin-media.2.diff

File admin-media.2.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 415756c..adffe0f 100644
    a b class AdminMediaHandler(object):  
    663663        return safe_join(self.media_dir, relative_path)
    664664
    665665    def __call__(self, environ, start_response):
    666         import os.path
     666        from django.core.handlers.wsgi import STATUS_CODE_TEXT
     667        from django.http import Http404
     668        from django.views.static import serve
    667669
    668670        # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
    669671        # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
    class AdminMediaHandler(object):  
    671673            or not environ['PATH_INFO'].startswith(self.media_url):
    672674            return self.application(environ, start_response)
    673675
    674         # Find the admin file and serve it up, if it exists and is readable.
     676        request = self.application.request_class(environ)
    675677        try:
    676             file_path = self.file_path(environ['PATH_INFO'])
    677         except ValueError: # Resulting file path was not valid.
     678            response = serve(request, environ['PATH_INFO'][len(self.media_url):], document_root=self.media_dir)
     679        except Http404:
    678680            status = '404 NOT FOUND'
    679             headers = {'Content-type': 'text/plain'}
    680             output = ['Page not found: %s' % environ['PATH_INFO']]
    681             start_response(status, headers.items())
    682             return output
    683         if not os.path.exists(file_path):
    684             status = '404 NOT FOUND'
    685             headers = {'Content-type': 'text/plain'}
    686             output = ['Page not found: %s' % environ['PATH_INFO']]
    687         else:
    688             try:
    689                 fp = open(file_path, 'rb')
    690             except IOError:
    691                 status = '401 UNAUTHORIZED'
    692                 headers = {'Content-type': 'text/plain'}
    693                 output = ['Permission denied: %s' % environ['PATH_INFO']]
    694             else:
    695                 # This is a very simple implementation of conditional GET with
    696                 # the Last-Modified header. It makes media files a bit speedier
    697                 # because the files are only read off disk for the first
    698                 # request (assuming the browser/client supports conditional
    699                 # GET).
    700                 mtime = http_date(os.stat(file_path)[stat.ST_MTIME])
    701                 headers = {'Last-Modified': mtime}
    702                 if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime:
    703                     status = '304 NOT MODIFIED'
    704                     output = []
    705                 else:
    706                     status = '200 OK'
    707                     mime_type = mimetypes.guess_type(file_path)[0]
    708                     if mime_type:
    709                         headers['Content-Type'] = mime_type
    710                     output = [fp.read()]
    711                     fp.close()
    712         start_response(status, headers.items())
    713         return output
     681            start_response(status, {'Content-type': 'text/plain'}.items())
     682            return [str('Page not found: %s' % environ['PATH_INFO'])]
     683        status_text = STATUS_CODE_TEXT[response.status_code]
     684        status = '%s %s' % (response.status_code, status_text)
     685        response_headers = [(str(k), str(v)) for k, v in response.items()]
     686        for c in response.cookies.values():
     687            response_headers.append(('Set-Cookie', str(c.output(header=''))))
     688        start_response(status, response_headers)
     689        return response
     690
     691
    714692
    715693def run(addr, port, wsgi_handler):
    716694    server_address = (addr, port)
Back to Top