From 31c0962e83c8f3b043b3faceff880f69a9a4dbdc Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 13 Oct 2013 09:53:44 +0100 Subject: [PATCH 1/2] Better connection behaviour for chunked upload. --- requests/adapters.py | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index 0adca690..1d246f2d 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -327,27 +327,39 @@ class HTTPAdapter(BaseAdapter): conn = conn.proxy_pool low_conn = conn._get_conn(timeout=timeout) - low_conn.putrequest(request.method, url, skip_accept_encoding=True) - for header, value in request.headers.items(): - low_conn.putheader(header, value) + try: + low_conn.putrequest(request.method, + url, + skip_accept_encoding=True) - low_conn.endheaders() + for header, value in request.headers.items(): + low_conn.putheader(header, value) - for i in request.body: - low_conn.send(hex(len(i))[2:].encode('utf-8')) - low_conn.send(b'\r\n') - low_conn.send(i) - low_conn.send(b'\r\n') - low_conn.send(b'0\r\n\r\n') + low_conn.endheaders() - r = low_conn.getresponse() - resp = HTTPResponse.from_httplib(r, - pool=conn, - connection=low_conn, - preload_content=False, - decode_content=False - ) + for i in request.body: + low_conn.send(hex(len(i))[2:].encode('utf-8')) + low_conn.send(b'\r\n') + low_conn.send(i) + low_conn.send(b'\r\n') + low_conn.send(b'0\r\n\r\n') + + r = low_conn.getresponse() + resp = HTTPResponse.from_httplib(r, + pool=conn, + connection=low_conn, + preload_content=False, + decode_content=False + ) + except: + # If we hit any problems here, clean up the connection. + # Then, reraise so that we can handle the actual exception. + low_conn.close() + raise + else: + # All is well, return the connection to the pool. + conn._put_conn(low_conn) except socket.error as sockerr: raise ConnectionError(sockerr) From 9606f0240b8e0ce5f64e9659c2161a91c8dc6b60 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 13 Oct 2013 09:54:01 +0100 Subject: [PATCH 2/2] PEP8 fix to make me happier. --- requests/adapters.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requests/adapters.py b/requests/adapters.py index 1d246f2d..4a0977a6 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -346,7 +346,8 @@ class HTTPAdapter(BaseAdapter): low_conn.send(b'0\r\n\r\n') r = low_conn.getresponse() - resp = HTTPResponse.from_httplib(r, + resp = HTTPResponse.from_httplib( + r, pool=conn, connection=low_conn, preload_content=False,