Skip to content

Commit

Permalink
Update views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dkxce authored Jul 8, 2023
1 parent 860ad0c commit 1e29ab0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion proxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

version = '1.2.0-dkxce'
module = 'django-proxy'
allow_redirect = True

def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
Expand All @@ -26,12 +27,13 @@ def proxy_view(request, url, requests_args=None, *args, **kwargs):
If there are any additional arguments you wish to send to requests, put
them in the requests_args dictionary.
kwargs: HOST=, IP=
kwargs: HOST=, IP=, NOREDIRECT=True
"""
requests_args = (requests_args or {}).copy()
headers = get_headers(request.META)
params = request.GET.copy()
remote_ip = get_client_ip(request)
follow_redirect = True

# Remote IP Headers
headers["X-Real-IP"] = remote_ip
Expand All @@ -46,6 +48,8 @@ def proxy_view(request, url, requests_args=None, *args, **kwargs):
if key.upper() == "IP":
headers["X-Real-IP"] = value
headers["X-Forwarded-For"] = value
if key.upper() == "NOREDIRECT":
follow_redirect = value

if 'headers' not in requests_args:
requests_args['headers'] = {}
Expand All @@ -70,6 +74,9 @@ def proxy_view(request, url, requests_args=None, *args, **kwargs):

response = requests.request(request.method, url, **requests_args)

if allow_redirect and follow_redirect and response.status_code in [301,302,303,304] and url != response.url:
response = requests.request(request.method, response.url, **requests_args)

proxy_response = HttpResponse(
response.content,
status=response.status_code)
Expand Down

0 comments on commit 1e29ab0

Please sign in to comment.