question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTTP CONNECT request sent in different packets while using an HTTPS Proxy

See original GitHub issue

Note: At the request of the requests team, I’ve opened a ticket here.

When using an HTTPS proxy, HTTP headers (and other parts of the request) are sent at different times then the initial HTTP CONNECT method, causing some proxy services to abort the connection.

Expected Result

The entire HTTP request should be sent in the same packet as the CONNECT method.

Actual Result

HTTP Headers and trailing \r\n are sent at a different time.

You can see this in the Wireshark Capture:

First Packet contains the HTTP CONNECT (from here)

screen shot 2018-11-28 at 15 55 17

Second packet shows the left over \r\n (from here)

screen shot 2018-11-28 at 15 56 05

This results in some proxy servers aborting the connection.

Reproduction Steps

>>> import requests
>>> proxies = { 'https': 'https://someproxy:443' }
>>> requests.get('https://www.google.com', proxies=proxies)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/requests/sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "/Users/user/Library/Python/2.7/lib/python/site-packages/requests/adapters.py", line 485, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', error(54, 'Connection reset by peer')))

System Information

$ python -V
Python 2.7.11

(Same behavior on Python 3)

MacOSX

Work Around

The following work around sends the entire HTTP CONNECT request at the same time:

https://gist.github.com/brad-anton/ff517025a86754793b680b3c5cfc9797

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
brad-antoncommented, Dec 10, 2018

One other thing @SethMichaelLarson - wouldnt the fix be in here rather then in urllib?

0reactions
sethmlarsoncommented, Oct 9, 2021

This was fixed in https://github.com/python/cpython/pull/24780 and is available in Python 3.10+

Read more comments on GitHub >

github_iconTop Results From Across the Web

CONNECT - HTTP - MDN Web Docs
The HTTP CONNECT method starts two-way communications with the requested resource. It can be used to open a tunnel.
Read more >
The HTTP CONNECT tunnel
When receiving the CONNECT request, the proxy establishes a TCP connection to the requested hostname on the specified port and then returns HTTP...
Read more >
At a Glance: HTTP Proxy Packets vs. Normal HTTP Packets
HTTP proxy requests : The first packet is sent to the proxy. The requested URI shows the complete URL (host + path). The...
Read more >
How HTTP Tunneling works, The CONNECT method, Pros ...
HTTP CONNECT Method allows the client to create a tunnel through a proxy to forward any free-form content through it.
Read more >
HTTPS proxy CONNECT issue - python - Stack Overflow
The browser sent a CONNECT request to the proxy and the proxy established a TCP connection with the server but when the proxy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found