Detect 'unknown protocol' for connecting via HTTPS to an HTTP proxy
MODIFY
src/urllib3/connection.py
src/urllib3/connection.py
@@ -690,10 +690,15 @@ def _wrap_proxy_error(err: Exception) -> ProxyError:
# then we should warn the user that we're very sure that
# this proxy is HTTP-only and they have a configuration issue.
error_normalized = " ".join(re.split("[^a-z]", str(err).lower()))
- is_likely_http_proxy = "wrong version number" in error_normalized
+ is_likely_http_proxy = (
+ "wrong version number" in error_normalized
+ or "unknown protocol" in error_normalized
+ )
http_proxy_warning = (
". Your proxy appears to only use HTTP and not HTTPS, "
- "did you intend to set a proxy URL using HTTPS instead of HTTP?"
+ "try changing your proxy URL to be HTTP. See: "
+ "https://urllib3.readthedocs.io/en/latest/advanced-usage.html"
+ "#https-proxy-error-http-proxy"
)
new_err = ProxyError(
f"Unable to connect to proxy"

MODIFY
test/with_dummyserver/test_proxy_poolmanager.py
test/with_dummyserver/test_proxy_poolmanager.py
@@ -515,7 +515,10 @@ class TestHTTPProxyManager(HTTPDummyProxyTestCase):
target_url = self.https_url if target_scheme == "https" else self.http_url
with proxy_from_url(
- proxy_url, ca_certs=DEFAULT_CA, timeout=SHORT_TIMEOUT
+ proxy_url,
+ ca_certs=DEFAULT_CA,
+ timeout=SHORT_TIMEOUT,
+ use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", target_url)
@@ -556,7 +559,11 @@ class TestHTTPProxyManager(HTTPDummyProxyTestCase):
) -> None:
target_url = self.https_url if target_scheme == "https" else self.http_url
proxy_ctx = ssl.create_default_context()
- with proxy_from_url(self.https_proxy_url, proxy_ssl_context=proxy_ctx) as proxy:
+ with proxy_from_url(
+ self.https_proxy_url,
+ proxy_ssl_context=proxy_ctx,
+ use_forwarding_for_https=use_forwarding_for_https,
+ ) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", target_url)
assert type(e.value.reason) == ProxyError
@@ -574,13 +581,19 @@ class TestHTTPProxyManager(HTTPDummyProxyTestCase):
def test_proxy_https_target_tls_error(
self, proxy_scheme: str, use_forwarding_for_https: str
) -> None:
+ if proxy_scheme == "https" and use_forwarding_for_https:
+ pytest.skip("Test is expected to fail due to urllib3/urllib3#2577")
+
proxy_url = self.https_proxy_url if proxy_scheme == "https" else self.proxy_url
proxy_ctx = ssl.create_default_context()
proxy_ctx.load_verify_locations(DEFAULT_CA)
ctx = ssl.create_default_context()
with proxy_from_url(
- proxy_url, proxy_ssl_context=proxy_ctx, ssl_context=ctx
+ proxy_url,
+ proxy_ssl_context=proxy_ctx,
+ ssl_context=ctx,
+ use_forwarding_for_https=use_forwarding_for_https,
) as proxy:
with pytest.raises(MaxRetryError) as e:
proxy.request("GET", self.https_url)

Detect 'unknown protocol' for connecting via HTTPS to an HTTP proxy