Exemple #1
0
 def test_cache_key_memcache_validation(self):
     """
     Handle cache key creation correctly, see #17861.
     """
     name = b"/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)
     cache_key = storage.staticfiles_storage.cache_key(name)
     cache_validator = BaseCache({})
     cache_validator.validate_key(cache_key)
     self.assertEqual(cache_key, 'staticfiles:e95bbc36387084582df2a70750d7b351')
Exemple #2
0
 def test_cache_key_memcache_validation(self):
     """
     Handle cache key creation correctly, see #17861.
     """
     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/" + "\x16" + "\xb4"
     cache_key = storage.staticfiles_storage.hash_key(name)
     cache_validator = BaseCache({})
     cache_validator.validate_key(cache_key)
     self.assertEqual(cache_key, 'staticfiles:821ea71ef36f95b3922a77f7364670e7')
Exemple #3
0
 def test_cache_key_memcache_validation(self):
     """
     Handle cache key creation correctly, see #17861.
     """
     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)
     cache_key = storage.staticfiles_storage.cache_key(name)
     self.save_warnings_state()
     cache_validator = BaseCache({})
     warnings.filterwarnings('error', category=CacheKeyWarning)
     cache_validator.validate_key(cache_key)
     self.restore_warnings_state()
     self.assertEqual(cache_key, 'staticfiles:e95bbc36387084582df2a70750d7b351')
Exemple #4
0
__FILENAME__ = cachebe
import hashlib

from django.core.cache import cache
from django.core.cache.backends.base import BaseCache

from brake.backends import BaseBackend

CACHE_PREFIX = 'rl:'
BASE_CACHE = BaseCache({})
IP_PREFIX = 'ip:'
KEY_TEMPLATE = 'func:%s:%s%s:%s%s'
PERIOD_PREFIX = 'period:'


class CacheBackend(BaseBackend):
    def get_ip(self, request):
        """This gets the IP we wish to use for ratelimiting.

        It defaults to 'REMOTE_ADDR'. It's recommended that you override
        this function if you're using loadbalancers or any kind of upstream
        proxy service to route requests to Django.
        """
        return request.META['REMOTE_ADDR']

    def _keys(self, func_name, request, ip=True, field=None, period=None):
        keys = []
        if ip:
            keys.append(KEY_TEMPLATE % (func_name, PERIOD_PREFIX, period,
                                        IP_PREFIX, self.get_ip(request)))