Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions proxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@
from urllib.parse import urlparse


def querydict_to_dict(querydict):
"""
Converts a QueryDict object to a dictionary.
Unlike Django's QueryDict.dict() function, this keeps lists that
have two or more items as lists.
"""
data = {}
for key in querydict.keys():
v = querydict.getlist(key)
if len(v) == 1:
v = v[0]
data[key] = v
return data


def proxy_view(request, url, requests_args=None):
"""
Forward as close to an exact copy of the request as possible along to the
given url. Respond with as close to an exact copy of the resulting
response as possible.

If there are any additional arguments you wish to send to requests, put
them in the requests_args dictionary.
"""
requests_args = (requests_args or {}).copy()
headers = get_headers(request.META)
params = request.GET.copy()
params = QueryDict('', mutable=True)
params.update(querydict_to_dict(request.GET))

if 'headers' not in requests_args:
requests_args['headers'] = {}
Expand Down