Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17861 -- Took care of special characters when creating the sta…

…ticfiles storage cache keys. Many thanks to Preston Holmes.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17688 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3a5f9cd1ae8a2c7101f141d9730878abb74ec92f 1 parent ebc6fc9
Jannis Leidel authored
2  django/contrib/staticfiles/storage.py
@@ -95,7 +95,7 @@ def hashed_name(self, name, content=None):
95 95
         return urlunsplit(unparsed_name)
96 96
 
97 97
     def cache_key(self, name):
98  
-        return u'staticfiles:cache:%s' % name
  98
+        return u'staticfiles:%s' % hashlib.md5(smart_str(name)).hexdigest()
99 99
 
100 100
     def url(self, name, force=False):
101 101
         """
16  tests/regressiontests/staticfiles_tests/tests.py
@@ -7,10 +7,12 @@
7 7
 import shutil
8 8
 import sys
9 9
 import tempfile
  10
+import warnings
10 11
 from StringIO import StringIO
11 12
 
12 13
 from django.template import loader, Context
13 14
 from django.conf import settings
  15
+from django.core.cache.backends.base import BaseCache, CacheKeyWarning
14 16
 from django.core.exceptions import ImproperlyConfigured
15 17
 from django.core.files.storage import default_storage
16 18
 from django.core.management import call_command
@@ -498,6 +500,20 @@ def test_post_processing(self):
498 500
         self.assertTrue(os.path.join('cached', 'css', 'window.css') in stats['post_processed'])
499 501
         self.assertTrue(os.path.join('cached', 'css', 'img', 'window.png') in stats['unmodified'])
500 502
 
  503
+    def test_cache_key_memcache_validation(self):
  504
+        """
  505
+        Handle cache key creation correctly, see #17861.
  506
+        """
  507
+        name = "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/" + chr(22) + chr(180)
  508
+        cache_key = storage.staticfiles_storage.cache_key(name)
  509
+        self.save_warnings_state()
  510
+        cache_validator = BaseCache({})
  511
+        warnings.filterwarnings('error', category=CacheKeyWarning)
  512
+        cache_validator.validate_key(cache_key)
  513
+        self.restore_warnings_state()
  514
+        self.assertEqual(cache_key, 'staticfiles:e95bbc36387084582df2a70750d7b351')
  515
+
  516
+
501 517
 # we set DEBUG to False here since the template tag wouldn't work otherwise
502 518
 TestCollectionCachedStorage = override_settings(**dict(TEST_SETTINGS,
503 519
     STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',

0 notes on commit 3a5f9cd

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