From 3ca85914c9b43d3ba6ebda71feb231b087fa0051 Mon Sep 17 00:00:00 2001 From: randoms Date: Fri, 28 Jul 2017 21:17:47 +0800 Subject: [PATCH 1/2] add stream support --- proxy/views.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/proxy/views.py b/proxy/views.py index 9125c7b..1dbb670 100644 --- a/proxy/views.py +++ b/proxy/views.py @@ -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) + + def stream_content(): + for chunk in response.iter_content(chunk_size=1024): + if chunk: + yield chunk proxy_response = HttpResponse( - response.content, + stream_content(), status=response.status_code) excluded_headers = set([ @@ -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 From 6f4842438df85d2d335135e25646616286f5a4be Mon Sep 17 00:00:00 2001 From: randoms Date: Sat, 29 Jul 2017 09:53:59 +0800 Subject: [PATCH 2/2] use stream reponse --- proxy/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proxy/views.py b/proxy/views.py index 1dbb670..f564bb4 100644 --- a/proxy/views.py +++ b/proxy/views.py @@ -1,5 +1,5 @@ import requests -from django.http import HttpResponse +from django.http import StreamingHttpResponse from django.http import QueryDict @@ -39,13 +39,13 @@ def proxy_view(request, url, requests_args=None): response = requests.request(request.method, url, stream=True, **requests_args) - + def stream_content(): for chunk in response.iter_content(chunk_size=1024): if chunk: yield chunk - proxy_response = HttpResponse( + proxy_response = StreamingHttpResponse( stream_content(), status=response.status_code)