async def test_proxy_socket_does_not_leak_when_the_connection_hasnt_been_added_to_pool( proxy_server: URL, server: Server, proxy_mode: str, protocol: bytes, port: int, ): method = b"GET" url = (protocol, b"blockedhost.example.com", port, b"/") headers = [(b"host", b"blockedhost.example.com")] with pytest.warns(None) as recorded_warnings: async with httpcore.AsyncHTTPProxy(proxy_server, proxy_mode=proxy_mode) as http: for _ in range(100): try: _ = await http.arequest(method, url, headers) except (httpcore.ProxyError, httpcore.RemoteProtocolError): pass # have to filter out https://github.com/encode/httpx/issues/825 from other tests warnings = [ *filter(lambda warn: "asyncio" not in warn.filename, recorded_warnings.list) ] assert len(warnings) == 0
async def test_proxy_https_requests( proxy_server: URL, ca_ssl_context: ssl.SSLContext, proxy_mode: str, http2: bool, ) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") headers = [(b"host", b"example.org")] max_connections = 1 max_keepalive = 2 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, ssl_context=ca_ssl_context, max_connections=max_connections, max_keepalive=max_keepalive, http2=http2, ) as http: http_version, status_code, reason, headers, stream = await http.request( method, url, headers) _ = await read_body(stream) assert http_version == (b"HTTP/2" if http2 else b"HTTP/1.1") assert status_code == 200 assert reason == b"OK"
async def test_proxy_https_requests( proxy_server: URL, proxy_mode: str, http2: bool, https_server: Server, ) -> None: max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, extensions = await http.handle_async_request( method=b"GET", url=(b"https", *https_server.netloc, b"/"), headers=[https_server.host_header], stream=httpcore.ByteStream(b""), extensions={}, ) _ = await read_body(stream) assert status_code == 200 assert extensions["http_version"] == b"HTTP/2" if http2 else b"HTTP/1.1" assert extensions.get("reason_phrase", b"") == b"" if http2 else b"OK"
async def test_http_proxy(proxy_server: URL, proxy_mode: str) -> None: method = b"GET" url = (b"http", b"example.org", 80, b"/") headers = [(b"host", b"example.org")] max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, ) as http: http_version, status_code, reason, headers, stream = await http.request( method, url, headers ) await read_body(stream) assert http_version == b"HTTP/1.1" assert status_code == 200 assert reason == b"OK"
async def test_http_proxy( proxy_server: URL, proxy_mode: str, backend: str, server: Server ) -> None: method = b"GET" url = (b"http", *server.netloc, b"/") headers = [server.host_header] max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, backend=backend, ) as http: status_code, headers, stream, ext = await http.arequest(method, url, headers) await read_body(stream) assert status_code == 200 assert ext == {"http_version": "HTTP/1.1", "reason": "OK"}
def __init__( self, verify: VerifyTypes = True, cert: CertTypes = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, proxy: Proxy = None, uds: str = None, local_address: str = None, retries: int = 0, backend: str = "auto", ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) if proxy is None: self._pool = httpcore.AsyncConnectionPool( ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http1=http1, http2=http2, uds=uds, local_address=local_address, retries=retries, backend=backend, ) else: self._pool = httpcore.AsyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http2=http2, backend=backend, )
def _init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, cert: CertTypes = None, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, ) -> httpcore.AsyncHTTPTransport: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) return httpcore.AsyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=KEEPALIVE_EXPIRY, http2=http2, )
def init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, cert: CertTypes = None, http2: bool = False, pool_limits: PoolLimits = DEFAULT_POOL_LIMITS, trust_env: bool = True, ) -> httpcore.AsyncHTTPTransport: ssl_context = SSLConfig(verify=verify, cert=cert, trust_env=trust_env).ssl_context return httpcore.AsyncHTTPProxy( proxy_url=proxy.url.raw, proxy_headers=proxy.headers.raw, proxy_mode=proxy.mode, ssl_context=ssl_context, max_keepalive=pool_limits.max_keepalive, max_connections=pool_limits.max_connections, http2=http2, )
async def test_proxy_https_requests(proxy_server: URL, ca_ssl_context: ssl.SSLContext, proxy_mode: str, http2: bool) -> None: method = b"GET" url = (b"https", b"example.org", 443, b"/") headers = [(b"host", b"example.org")] max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, ssl_context=ca_ssl_context, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, ext = await http.arequest( method, url, headers) _ = await read_body(stream) assert status_code == 200 assert ext["http_version"] == "HTTP/2" if http2 else "HTTP/1.1" assert ext.get("reason", "") == "" if http2 else "OK"
async def test_proxy_https_requests( proxy_server: URL, proxy_mode: str, http2: bool, https_server: Server, ) -> None: method = b"GET" url = (b"https", *https_server.netloc, b"/") headers = [https_server.host_header] max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, http2=http2, ) as http: status_code, headers, stream, ext = await http.arequest(method, url, headers) _ = await read_body(stream) assert status_code == 200 assert ext["http_version"] == "HTTP/2" if http2 else "HTTP/1.1" assert ext.get("reason", "") == "" if http2 else "OK"
async def test_http_proxy(proxy_server: URL, proxy_mode: str, backend: str, server: Server) -> None: max_connections = 1 async with httpcore.AsyncHTTPProxy( proxy_server, proxy_mode=proxy_mode, max_connections=max_connections, backend=backend, ) as http: status_code, headers, stream, extensions = await http.handle_async_request( method=b"GET", url=(b"http", *server.netloc, b"/"), headers=[server.host_header], stream=httpcore.ByteStream(b""), extensions={}, ) await read_body(stream) assert status_code == 200 reason_phrase = b"OK" if server.sends_reason else b"" assert extensions == { "http_version": b"HTTP/1.1", "reason_phrase": reason_phrase, }
def __init__( self, verify: VerifyTypes = True, cert: CertTypes = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, proxy: Proxy = None, uds: str = None, local_address: str = None, retries: int = 0, ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) if proxy is None: self._pool = httpcore.AsyncConnectionPool( ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http1=http1, http2=http2, uds=uds, local_address=local_address, retries=retries, ) elif proxy.url.scheme in ("http", "https"): self._pool = httpcore.AsyncHTTPProxy( proxy_url=httpcore.URL( scheme=proxy.url.raw_scheme, host=proxy.url.raw_host, port=proxy.url.port, target=proxy.url.raw_path, ), proxy_auth=proxy.raw_auth, proxy_headers=proxy.headers.raw, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http1=http1, http2=http2, ) elif proxy.url.scheme == "socks5": try: import socksio # noqa except ImportError: # pragma: nocover raise ImportError( "Using SOCKS proxy, but the 'socksio' package is not installed. " "Make sure to install httpx using `pip install httpx[socks]`." ) from None self._pool = httpcore.AsyncSOCKSProxy( proxy_url=httpcore.URL( scheme=proxy.url.raw_scheme, host=proxy.url.raw_host, port=proxy.url.port, target=proxy.url.raw_path, ), proxy_auth=proxy.raw_auth, ssl_context=ssl_context, max_connections=limits.max_connections, max_keepalive_connections=limits.max_keepalive_connections, keepalive_expiry=limits.keepalive_expiry, http1=http1, http2=http2, ) else: # pragma: nocover raise ValueError( f"Proxy protocol must be either 'http', 'https', or 'socks5', but got {proxy.url.scheme!r}." )