]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
add socks5h proxy support (#3178)
authorBin Liu <liubin0329@gmail.com>
Tue, 29 Oct 2024 14:10:33 +0000 (22:10 +0800)
committerGitHub <noreply@github.com>
Tue, 29 Oct 2024 14:10:33 +0000 (14:10 +0000)
Signed-off-by: bin liu <liubin0329@gmail.com>
Co-authored-by: Tom Christie <tom@tomchristie.com>
CHANGELOG.md
httpx/_config.py
httpx/_transports/default.py
tests/client/test_proxies.py

index 1d32e53dc695cff187f8a7a1fc99a00813829532..460e315422a58a2597411f469f475e03bc3ea493 100644 (file)
@@ -16,6 +16,7 @@ This release introduces an `httpx.SSLContext()` class and `ssl_context` paramete
 * Ensure JSON request bodies are compact. (#3363)
 * Review URL percent escape sets, based on WHATWG spec. (#3371, #3373)
 * Ensure `certifi` and `httpcore` are only imported if required. (#3377)
+* Treat `socks5h` as a valid proxy scheme. (#3178)
 
 ## 0.27.2 (27th August, 2024)
 
index 5a1a98a0241f179b8dbc43e5d86c9f6f8fd17073..3fd5e1ddcee7b5fe7649eff4865100d9b37fb577 100644 (file)
@@ -262,7 +262,7 @@ class Proxy:
         url = URL(url)
         headers = Headers(headers)
 
-        if url.scheme not in ("http", "https", "socks5"):
+        if url.scheme not in ("http", "https", "socks5", "socks5h"):
             raise ValueError(f"Unknown scheme for proxy URL {url!r}")
 
         if url.username or url.password:
index 50ff91055ef377b6af1af0c48ae41a5387edc760..85d0f5f5227fb8efeb46e7b13f43d93c37ff9520 100644 (file)
@@ -189,7 +189,7 @@ class HTTPTransport(BaseTransport):
                 http2=http2,
                 socket_options=socket_options,
             )
-        elif proxy.url.scheme == "socks5":
+        elif proxy.url.scheme in ("socks5", "socks5h"):
             try:
                 import socksio  # noqa
             except ImportError:  # pragma: no cover
@@ -215,7 +215,7 @@ class HTTPTransport(BaseTransport):
             )
         else:  # pragma: no cover
             raise ValueError(
-                "Proxy protocol must be either 'http', 'https', or 'socks5',"
+                "Proxy protocol must be either 'http', 'https', 'socks5', or 'socks5h',"
                 f" but got {proxy.url.scheme!r}."
             )
 
@@ -338,7 +338,7 @@ class AsyncHTTPTransport(AsyncBaseTransport):
                 http2=http2,
                 socket_options=socket_options,
             )
-        elif proxy.url.scheme == "socks5":
+        elif proxy.url.scheme in ("socks5", "socks5h"):
             try:
                 import socksio  # noqa
             except ImportError:  # pragma: no cover
@@ -364,7 +364,7 @@ class AsyncHTTPTransport(AsyncBaseTransport):
             )
         else:  # pragma: no cover
             raise ValueError(
-                "Proxy protocol must be either 'http', 'https', or 'socks5',"
+                "Proxy protocol must be either 'http', 'https', 'socks5', or 'socks5h',"
                 " but got {proxy.url.scheme!r}."
             )
 
index 90a92f56bbb28c31821c7c94391d24026dd9e2d3..3e4090dcecb3e805041ec23738d441e2c23e8c23 100644 (file)
@@ -16,15 +16,16 @@ def url_to_origin(url: str) -> httpcore.URL:
 def test_socks_proxy():
     url = httpx.URL("http://www.example.com")
 
-    client = httpx.Client(proxy="socks5://localhost/")
-    transport = client._transport_for_url(url)
-    assert isinstance(transport, httpx.HTTPTransport)
-    assert isinstance(transport._pool, httpcore.SOCKSProxy)
+    for proxy in ("socks5://localhost/", "socks5h://localhost/"):
+        client = httpx.Client(proxy=proxy)
+        transport = client._transport_for_url(url)
+        assert isinstance(transport, httpx.HTTPTransport)
+        assert isinstance(transport._pool, httpcore.SOCKSProxy)
 
-    async_client = httpx.AsyncClient(proxy="socks5://localhost/")
-    async_transport = async_client._transport_for_url(url)
-    assert isinstance(async_transport, httpx.AsyncHTTPTransport)
-    assert isinstance(async_transport._pool, httpcore.AsyncSOCKSProxy)
+        async_client = httpx.AsyncClient(proxy=proxy)
+        async_transport = async_client._transport_for_url(url)
+        assert isinstance(async_transport, httpx.AsyncHTTPTransport)
+        assert isinstance(async_transport._pool, httpcore.AsyncSOCKSProxy)
 
 
 PROXY_URL = "http://[::1]"