add socks5h proxy support (#3178)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bin Liu · committed by GitHub · parent e9cabc8 · Browse files · 4 files·+15 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: bin liu <liubin0329@gmail.com>
- Co-authored-by: Tom Christie <tom@tomchristie.com>
Diff stats
4 files+15 added−13 removed
- tests:+9 added−8 removed
- other:+6 added−5 removed
Provenance
- Commit
12be5c4- Tree
07df65b- Author
Bin Liu <liubin0329@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
e9cabc8
4 files changed+15 added−13 removed
modified:httpx/_transports/default.py+4 added-4 removed
187 unmodified lines188 http1=http1,189 http2=http2,190 socket_options=socket_options,191 )192- elif proxy.url.scheme == "socks5":192+ elif proxy.url.scheme in ("socks5", "socks5h"):193 try:194 import socksio # noqa195 except ImportError: # pragma: no cover196 raise ImportError(17 unmodified lines214 http2=http2,215 )216 else: # pragma: no cover217 raise ValueError(218- "Proxy protocol must be either 'http', 'https', or 'socks5',"218+ "Proxy protocol must be either 'http', 'https', 'socks5', or 'socks5h',"219 f" but got {proxy.url.scheme!r}."220 )221 222 def __enter__(self: T) -> T: # Use generics for subclass support.114 unmodified lines337 http1=http1,338 http2=http2,339 socket_options=socket_options,340 )341- elif proxy.url.scheme == "socks5":341+ elif proxy.url.scheme in ("socks5", "socks5h"):342 try:343 import socksio # noqa344 except ImportError: # pragma: no cover345 raise ImportError(17 unmodified lines363 http2=http2,364 )365 else: # pragma: no cover366 raise ValueError(367- "Proxy protocol must be either 'http', 'https', or 'socks5',"367+ "Proxy protocol must be either 'http', 'https', 'socks5', or 'socks5h',"368 " but got {proxy.url.scheme!r}."369 )370 371 async def __aenter__(self: A) -> A: # Use generics for subclass support.45 unmodified linesmodified:httpx/_config.py+1 added-1 removed
260 unmodified lines261 ) -> None:262 url = URL(url)263 headers = Headers(headers)264 265- if url.scheme not in ("http", "https", "socks5"):265+ if url.scheme not in ("http", "https", "socks5", "socks5h"):266 raise ValueError(f"Unknown scheme for proxy URL {url!r}")267 268 if url.username or url.password:269 # Remove any auth credentials from the URL.31 unmodified linesmodified:tests/client/test_proxies.py+9 added-8 removed
14 unmodified lines15 16 def test_socks_proxy():17 url = httpx.URL("http://www.example.com")18 19+ for proxy in ("socks5://localhost/", "socks5h://localhost/"):19- client = httpx.Client(proxy="socks5://localhost/")20- transport = client._transport_for_url(url)21- assert isinstance(transport, httpx.HTTPTransport)22- assert isinstance(transport._pool, httpcore.SOCKSProxy)20+ client = httpx.Client(proxy=proxy)21+ transport = client._transport_for_url(url)22+ assert isinstance(transport, httpx.HTTPTransport)23+ assert isinstance(transport._pool, httpcore.SOCKSProxy)24 24- async_client = httpx.AsyncClient(proxy="socks5://localhost/")25- async_transport = async_client._transport_for_url(url)26- assert isinstance(async_transport, httpx.AsyncHTTPTransport)27- assert isinstance(async_transport._pool, httpcore.AsyncSOCKSProxy)25+ async_client = httpx.AsyncClient(proxy=proxy)26+ async_transport = async_client._transport_for_url(url)27+ assert isinstance(async_transport, httpx.AsyncHTTPTransport)28+ assert isinstance(async_transport._pool, httpcore.AsyncSOCKSProxy)29 30 31 PROXY_URL = "http://[::1]"32 233 unmodified linesmodified:CHANGELOG.md+1 added
14 unmodified lines15 * The `URL.raw` property has now been removed.16 * Ensure JSON request bodies are compact. (#3363)17 * Review URL percent escape sets, based on WHATWG spec. (#3371, #3373)18 * Ensure `certifi` and `httpcore` are only imported if required. (#3377)19+* Treat `socks5h` as a valid proxy scheme. (#3178)20 21 ## 0.27.2 (27th August, 2024)22 23 ### Fixed1091 unmodified lines