Commit 7bf7eab6 by Chris Dodge

support client-side caching through 'Last-Modified' response headers

parent 7512b78e
import logging import logging
import time
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404, HttpResponseNotModified
from xmodule.contentstore.django import contentstore from xmodule.contentstore.django import contentstore
from xmodule.contentstore import StaticContent from xmodule.contentstore import StaticContent
...@@ -30,7 +31,22 @@ class StaticContentServer(object): ...@@ -30,7 +31,22 @@ class StaticContentServer(object):
else: else:
logging.debug('cache hit on {0}'.format(content.filename)) logging.debug('cache hit on {0}'.format(content.filename))
# see if the last-modified at hasn't changed, if not return a 302 (Not Modified)
logging.debug(request.META)
# convert over the DB persistent last modified timestamp to a HTTP compatible
# timestamp
last_modified_at_str = content.last_modified_at.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
# see if the client has cached this content, if so then compare the
# timestamps, if they are the same then just return a 304 (Not Modified)
if 'HTTP_IF_MODIFIED_SINCE' in request.META:
if_modified_since = request.META['HTTP_IF_MODIFIED_SINCE']
if if_modified_since == last_modified_at_str:
return HttpResponseNotModified()
response = HttpResponse(content.data, content_type=content.content_type) response = HttpResponse(content.data, content_type=content.content_type)
response['Content-Disposition'] = 'attachment; filename={0}'.format(content.name) response['Last-Modified'] = last_modified_at_str
return response return response
class StaticContent(object): class StaticContent(object):
def __init__(self, filename, name, content_type, data): def __init__(self, filename, name, content_type, data, last_modified_at=None):
self.filename = filename self.filename = filename
self.name = name self.name = name
self.content_type = content_type self.content_type = content_type
self.data = data self.data = data
self.last_modified_at = last_modified_at
@staticmethod @staticmethod
def get_location_tag(): def get_location_tag():
......
...@@ -23,8 +23,7 @@ class MongoContentStore(object): ...@@ -23,8 +23,7 @@ class MongoContentStore(object):
def find(self, filename): def find(self, filename):
try: try:
with self.fs.get_last_version(filename) as fp: with self.fs.get_last_version(filename) as fp:
logging.debug('fetched {0}'.format(fp.name)) return StaticContent(fp.filename, fp.displayname, fp.content_type, fp.read(), fp.uploadDate)
return StaticContent(fp.filename, fp.displayname, fp.content_type, fp.read())
except NoFile: except NoFile:
raise NotFoundError() raise NotFoundError()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment