Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add stream support #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
20 changes: 13 additions & 7 deletions proxy/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import requests
from django.http import HttpResponse
from django.http import StreamingHttpResponse
from django.http import QueryDict


Expand Down Expand Up @@ -37,10 +37,16 @@ def proxy_view(request, url, requests_args=None):
requests_args['headers'] = headers
requests_args['params'] = params

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

proxy_response = HttpResponse(
response.content,
def stream_content():
for chunk in response.iter_content(chunk_size=1024):
if chunk:
yield chunk

proxy_response = StreamingHttpResponse(
stream_content(),
status=response.status_code)

excluded_headers = set([
Expand All @@ -49,9 +55,9 @@ def proxy_view(request, url, requests_args=None):
# Certain response headers should NOT be just tunneled through. These
# are they. For more info, see:
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
'connection', 'keep-alive', 'proxy-authenticate',
'proxy-authorization', 'te', 'trailers', 'transfer-encoding',
'upgrade',
'connection', 'keep-alive', 'proxy-authenticate',
'proxy-authorization', 'te', 'trailers', 'transfer-encoding',
'upgrade',

# Although content-encoding is not listed among the hop-by-hop headers,
# it can cause trouble as well. Just let the server set the value as
Expand Down