summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2010-09-26 20:44:56 +0000
committerJannis Leidel <jannis@leidel.info>2010-09-26 20:44:56 +0000
commit94096e83e9314d960599d4d4773fe4a6329bd321 (patch)
treef00c8d8036e222ba3aec203c4113ffda34660e79
parenta686096c262b706b1367abc87436ae9dc47dbe2a (diff)
Fixed #12544 and #13600 -- Fixed static files serving view to catch invalid date from If-Modified-Since header. Thanks akaihola and SmileyChris for patches.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13870 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/views/static.py2
-rw-r--r--tests/regressiontests/views/tests/static.py34
2 files changed, 35 insertions, 1 deletions
diff --git a/django/views/static.py b/django/views/static.py
index a2990f1d1d..cc88211876 100644
--- a/django/views/static.py
+++ b/django/views/static.py
@@ -135,6 +135,6 @@ def was_modified_since(header=None, mtime=0, size=0):
raise ValueError
if mtime > header_mtime:
raise ValueError
- except (AttributeError, ValueError):
+ except (AttributeError, ValueError, OverflowError):
return True
return False
diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py
index fdd31b4660..25153e86b0 100644
--- a/tests/regressiontests/views/tests/static.py
+++ b/tests/regressiontests/views/tests/static.py
@@ -2,6 +2,7 @@ import mimetypes
from os import path
from django.test import TestCase
+from django.http import HttpResponseNotModified
from regressiontests.views.urls import media_dir
class StaticTests(TestCase):
@@ -27,3 +28,36 @@ class StaticTests(TestCase):
file = open(path.join(media_dir, file_name))
self.assertEquals(file.read(), response.content)
+ def test_is_modified_since(self):
+ file_name = 'file.txt'
+ response = self.client.get(
+ '/views/site_media/%s' % file_name,
+ HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT')
+ file = open(path.join(media_dir, file_name))
+ self.assertEquals(file.read(), response.content)
+
+ def test_not_modified_since(self):
+ file_name = 'file.txt'
+ response = self.client.get(
+ '/views/site_media/%s' % file_name,
+ HTTP_IF_MODIFIED_SINCE='Mon, 18 Jan 2038 05:14:07 UTC'
+ # This is 24h before max Unix time. Remember to fix Django and
+ # update this test well before 2038 :)
+ )
+ self.assertTrue(isinstance(response, HttpResponseNotModified))
+
+ def test_invalid_if_modified_since(self):
+ """Handle bogus If-Modified-Since values gracefully
+
+ Assume that a file is modified since an invalid timestamp as per RFC
+ 2616, section 14.25.
+ """
+ file_name = 'file.txt'
+ invalid_date = 'Mon, 28 May 999999999999 28:25:26 GMT'
+ response = self.client.get('/views/site_media/%s' % file_name,
+ HTTP_IF_MODIFIED_SINCE=invalid_date)
+ file = open(path.join(media_dir, file_name))
+ self.assertEquals(file.read(), response.content)
+ self.assertEquals(len(response.content),
+ int(response['Content-Length']))
+