poolmanager.py 20 KB

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