From f0ee72f578d6218ca30b9fa8e50d0b1adb0d828a Mon Sep 17 00:00:00 2001 From: kahnjw Date: Fri, 6 Dec 2013 13:51:30 -0800 Subject: [PATCH] Remove default_to arg and use X_FORWARDED_FOR & fallback to REMOTE_ADDR --- rest_framework/throttling.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 1fea202bc..f79e58303 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -18,21 +18,20 @@ class BaseThrottle(object): """ raise NotImplementedError('.allow_request() must be overridden') - def get_ident(self, request, default_to=None): + def get_ident(self, request): """ - Identify the machine making the request using HTTP_X_FORWARDED_FOR if - present and number of proxies is > 0. If not use default if it is set - and available, if not fall back to REMOTE_ADDR. + Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR + if present and number of proxies is > 0. If not use all of + HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR. """ - default = request.META.get(default_to) + xff = request.META.get('HTTP_X_FORWARDED_FOR') + remote_addr = request.META.get('REMOTE_ADDR') num_proxies = api_settings.NUM_PROXIES - if 'HTTP_X_FORWARDED_FOR' in request.META and num_proxies: - xff = request.META.get('HTTP_X_FORWARDED_FOR') - + if xff and num_proxies: return xff.split(',')[-min(num_proxies, len(xff))].strip() - return default if default else request.META.get('REMOTE_ADDR') + return xff if xff else remote_addr def wait(self): """ @@ -168,7 +167,7 @@ class AnonRateThrottle(SimpleRateThrottle): if request.user.is_authenticated(): return None # Only throttle unauthenticated requests. - ident = self.get_ident(request, default_to='HTTP_X_FORWARDED_FOR') + ident = self.get_ident(request) return self.cache_format % { 'scope': self.scope,