adapters.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. """
  2. requests.adapters
  3. ~~~~~~~~~~~~~~~~~
  4. This module contains the transport adapters that Requests uses to define
  5. and maintain connections.
  6. """
  7. from __future__ import annotations
  8. import os.path
  9. import socket # noqa: F401 # type: ignore[reportUnusedImport]
  10. import typing
  11. import warnings
  12. from typing import Any
  13. from urllib3.exceptions import (
  14. ClosedPoolError,
  15. ConnectTimeoutError,
  16. LocationValueError,
  17. MaxRetryError,
  18. NewConnectionError,
  19. ProtocolError,
  20. ReadTimeoutError,
  21. ResponseError,
  22. )
  23. from urllib3.exceptions import HTTPError as _HTTPError
  24. from urllib3.exceptions import InvalidHeader as _InvalidHeader
  25. from urllib3.exceptions import ProxyError as _ProxyError
  26. from urllib3.exceptions import SSLError as _SSLError
  27. from urllib3.poolmanager import PoolManager, proxy_from_url
  28. from urllib3.util import Timeout as TimeoutSauce
  29. from urllib3.util import parse_url
  30. from urllib3.util.retry import Retry
  31. from .auth import _basic_auth_str # type: ignore[reportPrivateUsage]
  32. from .compat import basestring, urlparse
  33. from .cookies import extract_cookies_to_jar
  34. from .exceptions import (
  35. ConnectionError,
  36. ConnectTimeout,
  37. InvalidHeader,
  38. InvalidProxyURL,
  39. InvalidSchema,
  40. InvalidURL,
  41. ProxyError,
  42. ReadTimeout,
  43. RetryError,
  44. SSLError,
  45. )
  46. from .models import Response
  47. from .structures import CaseInsensitiveDict
  48. from .utils import (
  49. DEFAULT_CA_BUNDLE_PATH,
  50. get_auth_from_url,
  51. get_encoding_from_headers,
  52. prepend_scheme_if_needed,
  53. select_proxy,
  54. urldefragauth,
  55. )
  56. try:
  57. from urllib3.contrib.socks import SOCKSProxyManager # type: ignore[assignment]
  58. except ImportError:
  59. def SOCKSProxyManager(*args: Any, **kwargs: Any) -> None:
  60. raise InvalidSchema("Missing dependencies for SOCKS support.")
  61. if typing.TYPE_CHECKING:
  62. from urllib3.connectionpool import HTTPConnectionPool
  63. from urllib3.poolmanager import PoolManager as _PoolManager
  64. from . import _types as _t
  65. from .models import PreparedRequest
  66. from ._types import is_prepared as _is_prepared
  67. DEFAULT_POOLBLOCK = False
  68. DEFAULT_POOLSIZE = 10
  69. DEFAULT_RETRIES = 0
  70. DEFAULT_POOL_TIMEOUT = None
  71. def _urllib3_request_context(
  72. request: PreparedRequest,
  73. verify: bool | str | None,
  74. client_cert: tuple[str, str] | str | None,
  75. poolmanager: PoolManager,
  76. ) -> tuple[dict[str, Any], dict[str, Any]]:
  77. host_params: dict[str, Any] = {}
  78. pool_kwargs: dict[str, Any] = {}
  79. parsed_request_url = urlparse(request.url)
  80. scheme = parsed_request_url.scheme.lower()
  81. port = parsed_request_url.port
  82. cert_reqs = "CERT_REQUIRED"
  83. if verify is False:
  84. cert_reqs = "CERT_NONE"
  85. elif isinstance(verify, str):
  86. if not os.path.isdir(verify):
  87. pool_kwargs["ca_certs"] = verify
  88. else:
  89. pool_kwargs["ca_cert_dir"] = verify
  90. pool_kwargs["cert_reqs"] = cert_reqs
  91. if client_cert is not None:
  92. if isinstance(client_cert, tuple) and len(client_cert) == 2:
  93. pool_kwargs["cert_file"] = client_cert[0]
  94. pool_kwargs["key_file"] = client_cert[1]
  95. else:
  96. # According to our docs, we allow users to specify just the client
  97. # cert path
  98. pool_kwargs["cert_file"] = client_cert
  99. host_params = {
  100. "scheme": scheme,
  101. "host": parsed_request_url.hostname,
  102. "port": port,
  103. }
  104. return host_params, pool_kwargs
  105. class BaseAdapter:
  106. """The Base Transport Adapter"""
  107. def __init__(self) -> None:
  108. super().__init__()
  109. def send(
  110. self,
  111. request: PreparedRequest,
  112. stream: bool = False,
  113. timeout: _t.TimeoutType = None,
  114. verify: _t.VerifyType = True,
  115. cert: _t.CertType = None,
  116. proxies: dict[str, str] | None = None,
  117. ) -> Response:
  118. """Sends PreparedRequest object. Returns Response object.
  119. :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
  120. :param stream: (optional) Whether to stream the request content.
  121. :param timeout: (optional) How long to wait for the server to send
  122. data before giving up, as a float, or a :ref:`(connect timeout,
  123. read timeout) <timeouts>` tuple.
  124. :type timeout: float or tuple
  125. :param verify: (optional) Either a boolean, in which case it controls whether we verify
  126. the server's TLS certificate, or a string, in which case it must be a path
  127. to a CA bundle to use
  128. :param cert: (optional) Any user-provided SSL certificate to be trusted.
  129. :param proxies: (optional) The proxies dictionary to apply to the request.
  130. """
  131. raise NotImplementedError
  132. def close(self) -> None:
  133. """Cleans up adapter specific items."""
  134. raise NotImplementedError
  135. class HTTPAdapter(BaseAdapter):
  136. """The built-in HTTP Adapter for urllib3.
  137. Provides a general-case interface for Requests sessions to contact HTTP and
  138. HTTPS urls by implementing the Transport Adapter interface. This class will
  139. usually be created by the :class:`Session <Session>` class under the
  140. covers.
  141. :param pool_connections: The number of urllib3 connection pools to cache.
  142. :param pool_maxsize: The maximum number of connections to save in the pool.
  143. :param max_retries: The maximum number of retries each connection
  144. should attempt. Note, this applies only to failed DNS lookups, socket
  145. connections and connection timeouts, never to requests where data has
  146. made it to the server. By default, Requests does not retry failed
  147. connections. If you need granular control over the conditions under
  148. which we retry a request, import urllib3's ``Retry`` class and pass
  149. that instead.
  150. :param pool_block: Whether the connection pool should block for connections.
  151. Usage::
  152. >>> import requests
  153. >>> s = requests.Session()
  154. >>> a = requests.adapters.HTTPAdapter(max_retries=3)
  155. >>> s.mount('http://', a)
  156. """
  157. __attrs__: list[str] = [
  158. "max_retries",
  159. "config",
  160. "_pool_connections",
  161. "_pool_maxsize",
  162. "_pool_block",
  163. ]
  164. max_retries: Retry
  165. config: dict[str, Any]
  166. proxy_manager: dict[str, Any]
  167. _pool_connections: int
  168. _pool_maxsize: int
  169. _pool_block: bool
  170. poolmanager: _PoolManager
  171. def __init__(
  172. self,
  173. pool_connections: int = DEFAULT_POOLSIZE,
  174. pool_maxsize: int = DEFAULT_POOLSIZE,
  175. max_retries: int | Retry = DEFAULT_RETRIES,
  176. pool_block: bool = DEFAULT_POOLBLOCK,
  177. ) -> None:
  178. if max_retries == DEFAULT_RETRIES:
  179. self.max_retries = Retry(0, read=False)
  180. else:
  181. self.max_retries = Retry.from_int(max_retries)
  182. self.config = {}
  183. self.proxy_manager = {}
  184. super().__init__()
  185. self._pool_connections = pool_connections
  186. self._pool_maxsize = pool_maxsize
  187. self._pool_block = pool_block
  188. self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block)
  189. def __getstate__(self) -> dict[str, Any]:
  190. return {attr: getattr(self, attr, None) for attr in self.__attrs__}
  191. def __setstate__(self, state: dict[str, Any]) -> None:
  192. # Can't handle by adding 'proxy_manager' to self.__attrs__ because
  193. # self.poolmanager uses a lambda function, which isn't pickleable.
  194. self.proxy_manager = {}
  195. self.config = {}
  196. for attr, value in state.items():
  197. setattr(self, attr, value)
  198. self.init_poolmanager(
  199. self._pool_connections, self._pool_maxsize, block=self._pool_block
  200. )
  201. def init_poolmanager(
  202. self,
  203. connections: int,
  204. maxsize: int,
  205. block: bool = DEFAULT_POOLBLOCK,
  206. **pool_kwargs: Any,
  207. ) -> None:
  208. """Initializes a urllib3 PoolManager.
  209. This method should not be called from user code, and is only
  210. exposed for use when subclassing the
  211. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  212. :param connections: The number of urllib3 connection pools to cache.
  213. :param maxsize: The maximum number of connections to save in the pool.
  214. :param block: Block when no free connections are available.
  215. :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager.
  216. """
  217. # save these values for pickling
  218. self._pool_connections = connections
  219. self._pool_maxsize = maxsize
  220. self._pool_block = block
  221. self.poolmanager = PoolManager(
  222. num_pools=connections,
  223. maxsize=maxsize,
  224. block=block,
  225. **pool_kwargs,
  226. )
  227. def proxy_manager_for(self, proxy: str, **proxy_kwargs: Any) -> Any:
  228. """Return urllib3 ProxyManager for the given proxy.
  229. This method should not be called from user code, and is only
  230. exposed for use when subclassing the
  231. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  232. :param proxy: The proxy to return a urllib3 ProxyManager for.
  233. :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager.
  234. :returns: ProxyManager
  235. :rtype: urllib3.ProxyManager
  236. """
  237. if proxy in self.proxy_manager:
  238. manager = self.proxy_manager[proxy]
  239. elif proxy.lower().startswith("socks"):
  240. username, password = get_auth_from_url(proxy)
  241. manager = self.proxy_manager[proxy] = SOCKSProxyManager(
  242. proxy,
  243. username=username,
  244. password=password,
  245. num_pools=self._pool_connections,
  246. maxsize=self._pool_maxsize,
  247. block=self._pool_block,
  248. **proxy_kwargs,
  249. )
  250. else:
  251. proxy_headers = self.proxy_headers(proxy)
  252. manager = self.proxy_manager[proxy] = proxy_from_url(
  253. proxy,
  254. proxy_headers=proxy_headers,
  255. num_pools=self._pool_connections,
  256. maxsize=self._pool_maxsize,
  257. block=self._pool_block,
  258. **proxy_kwargs,
  259. )
  260. return manager
  261. def cert_verify(
  262. self, conn: Any, url: str, verify: _t.VerifyType, cert: _t.CertType
  263. ) -> None:
  264. """Verify a SSL certificate. This method should not be called from user
  265. code, and is only exposed for use when subclassing the
  266. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  267. :param conn: The urllib3 connection object associated with the cert.
  268. :param url: The requested URL.
  269. :param verify: Either a boolean, in which case it controls whether we verify
  270. the server's TLS certificate, or a string, in which case it must be a path
  271. to a CA bundle to use
  272. :param cert: The SSL certificate to verify.
  273. """
  274. if url.lower().startswith("https") and verify:
  275. cert_loc = None
  276. # Allow self-specified cert location.
  277. if verify is not True:
  278. cert_loc = verify
  279. if not cert_loc:
  280. cert_loc = DEFAULT_CA_BUNDLE_PATH
  281. if not cert_loc or not os.path.exists(cert_loc):
  282. raise OSError(
  283. f"Could not find a suitable TLS CA certificate bundle, "
  284. f"invalid path: {cert_loc}"
  285. )
  286. conn.cert_reqs = "CERT_REQUIRED"
  287. if not os.path.isdir(cert_loc):
  288. conn.ca_certs = cert_loc
  289. else:
  290. conn.ca_cert_dir = cert_loc
  291. else:
  292. conn.cert_reqs = "CERT_NONE"
  293. conn.ca_certs = None
  294. conn.ca_cert_dir = None
  295. if cert:
  296. if not isinstance(cert, basestring):
  297. conn.cert_file = cert[0]
  298. conn.key_file = cert[1]
  299. else:
  300. conn.cert_file = cert
  301. conn.key_file = None
  302. if conn.cert_file and not os.path.exists(conn.cert_file):
  303. raise OSError(
  304. f"Could not find the TLS certificate file, "
  305. f"invalid path: {conn.cert_file}"
  306. )
  307. if conn.key_file and not os.path.exists(conn.key_file):
  308. raise OSError(
  309. f"Could not find the TLS key file, invalid path: {conn.key_file}"
  310. )
  311. def build_response(self, req: PreparedRequest, resp: Any) -> Response:
  312. """Builds a :class:`Response <requests.Response>` object from a urllib3
  313. response. This should not be called from user code, and is only exposed
  314. for use when subclassing the
  315. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
  316. :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
  317. :param resp: The urllib3 response object.
  318. :rtype: requests.Response
  319. """
  320. assert _is_prepared(req)
  321. response = Response()
  322. # Fallback to None if there's no status_code, for whatever reason.
  323. response.status_code = getattr(resp, "status", None) # type: ignore[assignment]
  324. # Make headers case-insensitive.
  325. response.headers = CaseInsensitiveDict(getattr(resp, "headers", {}))
  326. # Set encoding.
  327. response.encoding = get_encoding_from_headers(response.headers)
  328. response.raw = resp
  329. response.reason = response.raw.reason
  330. if isinstance(req.url, bytes):
  331. response.url = req.url.decode("utf-8")
  332. else:
  333. response.url = req.url
  334. # Add new cookies from the server.
  335. extract_cookies_to_jar(response.cookies, req, resp)
  336. # Give the Response some context.
  337. response.request = req
  338. response.connection = self
  339. return response
  340. def build_connection_pool_key_attributes(
  341. self, request: PreparedRequest, verify: _t.VerifyType, cert: _t.CertType = None
  342. ) -> tuple[dict[str, Any], dict[str, Any]]:
  343. """Build the PoolKey attributes used by urllib3 to return a connection.
  344. This looks at the PreparedRequest, the user-specified verify value,
  345. and the value of the cert parameter to determine what PoolKey values
  346. to use to select a connection from a given urllib3 Connection Pool.
  347. The SSL related pool key arguments are not consistently set. As of
  348. this writing, use the following to determine what keys may be in that
  349. dictionary:
  350. * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the
  351. default Requests SSL Context
  352. * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but
  353. ``"cert_reqs"`` will be set
  354. * If ``verify`` is a string, (i.e., it is a user-specified trust bundle)
  355. ``"ca_certs"`` will be set if the string is not a directory recognized
  356. by :py:func:`os.path.isdir`, otherwise ``"ca_cert_dir"`` will be
  357. set.
  358. * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If
  359. ``"cert"`` is a tuple with a second item, ``"key_file"`` will also
  360. be present
  361. To override these settings, one may subclass this class, call this
  362. method and use the above logic to change parameters as desired. For
  363. example, if one wishes to use a custom :py:class:`ssl.SSLContext` one
  364. must both set ``"ssl_context"`` and based on what else they require,
  365. alter the other keys to ensure the desired behaviour.
  366. :param request:
  367. The PreparedRequest being sent over the connection.
  368. :type request:
  369. :class:`~requests.models.PreparedRequest`
  370. :param verify:
  371. Either a boolean, in which case it controls whether
  372. we verify the server's TLS certificate, or a string, in which case it
  373. must be a path to a CA bundle to use.
  374. :param cert:
  375. (optional) Any user-provided SSL certificate for client
  376. authentication (a.k.a., mTLS). This may be a string (i.e., just
  377. the path to a file which holds both certificate and key) or a
  378. tuple of length 2 with the certificate file path and key file
  379. path.
  380. :returns:
  381. A tuple of two dictionaries. The first is the "host parameters"
  382. portion of the Pool Key including scheme, hostname, and port. The
  383. second is a dictionary of SSLContext related parameters.
  384. """
  385. return _urllib3_request_context(request, verify, cert, self.poolmanager)
  386. def get_connection_with_tls_context(
  387. self,
  388. request: PreparedRequest,
  389. verify: _t.VerifyType,
  390. proxies: dict[str, str] | None = None,
  391. cert: _t.CertType = None,
  392. ) -> HTTPConnectionPool:
  393. """Returns a urllib3 connection for the given request and TLS settings.
  394. This should not be called from user code, and is only exposed for use
  395. when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  396. :param request:
  397. The :class:`PreparedRequest <PreparedRequest>` object to be sent
  398. over the connection.
  399. :param verify:
  400. Either a boolean, in which case it controls whether we verify the
  401. server's TLS certificate, or a string, in which case it must be a
  402. path to a CA bundle to use.
  403. :param proxies:
  404. (optional) The proxies dictionary to apply to the request.
  405. :param cert:
  406. (optional) Any user-provided SSL certificate to be used for client
  407. authentication (a.k.a., mTLS).
  408. :rtype:
  409. urllib3.HTTPConnectionPool
  410. """
  411. assert _is_prepared(request)
  412. proxy = select_proxy(request.url, proxies)
  413. try:
  414. host_params, pool_kwargs = self.build_connection_pool_key_attributes(
  415. request,
  416. verify,
  417. cert,
  418. )
  419. except ValueError as e:
  420. raise InvalidURL(e, request=request)
  421. if proxy:
  422. proxy = prepend_scheme_if_needed(proxy, "http")
  423. proxy_url = parse_url(proxy)
  424. if not proxy_url.host:
  425. raise InvalidProxyURL(
  426. "Please check proxy URL. It is malformed "
  427. "and could be missing the host."
  428. )
  429. proxy_manager = self.proxy_manager_for(proxy)
  430. conn = proxy_manager.connection_from_host(
  431. **host_params, pool_kwargs=pool_kwargs
  432. )
  433. else:
  434. # Only scheme should be lower case
  435. conn = self.poolmanager.connection_from_host(
  436. **host_params, pool_kwargs=pool_kwargs
  437. )
  438. return conn
  439. def get_connection(
  440. self, url: str, proxies: dict[str, str] | None = None
  441. ) -> HTTPConnectionPool:
  442. """DEPRECATED: Users should move to `get_connection_with_tls_context`
  443. for all subclasses of HTTPAdapter using Requests>=2.32.2.
  444. Returns a urllib3 connection for the given URL. This should not be
  445. called from user code, and is only exposed for use when subclassing the
  446. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  447. :param url: The URL to connect to.
  448. :param proxies: (optional) A Requests-style dictionary of proxies used on this request.
  449. :rtype: urllib3.HTTPConnectionPool
  450. """
  451. warnings.warn(
  452. (
  453. "`get_connection` has been deprecated in favor of "
  454. "`get_connection_with_tls_context`. Custom HTTPAdapter subclasses "
  455. "will need to migrate for Requests>=2.32.2. Please see "
  456. "https://github.com/psf/requests/pull/6710 for more details."
  457. ),
  458. DeprecationWarning,
  459. )
  460. proxy = select_proxy(url, proxies)
  461. if proxy:
  462. proxy = prepend_scheme_if_needed(proxy, "http")
  463. proxy_url = parse_url(proxy)
  464. if not proxy_url.host:
  465. raise InvalidProxyURL(
  466. "Please check proxy URL. It is malformed "
  467. "and could be missing the host."
  468. )
  469. proxy_manager = self.proxy_manager_for(proxy)
  470. conn = proxy_manager.connection_from_url(url)
  471. else:
  472. # Only scheme should be lower case
  473. parsed = urlparse(url)
  474. url = parsed.geturl()
  475. conn = self.poolmanager.connection_from_url(url)
  476. return conn
  477. def close(self) -> None:
  478. """Disposes of any internal state.
  479. Currently, this closes the PoolManager and any active ProxyManager,
  480. which closes any pooled connections.
  481. """
  482. self.poolmanager.clear()
  483. for proxy in self.proxy_manager.values():
  484. proxy.clear()
  485. def request_url(
  486. self, request: PreparedRequest, proxies: dict[str, str] | None
  487. ) -> str:
  488. """Obtain the url to use when making the final request.
  489. If the message is being sent through a HTTP proxy, the full URL has to
  490. be used. Otherwise, we should only use the path portion of the URL.
  491. This should not be called from user code, and is only exposed for use
  492. when subclassing the
  493. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  494. :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
  495. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs.
  496. :rtype: str
  497. """
  498. assert _is_prepared(request)
  499. proxy = select_proxy(request.url, proxies)
  500. scheme = urlparse(request.url).scheme
  501. is_proxied_http_request = proxy and scheme != "https"
  502. using_socks_proxy = False
  503. if proxy:
  504. proxy_scheme = urlparse(proxy).scheme.lower()
  505. using_socks_proxy = proxy_scheme.startswith("socks")
  506. url = request.path_url
  507. if is_proxied_http_request and not using_socks_proxy:
  508. url = urldefragauth(request.url)
  509. return url
  510. def add_headers(self, request: PreparedRequest, **kwargs: Any) -> None:
  511. """Add any headers needed by the connection. As of v2.0 this does
  512. nothing by default, but is left for overriding by users that subclass
  513. the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  514. This should not be called from user code, and is only exposed for use
  515. when subclassing the
  516. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  517. :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
  518. :param kwargs: The keyword arguments from the call to send().
  519. """
  520. pass
  521. def proxy_headers(self, proxy: str) -> dict[str, str]:
  522. """Returns a dictionary of the headers to add to any request sent
  523. through a proxy. This works with urllib3 magic to ensure that they are
  524. correctly sent to the proxy, rather than in a tunnelled request if
  525. CONNECT is being used.
  526. This should not be called from user code, and is only exposed for use
  527. when subclassing the
  528. :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
  529. :param proxy: The url of the proxy being used for this request.
  530. :rtype: dict
  531. """
  532. headers: dict[str, str] = {}
  533. username, password = get_auth_from_url(proxy)
  534. if username:
  535. headers["Proxy-Authorization"] = _basic_auth_str(username, password)
  536. return headers
  537. def send(
  538. self,
  539. request: PreparedRequest,
  540. stream: bool = False,
  541. timeout: _t.TimeoutType = None,
  542. verify: _t.VerifyType = True,
  543. cert: _t.CertType = None,
  544. proxies: dict[str, str] | None = None,
  545. ) -> Response:
  546. """Sends PreparedRequest object. Returns Response object.
  547. :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
  548. :param stream: (optional) Whether to stream the request content.
  549. :param timeout: (optional) How long to wait for the server to send
  550. data before giving up, as a float, or a :ref:`(connect timeout,
  551. read timeout) <timeouts>` tuple.
  552. :type timeout: float or tuple or urllib3 Timeout object
  553. :param verify: (optional) Either a boolean, in which case it controls whether
  554. we verify the server's TLS certificate, or a string, in which case it
  555. must be a path to a CA bundle to use
  556. :param cert: (optional) Any user-provided SSL certificate to be trusted.
  557. :param proxies: (optional) The proxies dictionary to apply to the request.
  558. :rtype: requests.Response
  559. """
  560. assert _is_prepared(request)
  561. try:
  562. conn = self.get_connection_with_tls_context(
  563. request, verify, proxies=proxies, cert=cert
  564. )
  565. except LocationValueError as e:
  566. raise InvalidURL(e, request=request)
  567. self.cert_verify(conn, request.url, verify, cert)
  568. url = self.request_url(request, proxies)
  569. self.add_headers(
  570. request,
  571. stream=stream,
  572. timeout=timeout,
  573. verify=verify,
  574. cert=cert,
  575. proxies=proxies,
  576. )
  577. chunked = not (request.body is None or "Content-Length" in request.headers)
  578. if isinstance(timeout, tuple):
  579. try:
  580. connect, read = timeout
  581. resolved_timeout = TimeoutSauce(connect=connect, read=read)
  582. except ValueError:
  583. raise ValueError(
  584. f"Invalid timeout {timeout}. Pass a (connect, read) timeout tuple, "
  585. f"or a single float to set both timeouts to the same value."
  586. )
  587. elif isinstance(timeout, TimeoutSauce):
  588. resolved_timeout = timeout
  589. else:
  590. resolved_timeout = TimeoutSauce(connect=timeout, read=timeout)
  591. try:
  592. resp = conn.urlopen(
  593. method=request.method,
  594. url=url,
  595. body=request.body, # type: ignore[arg-type] # urllib3 stubs don't accept Iterable[bytes | str]
  596. headers=request.headers, # type: ignore[arg-type] # urllib3#3072
  597. redirect=False,
  598. assert_same_host=False,
  599. preload_content=False,
  600. decode_content=False,
  601. retries=self.max_retries,
  602. timeout=resolved_timeout,
  603. chunked=chunked,
  604. )
  605. except (ProtocolError, OSError) as err:
  606. raise ConnectionError(err, request=request)
  607. except MaxRetryError as e:
  608. if isinstance(e.reason, ConnectTimeoutError):
  609. # TODO: Remove this in 3.0.0: see #2811
  610. if not isinstance(e.reason, NewConnectionError):
  611. raise ConnectTimeout(e, request=request)
  612. if isinstance(e.reason, ResponseError):
  613. raise RetryError(e, request=request)
  614. if isinstance(e.reason, _ProxyError):
  615. raise ProxyError(e, request=request)
  616. if isinstance(e.reason, _SSLError):
  617. # This branch is for urllib3 v1.22 and later.
  618. raise SSLError(e, request=request)
  619. raise ConnectionError(e, request=request)
  620. except ClosedPoolError as e:
  621. raise ConnectionError(e, request=request)
  622. except _ProxyError as e:
  623. raise ProxyError(e)
  624. except (_SSLError, _HTTPError) as e:
  625. if isinstance(e, _SSLError):
  626. # This branch is for urllib3 versions earlier than v1.22
  627. raise SSLError(e, request=request)
  628. elif isinstance(e, ReadTimeoutError):
  629. raise ReadTimeout(e, request=request)
  630. elif isinstance(e, _InvalidHeader):
  631. raise InvalidHeader(e, request=request)
  632. else:
  633. raise
  634. return self.build_response(request, resp)