poolmanager.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. from __future__ import annotations
  2. import functools
  3. import logging
  4. import typing
  5. import warnings
  6. from types import TracebackType
  7. from urllib.parse import urljoin
  8. from ._collections import HTTPHeaderDict, RecentlyUsedContainer
  9. from ._request_methods import RequestMethods
  10. from .connection import ProxyConfig
  11. from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, port_by_scheme
  12. from .exceptions import (
  13. LocationValueError,
  14. MaxRetryError,
  15. ProxySchemeUnknown,
  16. URLSchemeUnknown,
  17. )
  18. from .response import BaseHTTPResponse
  19. from .util.connection import _TYPE_SOCKET_OPTIONS
  20. from .util.proxy import connection_requires_http_tunnel
  21. from .util.retry import Retry
  22. from .util.timeout import Timeout
  23. from .util.url import Url, parse_url
  24. if typing.TYPE_CHECKING:
  25. import ssl
  26. from typing_extensions import Self
  27. __all__ = ["PoolManager", "ProxyManager", "proxy_from_url"]
  28. log = logging.getLogger(__name__)
  29. SSL_KEYWORDS = (
  30. "key_file",
  31. "cert_file",
  32. "cert_reqs",
  33. "ca_certs",
  34. "ca_cert_data",
  35. "ssl_version",
  36. "ssl_minimum_version",
  37. "ssl_maximum_version",
  38. "ca_cert_dir",
  39. "ssl_context",
  40. "key_password",
  41. "server_hostname",
  42. )
  43. # Default value for `blocksize` - a new parameter introduced to
  44. # http.client.HTTPConnection & http.client.HTTPSConnection in Python 3.7
  45. _DEFAULT_BLOCKSIZE = 16384
  46. class PoolKey(typing.NamedTuple):
  47. """
  48. All known keyword arguments that could be provided to the pool manager, its
  49. pools, or the underlying connections.
  50. All custom key schemes should include the fields in this key at a minimum.
  51. """
  52. key_scheme: str
  53. key_host: str
  54. key_port: int | None
  55. key_timeout: Timeout | float | int | None
  56. key_retries: Retry | bool | int | None
  57. key_block: bool | None
  58. key_source_address: tuple[str, int] | None
  59. key_key_file: str | None
  60. key_key_password: str | None
  61. key_cert_file: str | None
  62. key_cert_reqs: str | None
  63. key_ca_certs: str | None
  64. key_ca_cert_data: str | bytes | None
  65. key_ssl_version: int | str | None
  66. key_ssl_minimum_version: ssl.TLSVersion | None
  67. key_ssl_maximum_version: ssl.TLSVersion | None
  68. key_ca_cert_dir: str | None
  69. key_ssl_context: ssl.SSLContext | None
  70. key_maxsize: int | None
  71. key_headers: frozenset[tuple[str, str]] | None
  72. key__proxy: Url | None
  73. key__proxy_headers: frozenset[tuple[str, str]] | None
  74. key__proxy_config: ProxyConfig | None
  75. key_socket_options: _TYPE_SOCKET_OPTIONS | None
  76. key__socks_options: frozenset[tuple[str, str]] | None
  77. key_assert_hostname: bool | str | None
  78. key_assert_fingerprint: str | None
  79. key_server_hostname: str | None
  80. key_blocksize: int | None
  81. def _default_key_normalizer(
  82. key_class: type[PoolKey], request_context: dict[str, typing.Any]
  83. ) -> PoolKey:
  84. """
  85. Create a pool key out of a request context dictionary.
  86. According to RFC 3986, both the scheme and host are case-insensitive.
  87. Therefore, this function normalizes both before constructing the pool
  88. key for an HTTPS request. If you wish to change this behaviour, provide
  89. alternate callables to ``key_fn_by_scheme``.
  90. :param key_class:
  91. The class to use when constructing the key. This should be a namedtuple
  92. with the ``scheme`` and ``host`` keys at a minimum.
  93. :type key_class: namedtuple
  94. :param request_context:
  95. A dictionary-like object that contain the context for a request.
  96. :type request_context: dict
  97. :return: A namedtuple that can be used as a connection pool key.
  98. :rtype: PoolKey
  99. """
  100. # Since we mutate the dictionary, make a copy first
  101. context = request_context.copy()
  102. context["scheme"] = context["scheme"].lower()
  103. context["host"] = context["host"].lower()
  104. # These are both dictionaries and need to be transformed into frozensets
  105. for key in ("headers", "_proxy_headers", "_socks_options"):
  106. if key in context and context[key] is not None:
  107. context[key] = frozenset(context[key].items())
  108. # The socket_options key may be a list and needs to be transformed into a
  109. # tuple.
  110. socket_opts = context.get("socket_options")
  111. if socket_opts is not None:
  112. context["socket_options"] = tuple(socket_opts)
  113. # Map the kwargs to the names in the namedtuple - this is necessary since
  114. # namedtuples can't have fields starting with '_'.
  115. for key in list(context.keys()):
  116. context["key_" + key] = context.pop(key)
  117. # Default to ``None`` for keys missing from the context
  118. for field in key_class._fields:
  119. if field not in context:
  120. context[field] = None
  121. # Default key_blocksize to _DEFAULT_BLOCKSIZE if missing from the context
  122. if context.get("key_blocksize") is None:
  123. context["key_blocksize"] = _DEFAULT_BLOCKSIZE
  124. return key_class(**context)
  125. #: A dictionary that maps a scheme to a callable that creates a pool key.
  126. #: This can be used to alter the way pool keys are constructed, if desired.
  127. #: Each PoolManager makes a copy of this dictionary so they can be configured
  128. #: globally here, or individually on the instance.
  129. key_fn_by_scheme = {
  130. "http": functools.partial(_default_key_normalizer, PoolKey),
  131. "https": functools.partial(_default_key_normalizer, PoolKey),
  132. }
  133. pool_classes_by_scheme = {"http": HTTPConnectionPool, "https": HTTPSConnectionPool}
  134. class PoolManager(RequestMethods):
  135. """
  136. Allows for arbitrary requests while transparently keeping track of
  137. necessary connection pools for you.
  138. :param num_pools:
  139. Number of connection pools to cache before discarding the least
  140. recently used pool.
  141. :param headers:
  142. Headers to include with all requests, unless other headers are given
  143. explicitly.
  144. :param \\**connection_pool_kw:
  145. Additional parameters are used to create fresh
  146. :class:`urllib3.connectionpool.ConnectionPool` instances.
  147. Example:
  148. .. code-block:: python
  149. import urllib3
  150. http = urllib3.PoolManager(num_pools=2)
  151. resp1 = http.request("GET", "https://google.com/")
  152. resp2 = http.request("GET", "https://google.com/mail")
  153. resp3 = http.request("GET", "https://yahoo.com/")
  154. print(len(http.pools))
  155. # 2
  156. """
  157. proxy: Url | None = None
  158. proxy_config: ProxyConfig | None = None
  159. def __init__(
  160. self,
  161. num_pools: int = 10,
  162. headers: typing.Mapping[str, str] | None = None,
  163. **connection_pool_kw: typing.Any,
  164. ) -> None:
  165. super().__init__(headers)
  166. self.connection_pool_kw = connection_pool_kw
  167. self.pools: RecentlyUsedContainer[PoolKey, HTTPConnectionPool]
  168. self.pools = RecentlyUsedContainer(num_pools)
  169. # Locally set the pool classes and keys so other PoolManagers can
  170. # override them.
  171. self.pool_classes_by_scheme = pool_classes_by_scheme
  172. self.key_fn_by_scheme = key_fn_by_scheme.copy()
  173. def __enter__(self) -> Self:
  174. return self
  175. def __exit__(
  176. self,
  177. exc_type: type[BaseException] | None,
  178. exc_val: BaseException | None,
  179. exc_tb: TracebackType | None,
  180. ) -> typing.Literal[False]:
  181. self.clear()
  182. # Return False to re-raise any potential exceptions
  183. return False
  184. def _new_pool(
  185. self,
  186. scheme: str,
  187. host: str,
  188. port: int,
  189. request_context: dict[str, typing.Any] | None = None,
  190. ) -> HTTPConnectionPool:
  191. """
  192. Create a new :class:`urllib3.connectionpool.ConnectionPool` based on host, port, scheme, and
  193. any additional pool keyword arguments.
  194. If ``request_context`` is provided, it is provided as keyword arguments
  195. to the pool class used. This method is used to actually create the
  196. connection pools handed out by :meth:`connection_from_url` and
  197. companion methods. It is intended to be overridden for customization.
  198. """
  199. pool_cls: type[HTTPConnectionPool] = self.pool_classes_by_scheme[scheme]
  200. if request_context is None:
  201. request_context = self.connection_pool_kw.copy()
  202. # Default blocksize to _DEFAULT_BLOCKSIZE if missing or explicitly
  203. # set to 'None' in the request_context.
  204. if request_context.get("blocksize") is None:
  205. request_context["blocksize"] = _DEFAULT_BLOCKSIZE
  206. # Although the context has everything necessary to create the pool,
  207. # this function has historically only used the scheme, host, and port
  208. # in the positional args. When an API change is acceptable these can
  209. # be removed.
  210. for key in ("scheme", "host", "port"):
  211. request_context.pop(key, None)
  212. if scheme == "http":
  213. for kw in SSL_KEYWORDS:
  214. request_context.pop(kw, None)
  215. return pool_cls(host, port, **request_context)
  216. def clear(self) -> None:
  217. """
  218. Empty our store of pools and direct them all to close.
  219. This will not affect in-flight connections, but they will not be
  220. re-used after completion.
  221. """
  222. self.pools.clear()
  223. def connection_from_host(
  224. self,
  225. host: str | None,
  226. port: int | None = None,
  227. scheme: str | None = "http",
  228. pool_kwargs: dict[str, typing.Any] | None = None,
  229. ) -> HTTPConnectionPool:
  230. """
  231. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the host, port, and scheme.
  232. If ``port`` isn't given, it will be derived from the ``scheme`` using
  233. ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
  234. provided, it is merged with the instance's ``connection_pool_kw``
  235. variable and used to create the new connection pool, if one is
  236. needed.
  237. """
  238. if not host:
  239. raise LocationValueError("No host specified.")
  240. request_context = self._merge_pool_kwargs(pool_kwargs)
  241. request_context["scheme"] = scheme or "http"
  242. if not port:
  243. port = port_by_scheme.get(request_context["scheme"].lower(), 80)
  244. request_context["port"] = port
  245. request_context["host"] = host
  246. return self.connection_from_context(request_context)
  247. def connection_from_context(
  248. self, request_context: dict[str, typing.Any]
  249. ) -> HTTPConnectionPool:
  250. """
  251. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the request context.
  252. ``request_context`` must at least contain the ``scheme`` key and its
  253. value must be a key in ``key_fn_by_scheme`` instance variable.
  254. """
  255. if "strict" in request_context:
  256. warnings.warn(
  257. "The 'strict' parameter is no longer needed on Python 3+. "
  258. "This will raise an error in urllib3 v2.1.0.",
  259. DeprecationWarning,
  260. )
  261. request_context.pop("strict")
  262. scheme = request_context["scheme"].lower()
  263. pool_key_constructor = self.key_fn_by_scheme.get(scheme)
  264. if not pool_key_constructor:
  265. raise URLSchemeUnknown(scheme)
  266. pool_key = pool_key_constructor(request_context)
  267. return self.connection_from_pool_key(pool_key, request_context=request_context)
  268. def connection_from_pool_key(
  269. self, pool_key: PoolKey, request_context: dict[str, typing.Any]
  270. ) -> HTTPConnectionPool:
  271. """
  272. Get a :class:`urllib3.connectionpool.ConnectionPool` based on the provided pool key.
  273. ``pool_key`` should be a namedtuple that only contains immutable
  274. objects. At a minimum it must have the ``scheme``, ``host``, and
  275. ``port`` fields.
  276. """
  277. with self.pools.lock:
  278. # If the scheme, host, or port doesn't match existing open
  279. # connections, open a new ConnectionPool.
  280. pool = self.pools.get(pool_key)
  281. if pool:
  282. return pool
  283. # Make a fresh ConnectionPool of the desired type
  284. scheme = request_context["scheme"]
  285. host = request_context["host"]
  286. port = request_context["port"]
  287. pool = self._new_pool(scheme, host, port, request_context=request_context)
  288. self.pools[pool_key] = pool
  289. return pool
  290. def connection_from_url(
  291. self, url: str, pool_kwargs: dict[str, typing.Any] | None = None
  292. ) -> HTTPConnectionPool:
  293. """
  294. Similar to :func:`urllib3.connectionpool.connection_from_url`.
  295. If ``pool_kwargs`` is not provided and a new pool needs to be
  296. constructed, ``self.connection_pool_kw`` is used to initialize
  297. the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
  298. is provided, it is used instead. Note that if a new pool does not
  299. need to be created for the request, the provided ``pool_kwargs`` are
  300. not used.
  301. """
  302. u = parse_url(url)
  303. return self.connection_from_host(
  304. u.host, port=u.port, scheme=u.scheme, pool_kwargs=pool_kwargs
  305. )
  306. def _merge_pool_kwargs(
  307. self, override: dict[str, typing.Any] | None
  308. ) -> dict[str, typing.Any]:
  309. """
  310. Merge a dictionary of override values for self.connection_pool_kw.
  311. This does not modify self.connection_pool_kw and returns a new dict.
  312. Any keys in the override dictionary with a value of ``None`` are
  313. removed from the merged dictionary.
  314. """
  315. base_pool_kwargs = self.connection_pool_kw.copy()
  316. if override:
  317. for key, value in override.items():
  318. if value is None:
  319. try:
  320. del base_pool_kwargs[key]
  321. except KeyError:
  322. pass
  323. else:
  324. base_pool_kwargs[key] = value
  325. return base_pool_kwargs
  326. def _proxy_requires_url_absolute_form(self, parsed_url: Url) -> bool:
  327. """
  328. Indicates if the proxy requires the complete destination URL in the
  329. request. Normally this is only needed when not using an HTTP CONNECT
  330. tunnel.
  331. """
  332. if self.proxy is None:
  333. return False
  334. return not connection_requires_http_tunnel(
  335. self.proxy, self.proxy_config, parsed_url.scheme
  336. )
  337. def urlopen( # type: ignore[override]
  338. self, method: str, url: str, redirect: bool = True, **kw: typing.Any
  339. ) -> BaseHTTPResponse:
  340. """
  341. Same as :meth:`urllib3.HTTPConnectionPool.urlopen`
  342. with custom cross-host redirect logic and only sends the request-uri
  343. portion of the ``url``.
  344. The given ``url`` parameter must be absolute, such that an appropriate
  345. :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
  346. """
  347. u = parse_url(url)
  348. if u.scheme is None:
  349. warnings.warn(
  350. "URLs without a scheme (ie 'https://') are deprecated and will raise an error "
  351. "in a future version of urllib3. To avoid this DeprecationWarning ensure all URLs "
  352. "start with 'https://' or 'http://'. Read more in this issue: "
  353. "https://github.com/urllib3/urllib3/issues/2920",
  354. category=DeprecationWarning,
  355. stacklevel=2,
  356. )
  357. conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
  358. kw["assert_same_host"] = False
  359. kw["redirect"] = False
  360. if "headers" not in kw:
  361. kw["headers"] = self.headers
  362. if self._proxy_requires_url_absolute_form(u):
  363. response = conn.urlopen(method, url, **kw)
  364. else:
  365. response = conn.urlopen(method, u.request_uri, **kw)
  366. redirect_location = redirect and response.get_redirect_location()
  367. if not redirect_location:
  368. return response
  369. # Support relative URLs for redirecting.
  370. redirect_location = urljoin(url, redirect_location)
  371. if response.status == 303:
  372. # Change the method according to RFC 9110, Section 15.4.4.
  373. method = "GET"
  374. # And lose the body not to transfer anything sensitive.
  375. kw["body"] = None
  376. kw["headers"] = HTTPHeaderDict(kw["headers"])._prepare_for_method_change()
  377. retries = kw.get("retries")
  378. if not isinstance(retries, Retry):
  379. retries = Retry.from_int(retries, redirect=redirect)
  380. # Strip headers marked as unsafe to forward to the redirected location.
  381. # Check remove_headers_on_redirect to avoid a potential network call within
  382. # conn.is_same_host() which may use socket.gethostbyname() in the future.
  383. if retries.remove_headers_on_redirect and not conn.is_same_host(
  384. redirect_location
  385. ):
  386. new_headers = kw["headers"].copy()
  387. for header in kw["headers"]:
  388. if header.lower() in retries.remove_headers_on_redirect:
  389. new_headers.pop(header, None)
  390. kw["headers"] = new_headers
  391. try:
  392. retries = retries.increment(method, url, response=response, _pool=conn)
  393. except MaxRetryError:
  394. if retries.raise_on_redirect:
  395. response.drain_conn()
  396. raise
  397. return response
  398. kw["retries"] = retries
  399. kw["redirect"] = redirect
  400. log.info("Redirecting %s -> %s", url, redirect_location)
  401. response.drain_conn()
  402. return self.urlopen(method, redirect_location, **kw)
  403. class ProxyManager(PoolManager):
  404. """
  405. Behaves just like :class:`PoolManager`, but sends all requests through
  406. the defined proxy, using the CONNECT method for HTTPS URLs.
  407. :param proxy_url:
  408. The URL of the proxy to be used.
  409. :param proxy_headers:
  410. A dictionary containing headers that will be sent to the proxy. In case
  411. of HTTP they are being sent with each request, while in the
  412. HTTPS/CONNECT case they are sent only once. Could be used for proxy
  413. authentication.
  414. :param proxy_ssl_context:
  415. The proxy SSL context is used to establish the TLS connection to the
  416. proxy when using HTTPS proxies.
  417. :param use_forwarding_for_https:
  418. (Defaults to False) If set to True will forward requests to the HTTPS
  419. proxy to be made on behalf of the client instead of creating a TLS
  420. tunnel via the CONNECT method. **Enabling this flag means that request
  421. and response headers and content will be visible from the HTTPS proxy**
  422. whereas tunneling keeps request and response headers and content
  423. private. IP address, target hostname, SNI, and port are always visible
  424. to an HTTPS proxy even when this flag is disabled.
  425. :param proxy_assert_hostname:
  426. The hostname of the certificate to verify against.
  427. :param proxy_assert_fingerprint:
  428. The fingerprint of the certificate to verify against.
  429. Example:
  430. .. code-block:: python
  431. import urllib3
  432. proxy = urllib3.ProxyManager("https://localhost:3128/")
  433. resp1 = proxy.request("GET", "https://google.com/")
  434. resp2 = proxy.request("GET", "https://httpbin.org/")
  435. print(len(proxy.pools))
  436. # 1
  437. resp3 = proxy.request("GET", "https://httpbin.org/")
  438. resp4 = proxy.request("GET", "https://twitter.com/")
  439. print(len(proxy.pools))
  440. # 3
  441. """
  442. def __init__(
  443. self,
  444. proxy_url: str,
  445. num_pools: int = 10,
  446. headers: typing.Mapping[str, str] | None = None,
  447. proxy_headers: typing.Mapping[str, str] | None = None,
  448. proxy_ssl_context: ssl.SSLContext | None = None,
  449. use_forwarding_for_https: bool = False,
  450. proxy_assert_hostname: None | str | typing.Literal[False] = None,
  451. proxy_assert_fingerprint: str | None = None,
  452. **connection_pool_kw: typing.Any,
  453. ) -> None:
  454. if isinstance(proxy_url, HTTPConnectionPool):
  455. str_proxy_url = f"{proxy_url.scheme}://{proxy_url.host}:{proxy_url.port}"
  456. else:
  457. str_proxy_url = proxy_url
  458. proxy = parse_url(str_proxy_url)
  459. if proxy.scheme not in ("http", "https"):
  460. raise ProxySchemeUnknown(proxy.scheme)
  461. if not proxy.port:
  462. port = port_by_scheme.get(proxy.scheme, 80)
  463. proxy = proxy._replace(port=port)
  464. self.proxy = proxy
  465. self.proxy_headers = proxy_headers or {}
  466. self.proxy_ssl_context = proxy_ssl_context
  467. self.proxy_config = ProxyConfig(
  468. proxy_ssl_context,
  469. use_forwarding_for_https,
  470. proxy_assert_hostname,
  471. proxy_assert_fingerprint,
  472. )
  473. connection_pool_kw["_proxy"] = self.proxy
  474. connection_pool_kw["_proxy_headers"] = self.proxy_headers
  475. connection_pool_kw["_proxy_config"] = self.proxy_config
  476. super().__init__(num_pools, headers, **connection_pool_kw)
  477. def connection_from_host(
  478. self,
  479. host: str | None,
  480. port: int | None = None,
  481. scheme: str | None = "http",
  482. pool_kwargs: dict[str, typing.Any] | None = None,
  483. ) -> HTTPConnectionPool:
  484. if scheme == "https":
  485. return super().connection_from_host(
  486. host, port, scheme, pool_kwargs=pool_kwargs
  487. )
  488. return super().connection_from_host(
  489. self.proxy.host, self.proxy.port, self.proxy.scheme, pool_kwargs=pool_kwargs # type: ignore[union-attr]
  490. )
  491. def _set_proxy_headers(
  492. self, url: str, headers: typing.Mapping[str, str] | None = None
  493. ) -> typing.Mapping[str, str]:
  494. """
  495. Sets headers needed by proxies: specifically, the Accept and Host
  496. headers. Only sets headers not provided by the user.
  497. """
  498. headers_ = {"Accept": "*/*"}
  499. netloc = parse_url(url).netloc
  500. if netloc:
  501. headers_["Host"] = netloc
  502. if headers:
  503. headers_.update(headers)
  504. return headers_
  505. def urlopen( # type: ignore[override]
  506. self, method: str, url: str, redirect: bool = True, **kw: typing.Any
  507. ) -> BaseHTTPResponse:
  508. "Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
  509. u = parse_url(url)
  510. if not connection_requires_http_tunnel(self.proxy, self.proxy_config, u.scheme):
  511. # For connections using HTTP CONNECT, httplib sets the necessary
  512. # headers on the CONNECT to the proxy. If we're not using CONNECT,
  513. # we'll definitely need to set 'Host' at the very least.
  514. headers = kw.get("headers", self.headers)
  515. kw["headers"] = self._set_proxy_headers(url, headers)
  516. return super().urlopen(method, url, redirect=redirect, **kw)
  517. def proxy_from_url(url: str, **kw: typing.Any) -> ProxyManager:
  518. return ProxyManager(proxy_url=url, **kw)