From 8f591682e6a901baf0cc8a0393b5930252f0318a Mon Sep 17 00:00:00 2001 From: Dimitris Bliablias Date: Thu, 15 Oct 2015 16:26:08 +0300 Subject: [PATCH] Use buffering for HTTP responses on chunked requests For non-chunked requests, the request is performed using the 'urlopen' method that underlying uses buffering for the HTTP responses (for Python 2.7+ versions). For chunked requests though, the request is made via a different code path and so the 'getresponse' method is called without using buffering. So, the response is consumed per single byte via a recv() call on the underlying socket. This patch, fixes that issue to mimic the non-chucked request behavior. --- requests/adapters.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/requests/adapters.py b/requests/adapters.py index 7682db0a..c69c082e 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -394,7 +394,15 @@ class HTTPAdapter(BaseAdapter): low_conn.send(b'\r\n') low_conn.send(b'0\r\n\r\n') - r = low_conn.getresponse() + # Receive the response from the server + try: + # For Python 2.7+ versions, use buffering of HTTP + # responses + r = conn.getresponse(buffering=True) + except TypeError: + # For compatibility with Python 2.6 versions and back + r = conn.getresponse() + resp = HTTPResponse.from_httplib( r, pool=conn,