Skip to content

Commit

Permalink
Use urllib for chunked requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lverrall committed Jul 2, 2019
1 parent 4983a9b commit 2da7fe0
Showing 1 changed file with 13 additions and 58 deletions.
71 changes: 13 additions & 58 deletions requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,64 +435,19 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
timeout = TimeoutSauce(connect=timeout, read=timeout)

try:
if not chunked:
resp = conn.urlopen(
method=request.method,
url=url,
body=request.body,
headers=request.headers,
redirect=False,
assert_same_host=False,
preload_content=False,
decode_content=False,
retries=self.max_retries,
timeout=timeout
)

# Send the request.
else:
if hasattr(conn, 'proxy_pool'):
conn = conn.proxy_pool

low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)

try:
low_conn.putrequest(request.method,
url,
skip_accept_encoding=True)

for header, value in request.headers.items():
low_conn.putheader(header, value)

low_conn.endheaders()

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')

# Receive the response from the server
try:
# For Python 2.7, use buffering of HTTP responses
r = low_conn.getresponse(buffering=True)
except TypeError:
# For compatibility with Python 3.3+
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
resp = conn.urlopen(
method=request.method,
url=url,
body=request.body,
headers=request.headers,
redirect=False,
assert_same_host=False,
preload_content=False,
decode_content=False,
retries=self.max_retries,
timeout=timeout,
chunked=chunked
)

except (ProtocolError, socket.error) as err:
raise ConnectionError(err, request=request)
Expand Down

0 comments on commit 2da7fe0

Please sign in to comment.