Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing missed arguments for pools when init transports #857

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def __init__(
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: Optional[str] = None,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
NewUserHa marked this conversation as resolved.
Show resolved Hide resolved
network_backend: Optional[AsyncNetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
) -> None:
Expand All @@ -52,9 +52,9 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._http1 = http1
self._http2 = http2
self._retries = retries
self._local_address = local_address
self._uds = uds
self._local_address = local_address
self._retries = retries
NewUserHa marked this conversation as resolved.
Show resolved Hide resolved

self._network_backend: AsyncNetworkBackend = (
AutoBackend() if network_backend is None else network_backend
Expand Down
18 changes: 9 additions & 9 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def __init__(
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: Optional[str] = None,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
network_backend: Optional[AsyncNetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
) -> None:
Expand All @@ -75,13 +75,13 @@ def __init__(
by the connection pool. Defaults to True.
http2: A boolean indicating if HTTP/2 requests should be supported by
the connection pool. Defaults to False.
retries: The maximum number of retries when trying to establish a
connection.
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
local_address: Local address to connect from. Can also be used to connect
using a particular address family. Using `local_address="0.0.0.0"`
will connect using an `AF_INET` address (IPv4), while using
`local_address="::"` will connect using an `AF_INET6` address (IPv6).
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
retries: The maximum number of retries when trying to establish a
connection.
network_backend: A backend instance to use for handling network I/O.
socket_options: Socket options that have to be included
in the TCP socket when the connection was established.
Expand All @@ -103,9 +103,9 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._http1 = http1
self._http2 = http2
self._retries = retries
self._local_address = local_address
self._uds = uds
self._local_address = local_address
self._retries = retries

self._pool: List[AsyncConnectionInterface] = []
self._requests: List[RequestStatus] = []
Expand All @@ -122,9 +122,9 @@ def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
keepalive_expiry=self._keepalive_expiry,
http1=self._http1,
http2=self._http2,
retries=self._retries,
local_address=self._local_address,
uds=self._uds,
local_address=self._local_address,
retries=self._retries,
network_backend=self._network_backend,
socket_options=self._socket_options,
)
Expand Down
50 changes: 36 additions & 14 deletions httpcore/_async/http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def __init__(
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: Optional[str] = None,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
network_backend: Optional[AsyncNetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
) -> None:
Expand Down Expand Up @@ -101,15 +101,17 @@ def __init__(
by the connection pool. Defaults to True.
http2: A boolean indicating if HTTP/2 requests should be supported by
the connection pool. Defaults to False.
retries: The maximum number of retries when trying to establish
a connection.
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
local_address: Local address to connect from. Can also be used to
connect using a particular address family. Using
`local_address="0.0.0.0"` will connect using an `AF_INET` address
(IPv4), while using `local_address="::"` will connect using an
`AF_INET6` address (IPv6).
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
retries: The maximum number of retries when trying to establish
a connection.
network_backend: A backend instance to use for handling network I/O.
socket_options: Socket options that have to be included
in the TCP socket when the connection was established.
"""
super().__init__(
ssl_context=ssl_context,
Expand All @@ -118,10 +120,10 @@ def __init__(
keepalive_expiry=keepalive_expiry,
http1=http1,
http2=http2,
network_backend=network_backend,
retries=retries,
local_address=local_address,
uds=uds,
local_address=local_address,
retries=retries,
network_backend=network_backend,
socket_options=socket_options,
)

Expand All @@ -148,22 +150,30 @@ def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
if origin.scheme == b"http":
return AsyncForwardHTTPConnection(
proxy_origin=self._proxy_url.origin,
proxy_headers=self._proxy_headers,
remote_origin=origin,
proxy_ssl_context=self._proxy_ssl_context,
proxy_headers=self._proxy_headers,
keepalive_expiry=self._keepalive_expiry,
uds=self._uds,
local_address=self._local_address,
retries=self._retries,
network_backend=self._network_backend,
proxy_ssl_context=self._proxy_ssl_context,
socket_options=self._socket_options,
)
return AsyncTunnelHTTPConnection(
proxy_origin=self._proxy_url.origin,
proxy_headers=self._proxy_headers,
remote_origin=origin,
ssl_context=self._ssl_context,
proxy_ssl_context=self._proxy_ssl_context,
proxy_headers=self._proxy_headers,
keepalive_expiry=self._keepalive_expiry,
http1=self._http1,
http2=self._http2,
uds=self._uds,
local_address=self._local_address,
retries=self._retries,
network_backend=self._network_backend,
socket_options=self._socket_options,
)


Expand All @@ -172,18 +182,24 @@ def __init__(
self,
proxy_origin: Origin,
remote_origin: Origin,
proxy_ssl_context: Optional[ssl.SSLContext] = None,
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence, None] = None,
keepalive_expiry: Optional[float] = None,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
network_backend: Optional[AsyncNetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
proxy_ssl_context: Optional[ssl.SSLContext] = None,
) -> None:
self._connection = AsyncHTTPConnection(
origin=proxy_origin,
ssl_context=proxy_ssl_context,
keepalive_expiry=keepalive_expiry,
uds=uds,
local_address=local_address,
retries=retries,
network_backend=network_backend,
socket_options=socket_options,
ssl_context=proxy_ssl_context,
)
self._proxy_origin = proxy_origin
self._proxy_headers = enforce_headers(proxy_headers, name="proxy_headers")
Expand Down Expand Up @@ -242,15 +258,21 @@ def __init__(
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
network_backend: Optional[AsyncNetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
) -> None:
self._connection: AsyncConnectionInterface = AsyncHTTPConnection(
origin=proxy_origin,
ssl_context=proxy_ssl_context,
keepalive_expiry=keepalive_expiry,
uds=uds,
local_address=local_address,
retries=retries,
network_backend=network_backend,
socket_options=socket_options,
ssl_context=proxy_ssl_context,
)
self._proxy_origin = proxy_origin
self._remote_origin = remote_origin
Expand Down
66 changes: 53 additions & 13 deletions httpcore/_async/socks_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from socksio import socks5

from .._backends.auto import AutoBackend
from .._backends.base import AsyncNetworkBackend, AsyncNetworkStream
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream
from .._exceptions import ConnectionNotAvailable, ProxyError
from .._models import URL, Origin, Request, Response, enforce_bytes, enforce_url
from .._ssl import default_ssl_context
Expand Down Expand Up @@ -118,15 +118,20 @@ def __init__(
keepalive_expiry: typing.Optional[float] = None,
http1: bool = True,
http2: bool = False,
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
network_backend: typing.Optional[AsyncNetworkBackend] = None,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
"""
A connection pool for making HTTP requests.

Parameters:
proxy_url: The URL to use when connecting to the proxy server.
For example `"http://127.0.0.1:8080/"`.
proxy_auth: Any proxy authentication as a two-tuple of
(username, password). May be either bytes or ascii-only str.
ssl_context: An SSL context to use for verifying connections.
If not specified, the default `httpcore.default_ssl_context()`
will be used.
Expand All @@ -141,15 +146,17 @@ def __init__(
by the connection pool. Defaults to True.
http2: A boolean indicating if HTTP/2 requests should be supported by
the connection pool. Defaults to False.
retries: The maximum number of retries when trying to establish
a connection.
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
local_address: Local address to connect from. Can also be used to
connect using a particular address family. Using
`local_address="0.0.0.0"` will connect using an `AF_INET` address
(IPv4), while using `local_address="::"` will connect using an
`AF_INET6` address (IPv6).
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
retries: The maximum number of retries when trying to establish
a connection.
network_backend: A backend instance to use for handling network I/O.
socket_options: Socket options that have to be included
in the TCP socket when the connection was established.
"""
super().__init__(
ssl_context=ssl_context,
Expand All @@ -158,8 +165,11 @@ def __init__(
keepalive_expiry=keepalive_expiry,
http1=http1,
http2=http2,
network_backend=network_backend,
uds=uds,
local_address=local_address,
retries=retries,
network_backend=network_backend,
socket_options=socket_options,
)
self._ssl_context = ssl_context
self._proxy_url = enforce_url(proxy_url, name="proxy_url")
Expand All @@ -183,7 +193,11 @@ def create_connection(self, origin: Origin) -> AsyncConnectionInterface:
keepalive_expiry=self._keepalive_expiry,
http1=self._http1,
http2=self._http2,
uds=self._uds,
local_address=self._local_address,
retries=self._retries,
network_backend=self._network_backend,
socket_options=self._socket_options,
)


Expand All @@ -197,7 +211,11 @@ def __init__(
keepalive_expiry: typing.Optional[float] = None,
http1: bool = True,
http2: bool = False,
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
network_backend: typing.Optional[AsyncNetworkBackend] = None,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
self._proxy_origin = proxy_origin
self._remote_origin = remote_origin
Expand All @@ -206,6 +224,10 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._http1 = http1
self._http2 = http2
self._uds = uds
self._local_address = local_address
self._retries = retries
self._socket_options = socket_options

self._network_backend: AsyncNetworkBackend = (
AutoBackend() if network_backend is None else network_backend
Expand All @@ -223,14 +245,32 @@ async def handle_async_request(self, request: Request) -> Response:
if self._connection is None:
try:
# Connect to the proxy
kwargs = {
"host": self._proxy_origin.host.decode("ascii"),
"port": self._proxy_origin.port,
"timeout": timeout,
}
async with Trace("connect_tcp", logger, request, kwargs) as trace:
stream = await self._network_backend.connect_tcp(**kwargs)
trace.return_value = stream
if self._uds is None:
kwargs = {
"host": self._proxy_origin.host.decode("ascii"),
"port": self._proxy_origin.port,
"timeout": timeout,
"local_address": self._local_address,
"socket_options": self._socket_options,
}
async with Trace(
"connect_tcp", logger, request, kwargs
) as trace:
stream = await self._network_backend.connect_tcp(**kwargs)
trace.return_value = stream
else:
kwargs = {
"path": self._uds,
"timeout": timeout,
"socket_options": self._socket_options,
}
async with Trace(
"connect_unix_socket", logger, request, kwargs
) as trace:
stream = await self._network_backend.connect_unix_socket(
**kwargs
)
trace.return_value = stream

# Connect to the remote host using socks5
kwargs = {
Expand Down
8 changes: 4 additions & 4 deletions httpcore/_sync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def __init__(
keepalive_expiry: Optional[float] = None,
http1: bool = True,
http2: bool = False,
retries: int = 0,
local_address: Optional[str] = None,
uds: Optional[str] = None,
local_address: Optional[str] = None,
retries: int = 0,
network_backend: Optional[NetworkBackend] = None,
socket_options: Optional[Iterable[SOCKET_OPTION]] = None,
) -> None:
Expand All @@ -52,9 +52,9 @@ def __init__(
self._keepalive_expiry = keepalive_expiry
self._http1 = http1
self._http2 = http2
self._retries = retries
self._local_address = local_address
self._uds = uds
self._local_address = local_address
self._retries = retries

self._network_backend: NetworkBackend = (
SyncBackend() if network_backend is None else network_backend
Expand Down
Loading