Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
89f26c5e
Commit
89f26c5e
authored
Dec 06, 2013
by
kahnjw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add get_ident method to pass new tests.
parent
9ab0759e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
7 deletions
+19
-7
rest_framework/settings.py
+1
-0
rest_framework/throttling.py
+18
-7
No files found.
rest_framework/settings.py
View file @
89f26c5e
...
...
@@ -63,6 +63,7 @@ DEFAULTS = {
'user'
:
None
,
'anon'
:
None
,
},
'NUM_PROXIES'
:
None
,
# Pagination
'PAGINATE_BY'
:
None
,
...
...
rest_framework/throttling.py
View file @
89f26c5e
...
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment