Ticket #12544: 12544-handle-bogus-if-modified-since-r12117.2.diff

File 12544-handle-bogus-if-modified-since-r12117.2.diff, 2.5 KB (added by Antti Kaihola, 15 years ago)

Fixed to comply with RFC 2616 section 14.25

  • django/views/static.py

    diff --git a/django/views/static.py b/django/views/static.py
    index 8355950..52544c6 100644
    a b def was_modified_since(header=None, mtime=0, size=0):  
    132132            raise ValueError
    133133        if mtime > header_mtime:
    134134            raise ValueError
    135     except (AttributeError, ValueError):
     135    except (AttributeError, ValueError, OverflowError):
     136        # mktime_tz raises OverflowError on an invalid timestamp
    136137        return True
    137138    return False
  • tests/regressiontests/views/tests/static.py

    diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py
    index d7e87d1..ed8f5e1 100644
    a b  
    11from os import path
    22
    33from django.test import TestCase
     4from django.http import HttpResponseNotModified
    45from regressiontests.views.urls import media_dir
    56
    67class StaticTests(TestCase):
    class StaticTests(TestCase):  
    2526        file = open(path.join(media_dir, file_name))
    2627        self.assertEquals(file.read(), response.content)
    2728
     29    def test_is_modified_since(self):
     30        file_name = 'file.txt'
     31        response = self.client.get(
     32            '/views/site_media/%s' % file_name,
     33            HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT')
     34        file = open(path.join(media_dir, file_name))
     35        self.assertEquals(file.read(), response.content)
     36
     37    def test_not_modified_since(self):
     38        file_name = 'file.txt'
     39        response = self.client.get(
     40            '/views/site_media/%s' % file_name,
     41            HTTP_IF_MODIFIED_SINCE='Mon, 18 Jan 2038 05:14:07 UTC'
     42            # This is 24h before max Unix time. Remember to fix Django and
     43            # update this test well before 2038 :)
     44            )
     45        self.assertTrue(isinstance(response, HttpResponseNotModified))
     46
     47    def test_invalid_if_modified_since(self):
     48        """Handle bogus If-Modified-Since values gracefully
     49
     50        Assume that a file is modified since an invalid timestamp as per RFC
     51        2616, section 14.25.
     52        """
     53        file_name = 'file.txt'
     54        response = self.client.get(
     55            '/views/site_media/%s' % file_name,
     56            HTTP_IF_MODIFIED_SINCE='Fri, 34 Feb 3118 24:34:19 GMT'
     57            # This is a real-life example of a bogus If-Modified-Since header.
     58            )
     59        file = open(path.join(media_dir, file_name))
     60        self.assertEquals(file.read(), response.content)
Back to Top