Skip to content

Commit

Permalink
Fixed #1457 -- Added support for if-modified-since header in django.v…
Browse files Browse the repository at this point in the history
…iews.static. Thanks, Shannon -jj Behrens

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2476 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Mar 3, 2006
1 parent cffd184 commit 5beb39b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ answer newbie questions, and generally made Django that much better:
Arthur <avandorp@gmail.com> Arthur <avandorp@gmail.com>
Jiri Barton Jiri Barton
Ned Batchelder <http://www.nedbatchelder.com/> Ned Batchelder <http://www.nedbatchelder.com/>
Shannon -jj Behrens <http://jjinux.blogspot.com/>
James Bennett James Bennett
Paul Bissex <http://e-scribe.com/> Paul Bissex <http://e-scribe.com/>
Simon Blanchard Simon Blanchard
Expand Down
54 changes: 47 additions & 7 deletions django/views/static.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import urllib import urllib
import posixpath import posixpath
import mimetypes import mimetypes
import re
import rfc822
import stat
from django.core import template_loader from django.core import template_loader
from django.core.exceptions import Http404, ImproperlyConfigured from django.core.exceptions import Http404, ImproperlyConfigured
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, \
HttpResponseNotModified
from django.core.template import Template, Context, TemplateDoesNotExist from django.core.template import Template, Context, TemplateDoesNotExist


def serve(request, path, document_root=None, show_indexes=False): def serve(request, path, document_root=None, show_indexes=False):
Expand Down Expand Up @@ -41,13 +45,19 @@ def serve(request, path, document_root=None, show_indexes=False):
if os.path.isdir(fullpath): if os.path.isdir(fullpath):
if show_indexes: if show_indexes:
return directory_index(newpath, fullpath) return directory_index(newpath, fullpath)
else: raise Http404, "Directory indexes are not allowed here."
raise Http404, "Directory indexes are not allowed here." if not os.path.exists(fullpath):
elif not os.path.exists(fullpath):
raise Http404, '"%s" does not exist' % fullpath raise Http404, '"%s" does not exist' % fullpath
else: # Respect the If-Modified-Since header.
mimetype = mimetypes.guess_type(fullpath)[0] statobj = os.stat(fullpath)
return HttpResponse(open(fullpath, 'rb').read(), mimetype=mimetype) if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
return HttpResponseNotModified()
mimetype = mimetypes.guess_type(fullpath)[0]
contents = open(fullpath, 'rb').read()
response = HttpResponse(contents, mimetype=mimetype)
response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME])
return response


DEFAULT_DIRECTORY_INDEX_TEMPLATE = """ DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Expand Down Expand Up @@ -85,3 +95,33 @@ def directory_index(path, fullpath):
'file_list' : files, 'file_list' : files,
}) })
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))

def was_modified_since(header=None, mtime=0, size=0):
"""
Was something modified since the user last downloaded it?
header
This is the value of the If-Modified-Since header. If this is None,
I'll just return True.
mtime
This is the modification time of the item we're talking about.
size
This is the size of the item we're talking about.
"""
try:
if header is None:
raise ValueError
matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
re.IGNORECASE)
header_mtime = rfc822.mktime_tz(rfc822.parsedate_tz(
matches.group(1)))
header_len = matches.group(3)
if header_len and int(header_len) != size:
raise ValueError
if mtime > header_mtime:
raise ValueError
except (AttributeError, ValueError):
return True
return False

0 comments on commit 5beb39b

Please sign in to comment.