Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10016: the cache middleware no longer vomits when handed long …

…URLs. Thanks, Matt Croydon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10335 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 16c9df0a761732b8e3acbfa39b940ffde0b1dbf3 1 parent 644e981
Jacob Kaplan-Moss authored April 01, 2009
8  django/utils/cache.py
@@ -29,6 +29,7 @@
29 29
 from django.utils.encoding import smart_str, iri_to_uri
30 30
 from django.utils.http import http_date
31 31
 from django.utils.hashcompat import md5_constructor
  32
+from django.http import HttpRequest
32 33
 
33 34
 cc_delim_re = re.compile(r'\s*,\s*')
34 35
 
@@ -143,13 +144,14 @@ def _generate_cache_key(request, headerlist, key_prefix):
143 144
         value = request.META.get(header, None)
144 145
         if value is not None:
145 146
             ctx.update(value)
  147
+    path = md5_constructor(iri_to_uri(request.path))
146 148
     return 'views.decorators.cache.cache_page.%s.%s.%s' % (
147  
-               key_prefix, iri_to_uri(request.path), ctx.hexdigest())
  149
+               key_prefix, path.hexdigest(), ctx.hexdigest())
148 150
 
149 151
 def _generate_cache_header_key(key_prefix, request):
150 152
     """Returns a cache key for the header cache."""
151  
-    return 'views.decorators.cache.cache_header.%s.%s' % (
152  
-        key_prefix, iri_to_uri(request.path))
  153
+    path = md5_constructor(iri_to_uri(request.path))
  154
+    return 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, path.hexdigest())
153 155
 
154 156
 def get_cache_key(request, key_prefix=None):
155 157
     """
44  tests/regressiontests/cache/tests.py
@@ -13,8 +13,8 @@
13 13
 from django.core import management
14 14
 from django.core.cache import get_cache
15 15
 from django.core.cache.backends.base import InvalidCacheBackendError
16  
-from django.http import HttpResponse
17  
-from django.utils.cache import patch_vary_headers
  16
+from django.http import HttpResponse, HttpRequest
  17
+from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
18 18
 from django.utils.hashcompat import md5_constructor
19 19
 
20 20
 # functions/classes for complex data type tests
@@ -294,6 +294,26 @@ def test_subdirectory_removal(self):
294 294
 class CacheUtils(unittest.TestCase):
295 295
     """TestCase for django.utils.cache functions."""
296 296
 
  297
+    def setUp(self):
  298
+        self.path = '/cache/test/'
  299
+        self.old_settings_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
  300
+        self.old_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS
  301
+        settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'settingsprefix'
  302
+        settings.CACHE_MIDDLEWARE_SECONDS = 1
  303
+
  304
+    def tearDown(self):
  305
+        settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.old_settings_key_prefix
  306
+        settings.CACHE_MIDDLEWARE_SECONDS = self.old_middleware_seconds
  307
+
  308
+    def _get_request(self, path):
  309
+        request = HttpRequest()
  310
+        request.META = {
  311
+            'SERVER_NAME': 'testserver',
  312
+            'SERVER_PORT': 80,
  313
+        }
  314
+        request.path = request.path_info = "/cache/%s" % path
  315
+        return request
  316
+
297 317
     def test_patch_vary_headers(self):
298 318
         headers = (
299 319
             # Initial vary, new headers, resulting vary.
@@ -314,6 +334,26 @@ def test_patch_vary_headers(self):
314 334
             patch_vary_headers(response, newheaders)
315 335
             self.assertEqual(response['Vary'], resulting_vary)
316 336
 
  337
+    def test_get_cache_key(self):
  338
+        request = self._get_request(self.path)
  339
+        response = HttpResponse()
  340
+        key_prefix = 'localprefix'
  341
+        # Expect None if no headers have been set yet.
  342
+        self.assertEqual(get_cache_key(request), None)
  343
+        # Set headers to an empty list.
  344
+        learn_cache_key(request, response)
  345
+        self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
  346
+        # Verify that a specified key_prefix is taken in to account.
  347
+        learn_cache_key(request, response, key_prefix=key_prefix)
  348
+        self.assertEqual(get_cache_key(request, key_prefix=key_prefix), 'views.decorators.cache.cache_page.localprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
  349
+
  350
+    def test_learn_cache_key(self):
  351
+        request = self._get_request(self.path)
  352
+        response = HttpResponse()
  353
+        response['Vary'] = 'Pony'
  354
+        # Make sure that the Vary header is added to the key hash
  355
+        learn_cache_key(request, response)
  356
+        self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
317 357
 
318 358
 if __name__ == '__main__':
319 359
     unittest.main()

0 notes on commit 16c9df0

Please sign in to comment.
Something went wrong with that request. Please try again.