aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/throttling.py
diff options
context:
space:
mode:
authorkahnjw2013-12-06 14:21:52 -0800
committerkahnjw2013-12-06 14:21:52 -0800
commit89f26c5e040febd27bc9142b0096ca119bb3fa32 (patch)
tree977672b08b822fae30a435c52b0311ecf558cbac /rest_framework/throttling.py
parent9ab0759e38492d9950d66299ed5c58155d39e696 (diff)
downloaddjango-rest-framework-89f26c5e040febd27bc9142b0096ca119bb3fa32.tar.bz2
Add get_ident method to pass new tests.
Diffstat (limited to 'rest_framework/throttling.py')
-rw-r--r--rest_framework/throttling.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py
index a946d837..60e46d47 100644
--- a/rest_framework/throttling.py
+++ b/rest_framework/throttling.py
@@ -18,6 +18,21 @@ class BaseThrottle(object):
"""
raise NotImplementedError('.allow_request() must be overridden')
+ def get_ident(self, request):
+ """
+ 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.
+ """
+ xff = request.META.get('HTTP_X_FORWARDED_FOR')
+ remote_addr = request.META.get('REMOTE_ADDR')
+ num_proxies = api_settings.NUM_PROXIES
+
+ if xff and num_proxies:
+ return xff.split(',')[-min(num_proxies, len(xff))].strip()
+
+ return xff if xff else remote_addr
+
def wait(self):
"""
Optionally, return a recommended number of seconds to wait before
@@ -152,13 +167,9 @@ class AnonRateThrottle(SimpleRateThrottle):
if request.user.is_authenticated():
return None # Only throttle unauthenticated requests.
- ident = request.META.get('HTTP_X_FORWARDED_FOR')
- if ident is None:
- ident = request.META.get('REMOTE_ADDR')
-
return self.cache_format % {
'scope': self.scope,
- 'ident': ident
+ 'ident': self.get_ident(request)
}
@@ -176,7 +187,7 @@ class UserRateThrottle(SimpleRateThrottle):
if request.user.is_authenticated():
ident = request.user.id
else:
- ident = request.META.get('REMOTE_ADDR', None)
+ ident = self.get_ident(request)
return self.cache_format % {
'scope': self.scope,
@@ -224,7 +235,7 @@ class ScopedRateThrottle(SimpleRateThrottle):
if request.user.is_authenticated():
ident = request.user.id
else:
- ident = request.META.get('REMOTE_ADDR', None)
+ ident = self.get_ident(request)
return self.cache_format % {
'scope': self.scope,