Fixed #8409 -- The runserver now uses conditional GET for admin media files, instead of reloading the files off disk for every request. Thanks for reporting, andylowry

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9055 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2008-09-17 05:42:12 +00:00
parent 7a80a9fa7d
commit cc7b0f986a
1 changed files with 18 additions and 7 deletions

View File

@ -11,6 +11,7 @@ from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import mimetypes import mimetypes
import os import os
import re import re
import stat
import sys import sys
import urllib import urllib
@ -648,13 +649,23 @@ class AdminMediaHandler(object):
headers = {'Content-type': 'text/plain'} headers = {'Content-type': 'text/plain'}
output = ['Permission denied: %s' % file_path] output = ['Permission denied: %s' % file_path]
else: else:
status = '200 OK' # This is a very simple implementation of conditional GET with
headers = {} # the Last-Modified header. It makes media files a bit speedier
mime_type = mimetypes.guess_type(file_path)[0] # because the files are only read off disk for the first
if mime_type: # request (assuming the browser/client supports conditional
headers['Content-Type'] = mime_type # GET).
output = [fp.read()] mtime = http_date(os.stat(file_path)[stat.ST_MTIME])
fp.close() headers = {'Last-Modified': mtime}
if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime:
status = '304 NOT MODIFIED'
output = []
else:
status = '200 OK'
mime_type = mimetypes.guess_type(file_path)[0]
if mime_type:
headers['Content-Type'] = mime_type
output = [fp.read()]
fp.close()
start_response(status, headers.items()) start_response(status, headers.items())
return output return output