jpayne@68: from __future__ import annotations jpayne@68: jpayne@68: import errno jpayne@68: import logging jpayne@68: import queue jpayne@68: import sys jpayne@68: import typing jpayne@68: import warnings jpayne@68: import weakref jpayne@68: from socket import timeout as SocketTimeout jpayne@68: from types import TracebackType jpayne@68: jpayne@68: from ._base_connection import _TYPE_BODY jpayne@68: from ._collections import HTTPHeaderDict jpayne@68: from ._request_methods import RequestMethods jpayne@68: from .connection import ( jpayne@68: BaseSSLError, jpayne@68: BrokenPipeError, jpayne@68: DummyConnection, jpayne@68: HTTPConnection, jpayne@68: HTTPException, jpayne@68: HTTPSConnection, jpayne@68: ProxyConfig, jpayne@68: _wrap_proxy_error, jpayne@68: ) jpayne@68: from .connection import port_by_scheme as port_by_scheme jpayne@68: from .exceptions import ( jpayne@68: ClosedPoolError, jpayne@68: EmptyPoolError, jpayne@68: FullPoolError, jpayne@68: HostChangedError, jpayne@68: InsecureRequestWarning, jpayne@68: LocationValueError, jpayne@68: MaxRetryError, jpayne@68: NewConnectionError, jpayne@68: ProtocolError, jpayne@68: ProxyError, jpayne@68: ReadTimeoutError, jpayne@68: SSLError, jpayne@68: TimeoutError, jpayne@68: ) jpayne@68: from .response import BaseHTTPResponse jpayne@68: from .util.connection import is_connection_dropped jpayne@68: from .util.proxy import connection_requires_http_tunnel jpayne@68: from .util.request import _TYPE_BODY_POSITION, set_file_position jpayne@68: from .util.retry import Retry jpayne@68: from .util.ssl_match_hostname import CertificateError jpayne@68: from .util.timeout import _DEFAULT_TIMEOUT, _TYPE_DEFAULT, Timeout jpayne@68: from .util.url import Url, _encode_target jpayne@68: from .util.url import _normalize_host as normalize_host jpayne@68: from .util.url import parse_url jpayne@68: from .util.util import to_str jpayne@68: jpayne@68: if typing.TYPE_CHECKING: jpayne@68: import ssl jpayne@68: jpayne@68: from typing_extensions import Self jpayne@68: jpayne@68: from ._base_connection import BaseHTTPConnection, BaseHTTPSConnection jpayne@68: jpayne@68: log = logging.getLogger(__name__) jpayne@68: jpayne@68: _TYPE_TIMEOUT = typing.Union[Timeout, float, _TYPE_DEFAULT, None] jpayne@68: jpayne@68: jpayne@68: # Pool objects jpayne@68: class ConnectionPool: jpayne@68: """ jpayne@68: Base class for all connection pools, such as jpayne@68: :class:`.HTTPConnectionPool` and :class:`.HTTPSConnectionPool`. jpayne@68: jpayne@68: .. note:: jpayne@68: ConnectionPool.urlopen() does not normalize or percent-encode target URIs jpayne@68: which is useful if your target server doesn't support percent-encoded jpayne@68: target URIs. jpayne@68: """ jpayne@68: jpayne@68: scheme: str | None = None jpayne@68: QueueCls = queue.LifoQueue jpayne@68: jpayne@68: def __init__(self, host: str, port: int | None = None) -> None: jpayne@68: if not host: jpayne@68: raise LocationValueError("No host specified.") jpayne@68: jpayne@68: self.host = _normalize_host(host, scheme=self.scheme) jpayne@68: self.port = port jpayne@68: jpayne@68: # This property uses 'normalize_host()' (not '_normalize_host()') jpayne@68: # to avoid removing square braces around IPv6 addresses. jpayne@68: # This value is sent to `HTTPConnection.set_tunnel()` if called jpayne@68: # because square braces are required for HTTP CONNECT tunneling. jpayne@68: self._tunnel_host = normalize_host(host, scheme=self.scheme).lower() jpayne@68: jpayne@68: def __str__(self) -> str: jpayne@68: return f"{type(self).__name__}(host={self.host!r}, port={self.port!r})" jpayne@68: jpayne@68: def __enter__(self) -> Self: jpayne@68: return self jpayne@68: jpayne@68: def __exit__( jpayne@68: self, jpayne@68: exc_type: type[BaseException] | None, jpayne@68: exc_val: BaseException | None, jpayne@68: exc_tb: TracebackType | None, jpayne@68: ) -> typing.Literal[False]: jpayne@68: self.close() jpayne@68: # Return False to re-raise any potential exceptions jpayne@68: return False jpayne@68: jpayne@68: def close(self) -> None: jpayne@68: """ jpayne@68: Close all pooled connections and disable the pool. jpayne@68: """ jpayne@68: jpayne@68: jpayne@68: # This is taken from http://hg.python.org/cpython/file/7aaba721ebc0/Lib/socket.py#l252 jpayne@68: _blocking_errnos = {errno.EAGAIN, errno.EWOULDBLOCK} jpayne@68: jpayne@68: jpayne@68: class HTTPConnectionPool(ConnectionPool, RequestMethods): jpayne@68: """ jpayne@68: Thread-safe connection pool for one host. jpayne@68: jpayne@68: :param host: jpayne@68: Host used for this HTTP Connection (e.g. "localhost"), passed into jpayne@68: :class:`http.client.HTTPConnection`. jpayne@68: jpayne@68: :param port: jpayne@68: Port used for this HTTP Connection (None is equivalent to 80), passed jpayne@68: into :class:`http.client.HTTPConnection`. jpayne@68: jpayne@68: :param timeout: jpayne@68: Socket timeout in seconds for each individual connection. This can jpayne@68: be a float or integer, which sets the timeout for the HTTP request, jpayne@68: or an instance of :class:`urllib3.util.Timeout` which gives you more jpayne@68: fine-grained control over request timeouts. After the constructor has jpayne@68: been parsed, this is always a `urllib3.util.Timeout` object. jpayne@68: jpayne@68: :param maxsize: jpayne@68: Number of connections to save that can be reused. More than 1 is useful jpayne@68: in multithreaded situations. If ``block`` is set to False, more jpayne@68: connections will be created but they will not be saved once they've jpayne@68: been used. jpayne@68: jpayne@68: :param block: jpayne@68: If set to True, no more than ``maxsize`` connections will be used at jpayne@68: a time. When no free connections are available, the call will block jpayne@68: until a connection has been released. This is a useful side effect for jpayne@68: particular multithreaded situations where one does not want to use more jpayne@68: than maxsize connections per host to prevent flooding. jpayne@68: jpayne@68: :param headers: jpayne@68: Headers to include with all requests, unless other headers are given jpayne@68: explicitly. jpayne@68: jpayne@68: :param retries: jpayne@68: Retry configuration to use by default with requests in this pool. jpayne@68: jpayne@68: :param _proxy: jpayne@68: Parsed proxy URL, should not be used directly, instead, see jpayne@68: :class:`urllib3.ProxyManager` jpayne@68: jpayne@68: :param _proxy_headers: jpayne@68: A dictionary with proxy headers, should not be used directly, jpayne@68: instead, see :class:`urllib3.ProxyManager` jpayne@68: jpayne@68: :param \\**conn_kw: jpayne@68: Additional parameters are used to create fresh :class:`urllib3.connection.HTTPConnection`, jpayne@68: :class:`urllib3.connection.HTTPSConnection` instances. jpayne@68: """ jpayne@68: jpayne@68: scheme = "http" jpayne@68: ConnectionCls: ( jpayne@68: type[BaseHTTPConnection] | type[BaseHTTPSConnection] jpayne@68: ) = HTTPConnection jpayne@68: jpayne@68: def __init__( jpayne@68: self, jpayne@68: host: str, jpayne@68: port: int | None = None, jpayne@68: timeout: _TYPE_TIMEOUT | None = _DEFAULT_TIMEOUT, jpayne@68: maxsize: int = 1, jpayne@68: block: bool = False, jpayne@68: headers: typing.Mapping[str, str] | None = None, jpayne@68: retries: Retry | bool | int | None = None, jpayne@68: _proxy: Url | None = None, jpayne@68: _proxy_headers: typing.Mapping[str, str] | None = None, jpayne@68: _proxy_config: ProxyConfig | None = None, jpayne@68: **conn_kw: typing.Any, jpayne@68: ): jpayne@68: ConnectionPool.__init__(self, host, port) jpayne@68: RequestMethods.__init__(self, headers) jpayne@68: jpayne@68: if not isinstance(timeout, Timeout): jpayne@68: timeout = Timeout.from_float(timeout) jpayne@68: jpayne@68: if retries is None: jpayne@68: retries = Retry.DEFAULT jpayne@68: jpayne@68: self.timeout = timeout jpayne@68: self.retries = retries jpayne@68: jpayne@68: self.pool: queue.LifoQueue[typing.Any] | None = self.QueueCls(maxsize) jpayne@68: self.block = block jpayne@68: jpayne@68: self.proxy = _proxy jpayne@68: self.proxy_headers = _proxy_headers or {} jpayne@68: self.proxy_config = _proxy_config jpayne@68: jpayne@68: # Fill the queue up so that doing get() on it will block properly jpayne@68: for _ in range(maxsize): jpayne@68: self.pool.put(None) jpayne@68: jpayne@68: # These are mostly for testing and debugging purposes. jpayne@68: self.num_connections = 0 jpayne@68: self.num_requests = 0 jpayne@68: self.conn_kw = conn_kw jpayne@68: jpayne@68: if self.proxy: jpayne@68: # Enable Nagle's algorithm for proxies, to avoid packet fragmentation. jpayne@68: # We cannot know if the user has added default socket options, so we cannot replace the jpayne@68: # list. jpayne@68: self.conn_kw.setdefault("socket_options", []) jpayne@68: jpayne@68: self.conn_kw["proxy"] = self.proxy jpayne@68: self.conn_kw["proxy_config"] = self.proxy_config jpayne@68: jpayne@68: # Do not pass 'self' as callback to 'finalize'. jpayne@68: # Then the 'finalize' would keep an endless living (leak) to self. jpayne@68: # By just passing a reference to the pool allows the garbage collector jpayne@68: # to free self if nobody else has a reference to it. jpayne@68: pool = self.pool jpayne@68: jpayne@68: # Close all the HTTPConnections in the pool before the jpayne@68: # HTTPConnectionPool object is garbage collected. jpayne@68: weakref.finalize(self, _close_pool_connections, pool) jpayne@68: jpayne@68: def _new_conn(self) -> BaseHTTPConnection: jpayne@68: """ jpayne@68: Return a fresh :class:`HTTPConnection`. jpayne@68: """ jpayne@68: self.num_connections += 1 jpayne@68: log.debug( jpayne@68: "Starting new HTTP connection (%d): %s:%s", jpayne@68: self.num_connections, jpayne@68: self.host, jpayne@68: self.port or "80", jpayne@68: ) jpayne@68: jpayne@68: conn = self.ConnectionCls( jpayne@68: host=self.host, jpayne@68: port=self.port, jpayne@68: timeout=self.timeout.connect_timeout, jpayne@68: **self.conn_kw, jpayne@68: ) jpayne@68: return conn jpayne@68: jpayne@68: def _get_conn(self, timeout: float | None = None) -> BaseHTTPConnection: jpayne@68: """ jpayne@68: Get a connection. Will return a pooled connection if one is available. jpayne@68: jpayne@68: If no connections are available and :prop:`.block` is ``False``, then a jpayne@68: fresh connection is returned. jpayne@68: jpayne@68: :param timeout: jpayne@68: Seconds to wait before giving up and raising jpayne@68: :class:`urllib3.exceptions.EmptyPoolError` if the pool is empty and jpayne@68: :prop:`.block` is ``True``. jpayne@68: """ jpayne@68: conn = None jpayne@68: jpayne@68: if self.pool is None: jpayne@68: raise ClosedPoolError(self, "Pool is closed.") jpayne@68: jpayne@68: try: jpayne@68: conn = self.pool.get(block=self.block, timeout=timeout) jpayne@68: jpayne@68: except AttributeError: # self.pool is None jpayne@68: raise ClosedPoolError(self, "Pool is closed.") from None # Defensive: jpayne@68: jpayne@68: except queue.Empty: jpayne@68: if self.block: jpayne@68: raise EmptyPoolError( jpayne@68: self, jpayne@68: "Pool is empty and a new connection can't be opened due to blocking mode.", jpayne@68: ) from None jpayne@68: pass # Oh well, we'll create a new connection then jpayne@68: jpayne@68: # If this is a persistent connection, check if it got disconnected jpayne@68: if conn and is_connection_dropped(conn): jpayne@68: log.debug("Resetting dropped connection: %s", self.host) jpayne@68: conn.close() jpayne@68: jpayne@68: return conn or self._new_conn() jpayne@68: jpayne@68: def _put_conn(self, conn: BaseHTTPConnection | None) -> None: jpayne@68: """ jpayne@68: Put a connection back into the pool. jpayne@68: jpayne@68: :param conn: jpayne@68: Connection object for the current host and port as returned by jpayne@68: :meth:`._new_conn` or :meth:`._get_conn`. jpayne@68: jpayne@68: If the pool is already full, the connection is closed and discarded jpayne@68: because we exceeded maxsize. If connections are discarded frequently, jpayne@68: then maxsize should be increased. jpayne@68: jpayne@68: If the pool is closed, then the connection will be closed and discarded. jpayne@68: """ jpayne@68: if self.pool is not None: jpayne@68: try: jpayne@68: self.pool.put(conn, block=False) jpayne@68: return # Everything is dandy, done. jpayne@68: except AttributeError: jpayne@68: # self.pool is None. jpayne@68: pass jpayne@68: except queue.Full: jpayne@68: # Connection never got put back into the pool, close it. jpayne@68: if conn: jpayne@68: conn.close() jpayne@68: jpayne@68: if self.block: jpayne@68: # This should never happen if you got the conn from self._get_conn jpayne@68: raise FullPoolError( jpayne@68: self, jpayne@68: "Pool reached maximum size and no more connections are allowed.", jpayne@68: ) from None jpayne@68: jpayne@68: log.warning( jpayne@68: "Connection pool is full, discarding connection: %s. Connection pool size: %s", jpayne@68: self.host, jpayne@68: self.pool.qsize(), jpayne@68: ) jpayne@68: jpayne@68: # Connection never got put back into the pool, close it. jpayne@68: if conn: jpayne@68: conn.close() jpayne@68: jpayne@68: def _validate_conn(self, conn: BaseHTTPConnection) -> None: jpayne@68: """ jpayne@68: Called right before a request is made, after the socket is created. jpayne@68: """ jpayne@68: jpayne@68: def _prepare_proxy(self, conn: BaseHTTPConnection) -> None: jpayne@68: # Nothing to do for HTTP connections. jpayne@68: pass jpayne@68: jpayne@68: def _get_timeout(self, timeout: _TYPE_TIMEOUT) -> Timeout: jpayne@68: """Helper that always returns a :class:`urllib3.util.Timeout`""" jpayne@68: if timeout is _DEFAULT_TIMEOUT: jpayne@68: return self.timeout.clone() jpayne@68: jpayne@68: if isinstance(timeout, Timeout): jpayne@68: return timeout.clone() jpayne@68: else: jpayne@68: # User passed us an int/float. This is for backwards compatibility, jpayne@68: # can be removed later jpayne@68: return Timeout.from_float(timeout) jpayne@68: jpayne@68: def _raise_timeout( jpayne@68: self, jpayne@68: err: BaseSSLError | OSError | SocketTimeout, jpayne@68: url: str, jpayne@68: timeout_value: _TYPE_TIMEOUT | None, jpayne@68: ) -> None: jpayne@68: """Is the error actually a timeout? Will raise a ReadTimeout or pass""" jpayne@68: jpayne@68: if isinstance(err, SocketTimeout): jpayne@68: raise ReadTimeoutError( jpayne@68: self, url, f"Read timed out. (read timeout={timeout_value})" jpayne@68: ) from err jpayne@68: jpayne@68: # See the above comment about EAGAIN in Python 3. jpayne@68: if hasattr(err, "errno") and err.errno in _blocking_errnos: jpayne@68: raise ReadTimeoutError( jpayne@68: self, url, f"Read timed out. (read timeout={timeout_value})" jpayne@68: ) from err jpayne@68: jpayne@68: def _make_request( jpayne@68: self, jpayne@68: conn: BaseHTTPConnection, jpayne@68: method: str, jpayne@68: url: str, jpayne@68: body: _TYPE_BODY | None = None, jpayne@68: headers: typing.Mapping[str, str] | None = None, jpayne@68: retries: Retry | None = None, jpayne@68: timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT, jpayne@68: chunked: bool = False, jpayne@68: response_conn: BaseHTTPConnection | None = None, jpayne@68: preload_content: bool = True, jpayne@68: decode_content: bool = True, jpayne@68: enforce_content_length: bool = True, jpayne@68: ) -> BaseHTTPResponse: jpayne@68: """ jpayne@68: Perform a request on a given urllib connection object taken from our jpayne@68: pool. jpayne@68: jpayne@68: :param conn: jpayne@68: a connection from one of our connection pools jpayne@68: jpayne@68: :param method: jpayne@68: HTTP request method (such as GET, POST, PUT, etc.) jpayne@68: jpayne@68: :param url: jpayne@68: The URL to perform the request on. jpayne@68: jpayne@68: :param body: jpayne@68: Data to send in the request body, either :class:`str`, :class:`bytes`, jpayne@68: an iterable of :class:`str`/:class:`bytes`, or a file-like object. jpayne@68: jpayne@68: :param headers: jpayne@68: Dictionary of custom headers to send, such as User-Agent, jpayne@68: If-None-Match, etc. If None, pool headers are used. If provided, jpayne@68: these headers completely replace any pool-specific headers. jpayne@68: jpayne@68: :param retries: jpayne@68: Configure the number of retries to allow before raising a jpayne@68: :class:`~urllib3.exceptions.MaxRetryError` exception. jpayne@68: jpayne@68: Pass ``None`` to retry until you receive a response. Pass a jpayne@68: :class:`~urllib3.util.retry.Retry` object for fine-grained control jpayne@68: over different types of retries. jpayne@68: Pass an integer number to retry connection errors that many times, jpayne@68: but no other types of errors. Pass zero to never retry. jpayne@68: jpayne@68: If ``False``, then retries are disabled and any exception is raised jpayne@68: immediately. Also, instead of raising a MaxRetryError on redirects, jpayne@68: the redirect response will be returned. jpayne@68: jpayne@68: :type retries: :class:`~urllib3.util.retry.Retry`, False, or an int. jpayne@68: jpayne@68: :param timeout: jpayne@68: If specified, overrides the default timeout for this one jpayne@68: request. It may be a float (in seconds) or an instance of jpayne@68: :class:`urllib3.util.Timeout`. jpayne@68: jpayne@68: :param chunked: jpayne@68: If True, urllib3 will send the body using chunked transfer jpayne@68: encoding. Otherwise, urllib3 will send the body using the standard jpayne@68: content-length form. Defaults to False. jpayne@68: jpayne@68: :param response_conn: jpayne@68: Set this to ``None`` if you will handle releasing the connection or jpayne@68: set the connection to have the response release it. jpayne@68: jpayne@68: :param preload_content: jpayne@68: If True, the response's body will be preloaded during construction. jpayne@68: jpayne@68: :param decode_content: jpayne@68: If True, will attempt to decode the body based on the jpayne@68: 'content-encoding' header. jpayne@68: jpayne@68: :param enforce_content_length: jpayne@68: Enforce content length checking. Body returned by server must match jpayne@68: value of Content-Length header, if present. Otherwise, raise error. jpayne@68: """ jpayne@68: self.num_requests += 1 jpayne@68: jpayne@68: timeout_obj = self._get_timeout(timeout) jpayne@68: timeout_obj.start_connect() jpayne@68: conn.timeout = Timeout.resolve_default_timeout(timeout_obj.connect_timeout) jpayne@68: jpayne@68: try: jpayne@68: # Trigger any extra validation we need to do. jpayne@68: try: jpayne@68: self._validate_conn(conn) jpayne@68: except (SocketTimeout, BaseSSLError) as e: jpayne@68: self._raise_timeout(err=e, url=url, timeout_value=conn.timeout) jpayne@68: raise jpayne@68: jpayne@68: # _validate_conn() starts the connection to an HTTPS proxy jpayne@68: # so we need to wrap errors with 'ProxyError' here too. jpayne@68: except ( jpayne@68: OSError, jpayne@68: NewConnectionError, jpayne@68: TimeoutError, jpayne@68: BaseSSLError, jpayne@68: CertificateError, jpayne@68: SSLError, jpayne@68: ) as e: jpayne@68: new_e: Exception = e jpayne@68: if isinstance(e, (BaseSSLError, CertificateError)): jpayne@68: new_e = SSLError(e) jpayne@68: # If the connection didn't successfully connect to it's proxy jpayne@68: # then there jpayne@68: if isinstance( jpayne@68: new_e, (OSError, NewConnectionError, TimeoutError, SSLError) jpayne@68: ) and (conn and conn.proxy and not conn.has_connected_to_proxy): jpayne@68: new_e = _wrap_proxy_error(new_e, conn.proxy.scheme) jpayne@68: raise new_e jpayne@68: jpayne@68: # conn.request() calls http.client.*.request, not the method in jpayne@68: # urllib3.request. It also calls makefile (recv) on the socket. jpayne@68: try: jpayne@68: conn.request( jpayne@68: method, jpayne@68: url, jpayne@68: body=body, jpayne@68: headers=headers, jpayne@68: chunked=chunked, jpayne@68: preload_content=preload_content, jpayne@68: decode_content=decode_content, jpayne@68: enforce_content_length=enforce_content_length, jpayne@68: ) jpayne@68: jpayne@68: # We are swallowing BrokenPipeError (errno.EPIPE) since the server is jpayne@68: # legitimately able to close the connection after sending a valid response. jpayne@68: # With this behaviour, the received response is still readable. jpayne@68: except BrokenPipeError: jpayne@68: pass jpayne@68: except OSError as e: jpayne@68: # MacOS/Linux jpayne@68: # EPROTOTYPE and ECONNRESET are needed on macOS jpayne@68: # https://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/ jpayne@68: # Condition changed later to emit ECONNRESET instead of only EPROTOTYPE. jpayne@68: if e.errno != errno.EPROTOTYPE and e.errno != errno.ECONNRESET: jpayne@68: raise jpayne@68: jpayne@68: # Reset the timeout for the recv() on the socket jpayne@68: read_timeout = timeout_obj.read_timeout jpayne@68: jpayne@68: if not conn.is_closed: jpayne@68: # In Python 3 socket.py will catch EAGAIN and return None when you jpayne@68: # try and read into the file pointer created by http.client, which jpayne@68: # instead raises a BadStatusLine exception. Instead of catching jpayne@68: # the exception and assuming all BadStatusLine exceptions are read jpayne@68: # timeouts, check for a zero timeout before making the request. jpayne@68: if read_timeout == 0: jpayne@68: raise ReadTimeoutError( jpayne@68: self, url, f"Read timed out. (read timeout={read_timeout})" jpayne@68: ) jpayne@68: conn.timeout = read_timeout jpayne@68: jpayne@68: # Receive the response from the server jpayne@68: try: jpayne@68: response = conn.getresponse() jpayne@68: except (BaseSSLError, OSError) as e: jpayne@68: self._raise_timeout(err=e, url=url, timeout_value=read_timeout) jpayne@68: raise jpayne@68: jpayne@68: # Set properties that are used by the pooling layer. jpayne@68: response.retries = retries jpayne@68: response._connection = response_conn # type: ignore[attr-defined] jpayne@68: response._pool = self # type: ignore[attr-defined] jpayne@68: jpayne@68: log.debug( jpayne@68: '%s://%s:%s "%s %s HTTP/%s" %s %s', jpayne@68: self.scheme, jpayne@68: self.host, jpayne@68: self.port, jpayne@68: method, jpayne@68: url, jpayne@68: response.version, jpayne@68: response.status, jpayne@68: response.length_remaining, jpayne@68: ) jpayne@68: jpayne@68: return response jpayne@68: jpayne@68: def close(self) -> None: jpayne@68: """ jpayne@68: Close all pooled connections and disable the pool. jpayne@68: """ jpayne@68: if self.pool is None: jpayne@68: return jpayne@68: # Disable access to the pool jpayne@68: old_pool, self.pool = self.pool, None jpayne@68: jpayne@68: # Close all the HTTPConnections in the pool. jpayne@68: _close_pool_connections(old_pool) jpayne@68: jpayne@68: def is_same_host(self, url: str) -> bool: jpayne@68: """ jpayne@68: Check if the given ``url`` is a member of the same host as this jpayne@68: connection pool. jpayne@68: """ jpayne@68: if url.startswith("/"): jpayne@68: return True jpayne@68: jpayne@68: # TODO: Add optional support for socket.gethostbyname checking. jpayne@68: scheme, _, host, port, *_ = parse_url(url) jpayne@68: scheme = scheme or "http" jpayne@68: if host is not None: jpayne@68: host = _normalize_host(host, scheme=scheme) jpayne@68: jpayne@68: # Use explicit default port for comparison when none is given jpayne@68: if self.port and not port: jpayne@68: port = port_by_scheme.get(scheme) jpayne@68: elif not self.port and port == port_by_scheme.get(scheme): jpayne@68: port = None jpayne@68: jpayne@68: return (scheme, host, port) == (self.scheme, self.host, self.port) jpayne@68: jpayne@68: def urlopen( # type: ignore[override] jpayne@68: self, jpayne@68: method: str, jpayne@68: url: str, jpayne@68: body: _TYPE_BODY | None = None, jpayne@68: headers: typing.Mapping[str, str] | None = None, jpayne@68: retries: Retry | bool | int | None = None, jpayne@68: redirect: bool = True, jpayne@68: assert_same_host: bool = True, jpayne@68: timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT, jpayne@68: pool_timeout: int | None = None, jpayne@68: release_conn: bool | None = None, jpayne@68: chunked: bool = False, jpayne@68: body_pos: _TYPE_BODY_POSITION | None = None, jpayne@68: preload_content: bool = True, jpayne@68: decode_content: bool = True, jpayne@68: **response_kw: typing.Any, jpayne@68: ) -> BaseHTTPResponse: jpayne@68: """ jpayne@68: Get a connection from the pool and perform an HTTP request. This is the jpayne@68: lowest level call for making a request, so you'll need to specify all jpayne@68: the raw details. jpayne@68: jpayne@68: .. note:: jpayne@68: jpayne@68: More commonly, it's appropriate to use a convenience method jpayne@68: such as :meth:`request`. jpayne@68: jpayne@68: .. note:: jpayne@68: jpayne@68: `release_conn` will only behave as expected if jpayne@68: `preload_content=False` because we want to make jpayne@68: `preload_content=False` the default behaviour someday soon without jpayne@68: breaking backwards compatibility. jpayne@68: jpayne@68: :param method: jpayne@68: HTTP request method (such as GET, POST, PUT, etc.) jpayne@68: jpayne@68: :param url: jpayne@68: The URL to perform the request on. jpayne@68: jpayne@68: :param body: jpayne@68: Data to send in the request body, either :class:`str`, :class:`bytes`, jpayne@68: an iterable of :class:`str`/:class:`bytes`, or a file-like object. jpayne@68: jpayne@68: :param headers: jpayne@68: Dictionary of custom headers to send, such as User-Agent, jpayne@68: If-None-Match, etc. If None, pool headers are used. If provided, jpayne@68: these headers completely replace any pool-specific headers. jpayne@68: jpayne@68: :param retries: jpayne@68: Configure the number of retries to allow before raising a jpayne@68: :class:`~urllib3.exceptions.MaxRetryError` exception. jpayne@68: jpayne@68: If ``None`` (default) will retry 3 times, see ``Retry.DEFAULT``. Pass a jpayne@68: :class:`~urllib3.util.retry.Retry` object for fine-grained control jpayne@68: over different types of retries. jpayne@68: Pass an integer number to retry connection errors that many times, jpayne@68: but no other types of errors. Pass zero to never retry. jpayne@68: jpayne@68: If ``False``, then retries are disabled and any exception is raised jpayne@68: immediately. Also, instead of raising a MaxRetryError on redirects, jpayne@68: the redirect response will be returned. jpayne@68: jpayne@68: :type retries: :class:`~urllib3.util.retry.Retry`, False, or an int. jpayne@68: jpayne@68: :param redirect: jpayne@68: If True, automatically handle redirects (status codes 301, 302, jpayne@68: 303, 307, 308). Each redirect counts as a retry. Disabling retries jpayne@68: will disable redirect, too. jpayne@68: jpayne@68: :param assert_same_host: jpayne@68: If ``True``, will make sure that the host of the pool requests is jpayne@68: consistent else will raise HostChangedError. When ``False``, you can jpayne@68: use the pool on an HTTP proxy and request foreign hosts. jpayne@68: jpayne@68: :param timeout: jpayne@68: If specified, overrides the default timeout for this one jpayne@68: request. It may be a float (in seconds) or an instance of jpayne@68: :class:`urllib3.util.Timeout`. jpayne@68: jpayne@68: :param pool_timeout: jpayne@68: If set and the pool is set to block=True, then this method will jpayne@68: block for ``pool_timeout`` seconds and raise EmptyPoolError if no jpayne@68: connection is available within the time period. jpayne@68: jpayne@68: :param bool preload_content: jpayne@68: If True, the response's body will be preloaded into memory. jpayne@68: jpayne@68: :param bool decode_content: jpayne@68: If True, will attempt to decode the body based on the jpayne@68: 'content-encoding' header. jpayne@68: jpayne@68: :param release_conn: jpayne@68: If False, then the urlopen call will not release the connection jpayne@68: back into the pool once a response is received (but will release if jpayne@68: you read the entire contents of the response such as when jpayne@68: `preload_content=True`). This is useful if you're not preloading jpayne@68: the response's content immediately. You will need to call jpayne@68: ``r.release_conn()`` on the response ``r`` to return the connection jpayne@68: back into the pool. If None, it takes the value of ``preload_content`` jpayne@68: which defaults to ``True``. jpayne@68: jpayne@68: :param bool chunked: jpayne@68: If True, urllib3 will send the body using chunked transfer jpayne@68: encoding. Otherwise, urllib3 will send the body using the standard jpayne@68: content-length form. Defaults to False. jpayne@68: jpayne@68: :param int body_pos: jpayne@68: Position to seek to in file-like body in the event of a retry or jpayne@68: redirect. Typically this won't need to be set because urllib3 will jpayne@68: auto-populate the value when needed. jpayne@68: """ jpayne@68: parsed_url = parse_url(url) jpayne@68: destination_scheme = parsed_url.scheme jpayne@68: jpayne@68: if headers is None: jpayne@68: headers = self.headers jpayne@68: jpayne@68: if not isinstance(retries, Retry): jpayne@68: retries = Retry.from_int(retries, redirect=redirect, default=self.retries) jpayne@68: jpayne@68: if release_conn is None: jpayne@68: release_conn = preload_content jpayne@68: jpayne@68: # Check host jpayne@68: if assert_same_host and not self.is_same_host(url): jpayne@68: raise HostChangedError(self, url, retries) jpayne@68: jpayne@68: # Ensure that the URL we're connecting to is properly encoded jpayne@68: if url.startswith("/"): jpayne@68: url = to_str(_encode_target(url)) jpayne@68: else: jpayne@68: url = to_str(parsed_url.url) jpayne@68: jpayne@68: conn = None jpayne@68: jpayne@68: # Track whether `conn` needs to be released before jpayne@68: # returning/raising/recursing. Update this variable if necessary, and jpayne@68: # leave `release_conn` constant throughout the function. That way, if jpayne@68: # the function recurses, the original value of `release_conn` will be jpayne@68: # passed down into the recursive call, and its value will be respected. jpayne@68: # jpayne@68: # See issue #651 [1] for details. jpayne@68: # jpayne@68: # [1] jpayne@68: release_this_conn = release_conn jpayne@68: jpayne@68: http_tunnel_required = connection_requires_http_tunnel( jpayne@68: self.proxy, self.proxy_config, destination_scheme jpayne@68: ) jpayne@68: jpayne@68: # Merge the proxy headers. Only done when not using HTTP CONNECT. We jpayne@68: # have to copy the headers dict so we can safely change it without those jpayne@68: # changes being reflected in anyone else's copy. jpayne@68: if not http_tunnel_required: jpayne@68: headers = headers.copy() # type: ignore[attr-defined] jpayne@68: headers.update(self.proxy_headers) # type: ignore[union-attr] jpayne@68: jpayne@68: # Must keep the exception bound to a separate variable or else Python 3 jpayne@68: # complains about UnboundLocalError. jpayne@68: err = None jpayne@68: jpayne@68: # Keep track of whether we cleanly exited the except block. This jpayne@68: # ensures we do proper cleanup in finally. jpayne@68: clean_exit = False jpayne@68: jpayne@68: # Rewind body position, if needed. Record current position jpayne@68: # for future rewinds in the event of a redirect/retry. jpayne@68: body_pos = set_file_position(body, body_pos) jpayne@68: jpayne@68: try: jpayne@68: # Request a connection from the queue. jpayne@68: timeout_obj = self._get_timeout(timeout) jpayne@68: conn = self._get_conn(timeout=pool_timeout) jpayne@68: jpayne@68: conn.timeout = timeout_obj.connect_timeout # type: ignore[assignment] jpayne@68: jpayne@68: # Is this a closed/new connection that requires CONNECT tunnelling? jpayne@68: if self.proxy is not None and http_tunnel_required and conn.is_closed: jpayne@68: try: jpayne@68: self._prepare_proxy(conn) jpayne@68: except (BaseSSLError, OSError, SocketTimeout) as e: jpayne@68: self._raise_timeout( jpayne@68: err=e, url=self.proxy.url, timeout_value=conn.timeout jpayne@68: ) jpayne@68: raise jpayne@68: jpayne@68: # If we're going to release the connection in ``finally:``, then jpayne@68: # the response doesn't need to know about the connection. Otherwise jpayne@68: # it will also try to release it and we'll have a double-release jpayne@68: # mess. jpayne@68: response_conn = conn if not release_conn else None jpayne@68: jpayne@68: # Make the request on the HTTPConnection object jpayne@68: response = self._make_request( jpayne@68: conn, jpayne@68: method, jpayne@68: url, jpayne@68: timeout=timeout_obj, jpayne@68: body=body, jpayne@68: headers=headers, jpayne@68: chunked=chunked, jpayne@68: retries=retries, jpayne@68: response_conn=response_conn, jpayne@68: preload_content=preload_content, jpayne@68: decode_content=decode_content, jpayne@68: **response_kw, jpayne@68: ) jpayne@68: jpayne@68: # Everything went great! jpayne@68: clean_exit = True jpayne@68: jpayne@68: except EmptyPoolError: jpayne@68: # Didn't get a connection from the pool, no need to clean up jpayne@68: clean_exit = True jpayne@68: release_this_conn = False jpayne@68: raise jpayne@68: jpayne@68: except ( jpayne@68: TimeoutError, jpayne@68: HTTPException, jpayne@68: OSError, jpayne@68: ProtocolError, jpayne@68: BaseSSLError, jpayne@68: SSLError, jpayne@68: CertificateError, jpayne@68: ProxyError, jpayne@68: ) as e: jpayne@68: # Discard the connection for these exceptions. It will be jpayne@68: # replaced during the next _get_conn() call. jpayne@68: clean_exit = False jpayne@68: new_e: Exception = e jpayne@68: if isinstance(e, (BaseSSLError, CertificateError)): jpayne@68: new_e = SSLError(e) jpayne@68: if isinstance( jpayne@68: new_e, jpayne@68: ( jpayne@68: OSError, jpayne@68: NewConnectionError, jpayne@68: TimeoutError, jpayne@68: SSLError, jpayne@68: HTTPException, jpayne@68: ), jpayne@68: ) and (conn and conn.proxy and not conn.has_connected_to_proxy): jpayne@68: new_e = _wrap_proxy_error(new_e, conn.proxy.scheme) jpayne@68: elif isinstance(new_e, (OSError, HTTPException)): jpayne@68: new_e = ProtocolError("Connection aborted.", new_e) jpayne@68: jpayne@68: retries = retries.increment( jpayne@68: method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2] jpayne@68: ) jpayne@68: retries.sleep() jpayne@68: jpayne@68: # Keep track of the error for the retry warning. jpayne@68: err = e jpayne@68: jpayne@68: finally: jpayne@68: if not clean_exit: jpayne@68: # We hit some kind of exception, handled or otherwise. We need jpayne@68: # to throw the connection away unless explicitly told not to. jpayne@68: # Close the connection, set the variable to None, and make sure jpayne@68: # we put the None back in the pool to avoid leaking it. jpayne@68: if conn: jpayne@68: conn.close() jpayne@68: conn = None jpayne@68: release_this_conn = True jpayne@68: jpayne@68: if release_this_conn: jpayne@68: # Put the connection back to be reused. If the connection is jpayne@68: # expired then it will be None, which will get replaced with a jpayne@68: # fresh connection during _get_conn. jpayne@68: self._put_conn(conn) jpayne@68: jpayne@68: if not conn: jpayne@68: # Try again jpayne@68: log.warning( jpayne@68: "Retrying (%r) after connection broken by '%r': %s", retries, err, url jpayne@68: ) jpayne@68: return self.urlopen( jpayne@68: method, jpayne@68: url, jpayne@68: body, jpayne@68: headers, jpayne@68: retries, jpayne@68: redirect, jpayne@68: assert_same_host, jpayne@68: timeout=timeout, jpayne@68: pool_timeout=pool_timeout, jpayne@68: release_conn=release_conn, jpayne@68: chunked=chunked, jpayne@68: body_pos=body_pos, jpayne@68: preload_content=preload_content, jpayne@68: decode_content=decode_content, jpayne@68: **response_kw, jpayne@68: ) jpayne@68: jpayne@68: # Handle redirect? jpayne@68: redirect_location = redirect and response.get_redirect_location() jpayne@68: if redirect_location: jpayne@68: if response.status == 303: jpayne@68: # Change the method according to RFC 9110, Section 15.4.4. jpayne@68: method = "GET" jpayne@68: # And lose the body not to transfer anything sensitive. jpayne@68: body = None jpayne@68: headers = HTTPHeaderDict(headers)._prepare_for_method_change() jpayne@68: jpayne@68: try: jpayne@68: retries = retries.increment(method, url, response=response, _pool=self) jpayne@68: except MaxRetryError: jpayne@68: if retries.raise_on_redirect: jpayne@68: response.drain_conn() jpayne@68: raise jpayne@68: return response jpayne@68: jpayne@68: response.drain_conn() jpayne@68: retries.sleep_for_retry(response) jpayne@68: log.debug("Redirecting %s -> %s", url, redirect_location) jpayne@68: return self.urlopen( jpayne@68: method, jpayne@68: redirect_location, jpayne@68: body, jpayne@68: headers, jpayne@68: retries=retries, jpayne@68: redirect=redirect, jpayne@68: assert_same_host=assert_same_host, jpayne@68: timeout=timeout, jpayne@68: pool_timeout=pool_timeout, jpayne@68: release_conn=release_conn, jpayne@68: chunked=chunked, jpayne@68: body_pos=body_pos, jpayne@68: preload_content=preload_content, jpayne@68: decode_content=decode_content, jpayne@68: **response_kw, jpayne@68: ) jpayne@68: jpayne@68: # Check if we should retry the HTTP response. jpayne@68: has_retry_after = bool(response.headers.get("Retry-After")) jpayne@68: if retries.is_retry(method, response.status, has_retry_after): jpayne@68: try: jpayne@68: retries = retries.increment(method, url, response=response, _pool=self) jpayne@68: except MaxRetryError: jpayne@68: if retries.raise_on_status: jpayne@68: response.drain_conn() jpayne@68: raise jpayne@68: return response jpayne@68: jpayne@68: response.drain_conn() jpayne@68: retries.sleep(response) jpayne@68: log.debug("Retry: %s", url) jpayne@68: return self.urlopen( jpayne@68: method, jpayne@68: url, jpayne@68: body, jpayne@68: headers, jpayne@68: retries=retries, jpayne@68: redirect=redirect, jpayne@68: assert_same_host=assert_same_host, jpayne@68: timeout=timeout, jpayne@68: pool_timeout=pool_timeout, jpayne@68: release_conn=release_conn, jpayne@68: chunked=chunked, jpayne@68: body_pos=body_pos, jpayne@68: preload_content=preload_content, jpayne@68: decode_content=decode_content, jpayne@68: **response_kw, jpayne@68: ) jpayne@68: jpayne@68: return response jpayne@68: jpayne@68: jpayne@68: class HTTPSConnectionPool(HTTPConnectionPool): jpayne@68: """ jpayne@68: Same as :class:`.HTTPConnectionPool`, but HTTPS. jpayne@68: jpayne@68: :class:`.HTTPSConnection` uses one of ``assert_fingerprint``, jpayne@68: ``assert_hostname`` and ``host`` in this order to verify connections. jpayne@68: If ``assert_hostname`` is False, no verification is done. jpayne@68: jpayne@68: The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs``, jpayne@68: ``ca_cert_dir``, ``ssl_version``, ``key_password`` are only used if :mod:`ssl` jpayne@68: is available and are fed into :meth:`urllib3.util.ssl_wrap_socket` to upgrade jpayne@68: the connection socket into an SSL socket. jpayne@68: """ jpayne@68: jpayne@68: scheme = "https" jpayne@68: ConnectionCls: type[BaseHTTPSConnection] = HTTPSConnection jpayne@68: jpayne@68: def __init__( jpayne@68: self, jpayne@68: host: str, jpayne@68: port: int | None = None, jpayne@68: timeout: _TYPE_TIMEOUT | None = _DEFAULT_TIMEOUT, jpayne@68: maxsize: int = 1, jpayne@68: block: bool = False, jpayne@68: headers: typing.Mapping[str, str] | None = None, jpayne@68: retries: Retry | bool | int | None = None, jpayne@68: _proxy: Url | None = None, jpayne@68: _proxy_headers: typing.Mapping[str, str] | None = None, jpayne@68: key_file: str | None = None, jpayne@68: cert_file: str | None = None, jpayne@68: cert_reqs: int | str | None = None, jpayne@68: key_password: str | None = None, jpayne@68: ca_certs: str | None = None, jpayne@68: ssl_version: int | str | None = None, jpayne@68: ssl_minimum_version: ssl.TLSVersion | None = None, jpayne@68: ssl_maximum_version: ssl.TLSVersion | None = None, jpayne@68: assert_hostname: str | typing.Literal[False] | None = None, jpayne@68: assert_fingerprint: str | None = None, jpayne@68: ca_cert_dir: str | None = None, jpayne@68: **conn_kw: typing.Any, jpayne@68: ) -> None: jpayne@68: super().__init__( jpayne@68: host, jpayne@68: port, jpayne@68: timeout, jpayne@68: maxsize, jpayne@68: block, jpayne@68: headers, jpayne@68: retries, jpayne@68: _proxy, jpayne@68: _proxy_headers, jpayne@68: **conn_kw, jpayne@68: ) jpayne@68: jpayne@68: self.key_file = key_file jpayne@68: self.cert_file = cert_file jpayne@68: self.cert_reqs = cert_reqs jpayne@68: self.key_password = key_password jpayne@68: self.ca_certs = ca_certs jpayne@68: self.ca_cert_dir = ca_cert_dir jpayne@68: self.ssl_version = ssl_version jpayne@68: self.ssl_minimum_version = ssl_minimum_version jpayne@68: self.ssl_maximum_version = ssl_maximum_version jpayne@68: self.assert_hostname = assert_hostname jpayne@68: self.assert_fingerprint = assert_fingerprint jpayne@68: jpayne@68: def _prepare_proxy(self, conn: HTTPSConnection) -> None: # type: ignore[override] jpayne@68: """Establishes a tunnel connection through HTTP CONNECT.""" jpayne@68: if self.proxy and self.proxy.scheme == "https": jpayne@68: tunnel_scheme = "https" jpayne@68: else: jpayne@68: tunnel_scheme = "http" jpayne@68: jpayne@68: conn.set_tunnel( jpayne@68: scheme=tunnel_scheme, jpayne@68: host=self._tunnel_host, jpayne@68: port=self.port, jpayne@68: headers=self.proxy_headers, jpayne@68: ) jpayne@68: conn.connect() jpayne@68: jpayne@68: def _new_conn(self) -> BaseHTTPSConnection: jpayne@68: """ jpayne@68: Return a fresh :class:`urllib3.connection.HTTPConnection`. jpayne@68: """ jpayne@68: self.num_connections += 1 jpayne@68: log.debug( jpayne@68: "Starting new HTTPS connection (%d): %s:%s", jpayne@68: self.num_connections, jpayne@68: self.host, jpayne@68: self.port or "443", jpayne@68: ) jpayne@68: jpayne@68: if not self.ConnectionCls or self.ConnectionCls is DummyConnection: # type: ignore[comparison-overlap] jpayne@68: raise ImportError( jpayne@68: "Can't connect to HTTPS URL because the SSL module is not available." jpayne@68: ) jpayne@68: jpayne@68: actual_host: str = self.host jpayne@68: actual_port = self.port jpayne@68: if self.proxy is not None and self.proxy.host is not None: jpayne@68: actual_host = self.proxy.host jpayne@68: actual_port = self.proxy.port jpayne@68: jpayne@68: return self.ConnectionCls( jpayne@68: host=actual_host, jpayne@68: port=actual_port, jpayne@68: timeout=self.timeout.connect_timeout, jpayne@68: cert_file=self.cert_file, jpayne@68: key_file=self.key_file, jpayne@68: key_password=self.key_password, jpayne@68: cert_reqs=self.cert_reqs, jpayne@68: ca_certs=self.ca_certs, jpayne@68: ca_cert_dir=self.ca_cert_dir, jpayne@68: assert_hostname=self.assert_hostname, jpayne@68: assert_fingerprint=self.assert_fingerprint, jpayne@68: ssl_version=self.ssl_version, jpayne@68: ssl_minimum_version=self.ssl_minimum_version, jpayne@68: ssl_maximum_version=self.ssl_maximum_version, jpayne@68: **self.conn_kw, jpayne@68: ) jpayne@68: jpayne@68: def _validate_conn(self, conn: BaseHTTPConnection) -> None: jpayne@68: """ jpayne@68: Called right before a request is made, after the socket is created. jpayne@68: """ jpayne@68: super()._validate_conn(conn) jpayne@68: jpayne@68: # Force connect early to allow us to validate the connection. jpayne@68: if conn.is_closed: jpayne@68: conn.connect() jpayne@68: jpayne@68: # TODO revise this, see https://github.com/urllib3/urllib3/issues/2791 jpayne@68: if not conn.is_verified and not conn.proxy_is_verified: jpayne@68: warnings.warn( jpayne@68: ( jpayne@68: f"Unverified HTTPS request is being made to host '{conn.host}'. " jpayne@68: "Adding certificate verification is strongly advised. See: " jpayne@68: "https://urllib3.readthedocs.io/en/latest/advanced-usage.html" jpayne@68: "#tls-warnings" jpayne@68: ), jpayne@68: InsecureRequestWarning, jpayne@68: ) jpayne@68: jpayne@68: jpayne@68: def connection_from_url(url: str, **kw: typing.Any) -> HTTPConnectionPool: jpayne@68: """ jpayne@68: Given a url, return an :class:`.ConnectionPool` instance of its host. jpayne@68: jpayne@68: This is a shortcut for not having to parse out the scheme, host, and port jpayne@68: of the url before creating an :class:`.ConnectionPool` instance. jpayne@68: jpayne@68: :param url: jpayne@68: Absolute URL string that must include the scheme. Port is optional. jpayne@68: jpayne@68: :param \\**kw: jpayne@68: Passes additional parameters to the constructor of the appropriate jpayne@68: :class:`.ConnectionPool`. Useful for specifying things like jpayne@68: timeout, maxsize, headers, etc. jpayne@68: jpayne@68: Example:: jpayne@68: jpayne@68: >>> conn = connection_from_url('http://google.com/') jpayne@68: >>> r = conn.request('GET', '/') jpayne@68: """ jpayne@68: scheme, _, host, port, *_ = parse_url(url) jpayne@68: scheme = scheme or "http" jpayne@68: port = port or port_by_scheme.get(scheme, 80) jpayne@68: if scheme == "https": jpayne@68: return HTTPSConnectionPool(host, port=port, **kw) # type: ignore[arg-type] jpayne@68: else: jpayne@68: return HTTPConnectionPool(host, port=port, **kw) # type: ignore[arg-type] jpayne@68: jpayne@68: jpayne@68: @typing.overload jpayne@68: def _normalize_host(host: None, scheme: str | None) -> None: jpayne@68: ... jpayne@68: jpayne@68: jpayne@68: @typing.overload jpayne@68: def _normalize_host(host: str, scheme: str | None) -> str: jpayne@68: ... jpayne@68: jpayne@68: jpayne@68: def _normalize_host(host: str | None, scheme: str | None) -> str | None: jpayne@68: """ jpayne@68: Normalize hosts for comparisons and use with sockets. jpayne@68: """ jpayne@68: jpayne@68: host = normalize_host(host, scheme) jpayne@68: jpayne@68: # httplib doesn't like it when we include brackets in IPv6 addresses jpayne@68: # Specifically, if we include brackets but also pass the port then jpayne@68: # httplib crazily doubles up the square brackets on the Host header. jpayne@68: # Instead, we need to make sure we never pass ``None`` as the port. jpayne@68: # However, for backward compatibility reasons we can't actually jpayne@68: # *assert* that. See http://bugs.python.org/issue28539 jpayne@68: if host and host.startswith("[") and host.endswith("]"): jpayne@68: host = host[1:-1] jpayne@68: return host jpayne@68: jpayne@68: jpayne@68: def _url_from_pool( jpayne@68: pool: HTTPConnectionPool | HTTPSConnectionPool, path: str | None = None jpayne@68: ) -> str: jpayne@68: """Returns the URL from a given connection pool. This is mainly used for testing and logging.""" jpayne@68: return Url(scheme=pool.scheme, host=pool.host, port=pool.port, path=path).url jpayne@68: jpayne@68: jpayne@68: def _close_pool_connections(pool: queue.LifoQueue[typing.Any]) -> None: jpayne@68: """Drains a queue of connections and closes each one.""" jpayne@68: try: jpayne@68: while True: jpayne@68: conn = pool.get(block=False) jpayne@68: if conn: jpayne@68: conn.close() jpayne@68: except queue.Empty: jpayne@68: pass # Done.