jpayne@69: """ jpayne@69: requests.adapters jpayne@69: ~~~~~~~~~~~~~~~~~ jpayne@69: jpayne@69: This module contains the transport adapters that Requests uses to define jpayne@69: and maintain connections. jpayne@69: """ jpayne@69: jpayne@69: import os.path jpayne@69: import socket # noqa: F401 jpayne@69: import typing jpayne@69: import warnings jpayne@69: jpayne@69: from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError jpayne@69: from urllib3.exceptions import HTTPError as _HTTPError jpayne@69: from urllib3.exceptions import InvalidHeader as _InvalidHeader jpayne@69: from urllib3.exceptions import ( jpayne@69: LocationValueError, jpayne@69: MaxRetryError, jpayne@69: NewConnectionError, jpayne@69: ProtocolError, jpayne@69: ) jpayne@69: from urllib3.exceptions import ProxyError as _ProxyError jpayne@69: from urllib3.exceptions import ReadTimeoutError, ResponseError jpayne@69: from urllib3.exceptions import SSLError as _SSLError jpayne@69: from urllib3.poolmanager import PoolManager, proxy_from_url jpayne@69: from urllib3.util import Timeout as TimeoutSauce jpayne@69: from urllib3.util import parse_url jpayne@69: from urllib3.util.retry import Retry jpayne@69: from urllib3.util.ssl_ import create_urllib3_context jpayne@69: jpayne@69: from .auth import _basic_auth_str jpayne@69: from .compat import basestring, urlparse jpayne@69: from .cookies import extract_cookies_to_jar jpayne@69: from .exceptions import ( jpayne@69: ConnectionError, jpayne@69: ConnectTimeout, jpayne@69: InvalidHeader, jpayne@69: InvalidProxyURL, jpayne@69: InvalidSchema, jpayne@69: InvalidURL, jpayne@69: ProxyError, jpayne@69: ReadTimeout, jpayne@69: RetryError, jpayne@69: SSLError, jpayne@69: ) jpayne@69: from .models import Response jpayne@69: from .structures import CaseInsensitiveDict jpayne@69: from .utils import ( jpayne@69: DEFAULT_CA_BUNDLE_PATH, jpayne@69: extract_zipped_paths, jpayne@69: get_auth_from_url, jpayne@69: get_encoding_from_headers, jpayne@69: prepend_scheme_if_needed, jpayne@69: select_proxy, jpayne@69: urldefragauth, jpayne@69: ) jpayne@69: jpayne@69: try: jpayne@69: from urllib3.contrib.socks import SOCKSProxyManager jpayne@69: except ImportError: jpayne@69: jpayne@69: def SOCKSProxyManager(*args, **kwargs): jpayne@69: raise InvalidSchema("Missing dependencies for SOCKS support.") jpayne@69: jpayne@69: jpayne@69: if typing.TYPE_CHECKING: jpayne@69: from .models import PreparedRequest jpayne@69: jpayne@69: jpayne@69: DEFAULT_POOLBLOCK = False jpayne@69: DEFAULT_POOLSIZE = 10 jpayne@69: DEFAULT_RETRIES = 0 jpayne@69: DEFAULT_POOL_TIMEOUT = None jpayne@69: jpayne@69: jpayne@69: try: jpayne@69: import ssl # noqa: F401 jpayne@69: jpayne@69: _preloaded_ssl_context = create_urllib3_context() jpayne@69: _preloaded_ssl_context.load_verify_locations( jpayne@69: extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH) jpayne@69: ) jpayne@69: except ImportError: jpayne@69: # Bypass default SSLContext creation when Python jpayne@69: # interpreter isn't built with the ssl module. jpayne@69: _preloaded_ssl_context = None jpayne@69: jpayne@69: jpayne@69: def _urllib3_request_context( jpayne@69: request: "PreparedRequest", jpayne@69: verify: "bool | str | None", jpayne@69: client_cert: "typing.Tuple[str, str] | str | None", jpayne@69: poolmanager: "PoolManager", jpayne@69: ) -> "(typing.Dict[str, typing.Any], typing.Dict[str, typing.Any])": jpayne@69: host_params = {} jpayne@69: pool_kwargs = {} jpayne@69: parsed_request_url = urlparse(request.url) jpayne@69: scheme = parsed_request_url.scheme.lower() jpayne@69: port = parsed_request_url.port jpayne@69: jpayne@69: # Determine if we have and should use our default SSLContext jpayne@69: # to optimize performance on standard requests. jpayne@69: poolmanager_kwargs = getattr(poolmanager, "connection_pool_kw", {}) jpayne@69: has_poolmanager_ssl_context = poolmanager_kwargs.get("ssl_context") jpayne@69: should_use_default_ssl_context = ( jpayne@69: _preloaded_ssl_context is not None and not has_poolmanager_ssl_context jpayne@69: ) jpayne@69: jpayne@69: cert_reqs = "CERT_REQUIRED" jpayne@69: if verify is False: jpayne@69: cert_reqs = "CERT_NONE" jpayne@69: elif verify is True and should_use_default_ssl_context: jpayne@69: pool_kwargs["ssl_context"] = _preloaded_ssl_context jpayne@69: elif isinstance(verify, str): jpayne@69: if not os.path.isdir(verify): jpayne@69: pool_kwargs["ca_certs"] = verify jpayne@69: else: jpayne@69: pool_kwargs["ca_cert_dir"] = verify jpayne@69: pool_kwargs["cert_reqs"] = cert_reqs jpayne@69: if client_cert is not None: jpayne@69: if isinstance(client_cert, tuple) and len(client_cert) == 2: jpayne@69: pool_kwargs["cert_file"] = client_cert[0] jpayne@69: pool_kwargs["key_file"] = client_cert[1] jpayne@69: else: jpayne@69: # According to our docs, we allow users to specify just the client jpayne@69: # cert path jpayne@69: pool_kwargs["cert_file"] = client_cert jpayne@69: host_params = { jpayne@69: "scheme": scheme, jpayne@69: "host": parsed_request_url.hostname, jpayne@69: "port": port, jpayne@69: } jpayne@69: return host_params, pool_kwargs jpayne@69: jpayne@69: jpayne@69: class BaseAdapter: jpayne@69: """The Base Transport Adapter""" jpayne@69: jpayne@69: def __init__(self): jpayne@69: super().__init__() jpayne@69: jpayne@69: def send( jpayne@69: self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None jpayne@69: ): jpayne@69: """Sends PreparedRequest object. Returns Response object. jpayne@69: jpayne@69: :param request: The :class:`PreparedRequest ` being sent. jpayne@69: :param stream: (optional) Whether to stream the request content. jpayne@69: :param timeout: (optional) How long to wait for the server to send jpayne@69: data before giving up, as a float, or a :ref:`(connect timeout, jpayne@69: read timeout) ` tuple. jpayne@69: :type timeout: float or tuple jpayne@69: :param verify: (optional) Either a boolean, in which case it controls whether we verify jpayne@69: the server's TLS certificate, or a string, in which case it must be a path jpayne@69: to a CA bundle to use jpayne@69: :param cert: (optional) Any user-provided SSL certificate to be trusted. jpayne@69: :param proxies: (optional) The proxies dictionary to apply to the request. jpayne@69: """ jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def close(self): jpayne@69: """Cleans up adapter specific items.""" jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: jpayne@69: class HTTPAdapter(BaseAdapter): jpayne@69: """The built-in HTTP Adapter for urllib3. jpayne@69: jpayne@69: Provides a general-case interface for Requests sessions to contact HTTP and jpayne@69: HTTPS urls by implementing the Transport Adapter interface. This class will jpayne@69: usually be created by the :class:`Session ` class under the jpayne@69: covers. jpayne@69: jpayne@69: :param pool_connections: The number of urllib3 connection pools to cache. jpayne@69: :param pool_maxsize: The maximum number of connections to save in the pool. jpayne@69: :param max_retries: The maximum number of retries each connection jpayne@69: should attempt. Note, this applies only to failed DNS lookups, socket jpayne@69: connections and connection timeouts, never to requests where data has jpayne@69: made it to the server. By default, Requests does not retry failed jpayne@69: connections. If you need granular control over the conditions under jpayne@69: which we retry a request, import urllib3's ``Retry`` class and pass jpayne@69: that instead. jpayne@69: :param pool_block: Whether the connection pool should block for connections. jpayne@69: jpayne@69: Usage:: jpayne@69: jpayne@69: >>> import requests jpayne@69: >>> s = requests.Session() jpayne@69: >>> a = requests.adapters.HTTPAdapter(max_retries=3) jpayne@69: >>> s.mount('http://', a) jpayne@69: """ jpayne@69: jpayne@69: __attrs__ = [ jpayne@69: "max_retries", jpayne@69: "config", jpayne@69: "_pool_connections", jpayne@69: "_pool_maxsize", jpayne@69: "_pool_block", jpayne@69: ] jpayne@69: jpayne@69: def __init__( jpayne@69: self, jpayne@69: pool_connections=DEFAULT_POOLSIZE, jpayne@69: pool_maxsize=DEFAULT_POOLSIZE, jpayne@69: max_retries=DEFAULT_RETRIES, jpayne@69: pool_block=DEFAULT_POOLBLOCK, jpayne@69: ): jpayne@69: if max_retries == DEFAULT_RETRIES: jpayne@69: self.max_retries = Retry(0, read=False) jpayne@69: else: jpayne@69: self.max_retries = Retry.from_int(max_retries) jpayne@69: self.config = {} jpayne@69: self.proxy_manager = {} jpayne@69: jpayne@69: super().__init__() jpayne@69: jpayne@69: self._pool_connections = pool_connections jpayne@69: self._pool_maxsize = pool_maxsize jpayne@69: self._pool_block = pool_block jpayne@69: jpayne@69: self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block) jpayne@69: jpayne@69: def __getstate__(self): jpayne@69: return {attr: getattr(self, attr, None) for attr in self.__attrs__} jpayne@69: jpayne@69: def __setstate__(self, state): jpayne@69: # Can't handle by adding 'proxy_manager' to self.__attrs__ because jpayne@69: # self.poolmanager uses a lambda function, which isn't pickleable. jpayne@69: self.proxy_manager = {} jpayne@69: self.config = {} jpayne@69: jpayne@69: for attr, value in state.items(): jpayne@69: setattr(self, attr, value) jpayne@69: jpayne@69: self.init_poolmanager( jpayne@69: self._pool_connections, self._pool_maxsize, block=self._pool_block jpayne@69: ) jpayne@69: jpayne@69: def init_poolmanager( jpayne@69: self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs jpayne@69: ): jpayne@69: """Initializes a urllib3 PoolManager. jpayne@69: jpayne@69: This method should not be called from user code, and is only jpayne@69: exposed for use when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param connections: The number of urllib3 connection pools to cache. jpayne@69: :param maxsize: The maximum number of connections to save in the pool. jpayne@69: :param block: Block when no free connections are available. jpayne@69: :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager. jpayne@69: """ jpayne@69: # save these values for pickling jpayne@69: self._pool_connections = connections jpayne@69: self._pool_maxsize = maxsize jpayne@69: self._pool_block = block jpayne@69: jpayne@69: self.poolmanager = PoolManager( jpayne@69: num_pools=connections, jpayne@69: maxsize=maxsize, jpayne@69: block=block, jpayne@69: **pool_kwargs, jpayne@69: ) jpayne@69: jpayne@69: def proxy_manager_for(self, proxy, **proxy_kwargs): jpayne@69: """Return urllib3 ProxyManager for the given proxy. jpayne@69: jpayne@69: This method should not be called from user code, and is only jpayne@69: exposed for use when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param proxy: The proxy to return a urllib3 ProxyManager for. jpayne@69: :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager. jpayne@69: :returns: ProxyManager jpayne@69: :rtype: urllib3.ProxyManager jpayne@69: """ jpayne@69: if proxy in self.proxy_manager: jpayne@69: manager = self.proxy_manager[proxy] jpayne@69: elif proxy.lower().startswith("socks"): jpayne@69: username, password = get_auth_from_url(proxy) jpayne@69: manager = self.proxy_manager[proxy] = SOCKSProxyManager( jpayne@69: proxy, jpayne@69: username=username, jpayne@69: password=password, jpayne@69: num_pools=self._pool_connections, jpayne@69: maxsize=self._pool_maxsize, jpayne@69: block=self._pool_block, jpayne@69: **proxy_kwargs, jpayne@69: ) jpayne@69: else: jpayne@69: proxy_headers = self.proxy_headers(proxy) jpayne@69: manager = self.proxy_manager[proxy] = proxy_from_url( jpayne@69: proxy, jpayne@69: proxy_headers=proxy_headers, jpayne@69: num_pools=self._pool_connections, jpayne@69: maxsize=self._pool_maxsize, jpayne@69: block=self._pool_block, jpayne@69: **proxy_kwargs, jpayne@69: ) jpayne@69: jpayne@69: return manager jpayne@69: jpayne@69: def cert_verify(self, conn, url, verify, cert): jpayne@69: """Verify a SSL certificate. This method should not be called from user jpayne@69: code, and is only exposed for use when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param conn: The urllib3 connection object associated with the cert. jpayne@69: :param url: The requested URL. jpayne@69: :param verify: Either a boolean, in which case it controls whether we verify jpayne@69: the server's TLS certificate, or a string, in which case it must be a path jpayne@69: to a CA bundle to use jpayne@69: :param cert: The SSL certificate to verify. jpayne@69: """ jpayne@69: if url.lower().startswith("https") and verify: jpayne@69: conn.cert_reqs = "CERT_REQUIRED" jpayne@69: jpayne@69: # Only load the CA certificates if 'verify' is a string indicating the CA bundle to use. jpayne@69: # Otherwise, if verify is a boolean, we don't load anything since jpayne@69: # the connection will be using a context with the default certificates already loaded, jpayne@69: # and this avoids a call to the slow load_verify_locations() jpayne@69: if verify is not True: jpayne@69: # `verify` must be a str with a path then jpayne@69: cert_loc = verify jpayne@69: jpayne@69: if not os.path.exists(cert_loc): jpayne@69: raise OSError( jpayne@69: f"Could not find a suitable TLS CA certificate bundle, " jpayne@69: f"invalid path: {cert_loc}" jpayne@69: ) jpayne@69: jpayne@69: if not os.path.isdir(cert_loc): jpayne@69: conn.ca_certs = cert_loc jpayne@69: else: jpayne@69: conn.ca_cert_dir = cert_loc jpayne@69: else: jpayne@69: conn.cert_reqs = "CERT_NONE" jpayne@69: conn.ca_certs = None jpayne@69: conn.ca_cert_dir = None jpayne@69: jpayne@69: if cert: jpayne@69: if not isinstance(cert, basestring): jpayne@69: conn.cert_file = cert[0] jpayne@69: conn.key_file = cert[1] jpayne@69: else: jpayne@69: conn.cert_file = cert jpayne@69: conn.key_file = None jpayne@69: if conn.cert_file and not os.path.exists(conn.cert_file): jpayne@69: raise OSError( jpayne@69: f"Could not find the TLS certificate file, " jpayne@69: f"invalid path: {conn.cert_file}" jpayne@69: ) jpayne@69: if conn.key_file and not os.path.exists(conn.key_file): jpayne@69: raise OSError( jpayne@69: f"Could not find the TLS key file, invalid path: {conn.key_file}" jpayne@69: ) jpayne@69: jpayne@69: def build_response(self, req, resp): jpayne@69: """Builds a :class:`Response ` object from a urllib3 jpayne@69: response. This should not be called from user code, and is only exposed jpayne@69: for use when subclassing the jpayne@69: :class:`HTTPAdapter ` jpayne@69: jpayne@69: :param req: The :class:`PreparedRequest ` used to generate the response. jpayne@69: :param resp: The urllib3 response object. jpayne@69: :rtype: requests.Response jpayne@69: """ jpayne@69: response = Response() jpayne@69: jpayne@69: # Fallback to None if there's no status_code, for whatever reason. jpayne@69: response.status_code = getattr(resp, "status", None) jpayne@69: jpayne@69: # Make headers case-insensitive. jpayne@69: response.headers = CaseInsensitiveDict(getattr(resp, "headers", {})) jpayne@69: jpayne@69: # Set encoding. jpayne@69: response.encoding = get_encoding_from_headers(response.headers) jpayne@69: response.raw = resp jpayne@69: response.reason = response.raw.reason jpayne@69: jpayne@69: if isinstance(req.url, bytes): jpayne@69: response.url = req.url.decode("utf-8") jpayne@69: else: jpayne@69: response.url = req.url jpayne@69: jpayne@69: # Add new cookies from the server. jpayne@69: extract_cookies_to_jar(response.cookies, req, resp) jpayne@69: jpayne@69: # Give the Response some context. jpayne@69: response.request = req jpayne@69: response.connection = self jpayne@69: jpayne@69: return response jpayne@69: jpayne@69: def build_connection_pool_key_attributes(self, request, verify, cert=None): jpayne@69: """Build the PoolKey attributes used by urllib3 to return a connection. jpayne@69: jpayne@69: This looks at the PreparedRequest, the user-specified verify value, jpayne@69: and the value of the cert parameter to determine what PoolKey values jpayne@69: to use to select a connection from a given urllib3 Connection Pool. jpayne@69: jpayne@69: The SSL related pool key arguments are not consistently set. As of jpayne@69: this writing, use the following to determine what keys may be in that jpayne@69: dictionary: jpayne@69: jpayne@69: * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the jpayne@69: default Requests SSL Context jpayne@69: * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but jpayne@69: ``"cert_reqs"`` will be set jpayne@69: * If ``verify`` is a string, (i.e., it is a user-specified trust bundle) jpayne@69: ``"ca_certs"`` will be set if the string is not a directory recognized jpayne@69: by :py:func:`os.path.isdir`, otherwise ``"ca_certs_dir"`` will be jpayne@69: set. jpayne@69: * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If jpayne@69: ``"cert"`` is a tuple with a second item, ``"key_file"`` will also jpayne@69: be present jpayne@69: jpayne@69: To override these settings, one may subclass this class, call this jpayne@69: method and use the above logic to change parameters as desired. For jpayne@69: example, if one wishes to use a custom :py:class:`ssl.SSLContext` one jpayne@69: must both set ``"ssl_context"`` and based on what else they require, jpayne@69: alter the other keys to ensure the desired behaviour. jpayne@69: jpayne@69: :param request: jpayne@69: The PreparedReqest being sent over the connection. jpayne@69: :type request: jpayne@69: :class:`~requests.models.PreparedRequest` jpayne@69: :param verify: jpayne@69: Either a boolean, in which case it controls whether jpayne@69: we verify the server's TLS certificate, or a string, in which case it jpayne@69: must be a path to a CA bundle to use. jpayne@69: :param cert: jpayne@69: (optional) Any user-provided SSL certificate for client jpayne@69: authentication (a.k.a., mTLS). This may be a string (i.e., just jpayne@69: the path to a file which holds both certificate and key) or a jpayne@69: tuple of length 2 with the certificate file path and key file jpayne@69: path. jpayne@69: :returns: jpayne@69: A tuple of two dictionaries. The first is the "host parameters" jpayne@69: portion of the Pool Key including scheme, hostname, and port. The jpayne@69: second is a dictionary of SSLContext related parameters. jpayne@69: """ jpayne@69: return _urllib3_request_context(request, verify, cert, self.poolmanager) jpayne@69: jpayne@69: def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None): jpayne@69: """Returns a urllib3 connection for the given request and TLS settings. jpayne@69: This should not be called from user code, and is only exposed for use jpayne@69: when subclassing the :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param request: jpayne@69: The :class:`PreparedRequest ` object to be sent jpayne@69: over the connection. jpayne@69: :param verify: jpayne@69: Either a boolean, in which case it controls whether we verify the jpayne@69: server's TLS certificate, or a string, in which case it must be a jpayne@69: path to a CA bundle to use. jpayne@69: :param proxies: jpayne@69: (optional) The proxies dictionary to apply to the request. jpayne@69: :param cert: jpayne@69: (optional) Any user-provided SSL certificate to be used for client jpayne@69: authentication (a.k.a., mTLS). jpayne@69: :rtype: jpayne@69: urllib3.ConnectionPool jpayne@69: """ jpayne@69: proxy = select_proxy(request.url, proxies) jpayne@69: try: jpayne@69: host_params, pool_kwargs = self.build_connection_pool_key_attributes( jpayne@69: request, jpayne@69: verify, jpayne@69: cert, jpayne@69: ) jpayne@69: except ValueError as e: jpayne@69: raise InvalidURL(e, request=request) jpayne@69: if proxy: jpayne@69: proxy = prepend_scheme_if_needed(proxy, "http") jpayne@69: proxy_url = parse_url(proxy) jpayne@69: if not proxy_url.host: jpayne@69: raise InvalidProxyURL( jpayne@69: "Please check proxy URL. It is malformed " jpayne@69: "and could be missing the host." jpayne@69: ) jpayne@69: proxy_manager = self.proxy_manager_for(proxy) jpayne@69: conn = proxy_manager.connection_from_host( jpayne@69: **host_params, pool_kwargs=pool_kwargs jpayne@69: ) jpayne@69: else: jpayne@69: # Only scheme should be lower case jpayne@69: conn = self.poolmanager.connection_from_host( jpayne@69: **host_params, pool_kwargs=pool_kwargs jpayne@69: ) jpayne@69: jpayne@69: return conn jpayne@69: jpayne@69: def get_connection(self, url, proxies=None): jpayne@69: """DEPRECATED: Users should move to `get_connection_with_tls_context` jpayne@69: for all subclasses of HTTPAdapter using Requests>=2.32.2. jpayne@69: jpayne@69: Returns a urllib3 connection for the given URL. This should not be jpayne@69: called from user code, and is only exposed for use when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param url: The URL to connect to. jpayne@69: :param proxies: (optional) A Requests-style dictionary of proxies used on this request. jpayne@69: :rtype: urllib3.ConnectionPool jpayne@69: """ jpayne@69: warnings.warn( jpayne@69: ( jpayne@69: "`get_connection` has been deprecated in favor of " jpayne@69: "`get_connection_with_tls_context`. Custom HTTPAdapter subclasses " jpayne@69: "will need to migrate for Requests>=2.32.2. Please see " jpayne@69: "https://github.com/psf/requests/pull/6710 for more details." jpayne@69: ), jpayne@69: DeprecationWarning, jpayne@69: ) jpayne@69: proxy = select_proxy(url, proxies) jpayne@69: jpayne@69: if proxy: jpayne@69: proxy = prepend_scheme_if_needed(proxy, "http") jpayne@69: proxy_url = parse_url(proxy) jpayne@69: if not proxy_url.host: jpayne@69: raise InvalidProxyURL( jpayne@69: "Please check proxy URL. It is malformed " jpayne@69: "and could be missing the host." jpayne@69: ) jpayne@69: proxy_manager = self.proxy_manager_for(proxy) jpayne@69: conn = proxy_manager.connection_from_url(url) jpayne@69: else: jpayne@69: # Only scheme should be lower case jpayne@69: parsed = urlparse(url) jpayne@69: url = parsed.geturl() jpayne@69: conn = self.poolmanager.connection_from_url(url) jpayne@69: jpayne@69: return conn jpayne@69: jpayne@69: def close(self): jpayne@69: """Disposes of any internal state. jpayne@69: jpayne@69: Currently, this closes the PoolManager and any active ProxyManager, jpayne@69: which closes any pooled connections. jpayne@69: """ jpayne@69: self.poolmanager.clear() jpayne@69: for proxy in self.proxy_manager.values(): jpayne@69: proxy.clear() jpayne@69: jpayne@69: def request_url(self, request, proxies): jpayne@69: """Obtain the url to use when making the final request. jpayne@69: jpayne@69: If the message is being sent through a HTTP proxy, the full URL has to jpayne@69: be used. Otherwise, we should only use the path portion of the URL. jpayne@69: jpayne@69: This should not be called from user code, and is only exposed for use jpayne@69: when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param request: The :class:`PreparedRequest ` being sent. jpayne@69: :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. jpayne@69: :rtype: str jpayne@69: """ jpayne@69: proxy = select_proxy(request.url, proxies) jpayne@69: scheme = urlparse(request.url).scheme jpayne@69: jpayne@69: is_proxied_http_request = proxy and scheme != "https" jpayne@69: using_socks_proxy = False jpayne@69: if proxy: jpayne@69: proxy_scheme = urlparse(proxy).scheme.lower() jpayne@69: using_socks_proxy = proxy_scheme.startswith("socks") jpayne@69: jpayne@69: url = request.path_url jpayne@69: if url.startswith("//"): # Don't confuse urllib3 jpayne@69: url = f"/{url.lstrip('/')}" jpayne@69: jpayne@69: if is_proxied_http_request and not using_socks_proxy: jpayne@69: url = urldefragauth(request.url) jpayne@69: jpayne@69: return url jpayne@69: jpayne@69: def add_headers(self, request, **kwargs): jpayne@69: """Add any headers needed by the connection. As of v2.0 this does jpayne@69: nothing by default, but is left for overriding by users that subclass jpayne@69: the :class:`HTTPAdapter `. jpayne@69: jpayne@69: This should not be called from user code, and is only exposed for use jpayne@69: when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param request: The :class:`PreparedRequest ` to add headers to. jpayne@69: :param kwargs: The keyword arguments from the call to send(). jpayne@69: """ jpayne@69: pass jpayne@69: jpayne@69: def proxy_headers(self, proxy): jpayne@69: """Returns a dictionary of the headers to add to any request sent jpayne@69: through a proxy. This works with urllib3 magic to ensure that they are jpayne@69: correctly sent to the proxy, rather than in a tunnelled request if jpayne@69: CONNECT is being used. jpayne@69: jpayne@69: This should not be called from user code, and is only exposed for use jpayne@69: when subclassing the jpayne@69: :class:`HTTPAdapter `. jpayne@69: jpayne@69: :param proxy: The url of the proxy being used for this request. jpayne@69: :rtype: dict jpayne@69: """ jpayne@69: headers = {} jpayne@69: username, password = get_auth_from_url(proxy) jpayne@69: jpayne@69: if username: jpayne@69: headers["Proxy-Authorization"] = _basic_auth_str(username, password) jpayne@69: jpayne@69: return headers jpayne@69: jpayne@69: def send( jpayne@69: self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None jpayne@69: ): jpayne@69: """Sends PreparedRequest object. Returns Response object. jpayne@69: jpayne@69: :param request: The :class:`PreparedRequest ` being sent. jpayne@69: :param stream: (optional) Whether to stream the request content. jpayne@69: :param timeout: (optional) How long to wait for the server to send jpayne@69: data before giving up, as a float, or a :ref:`(connect timeout, jpayne@69: read timeout) ` tuple. jpayne@69: :type timeout: float or tuple or urllib3 Timeout object jpayne@69: :param verify: (optional) Either a boolean, in which case it controls whether jpayne@69: we verify the server's TLS certificate, or a string, in which case it jpayne@69: must be a path to a CA bundle to use jpayne@69: :param cert: (optional) Any user-provided SSL certificate to be trusted. jpayne@69: :param proxies: (optional) The proxies dictionary to apply to the request. jpayne@69: :rtype: requests.Response jpayne@69: """ jpayne@69: jpayne@69: try: jpayne@69: conn = self.get_connection_with_tls_context( jpayne@69: request, verify, proxies=proxies, cert=cert jpayne@69: ) jpayne@69: except LocationValueError as e: jpayne@69: raise InvalidURL(e, request=request) jpayne@69: jpayne@69: self.cert_verify(conn, request.url, verify, cert) jpayne@69: url = self.request_url(request, proxies) jpayne@69: self.add_headers( jpayne@69: request, jpayne@69: stream=stream, jpayne@69: timeout=timeout, jpayne@69: verify=verify, jpayne@69: cert=cert, jpayne@69: proxies=proxies, jpayne@69: ) jpayne@69: jpayne@69: chunked = not (request.body is None or "Content-Length" in request.headers) jpayne@69: jpayne@69: if isinstance(timeout, tuple): jpayne@69: try: jpayne@69: connect, read = timeout jpayne@69: timeout = TimeoutSauce(connect=connect, read=read) jpayne@69: except ValueError: jpayne@69: raise ValueError( jpayne@69: f"Invalid timeout {timeout}. Pass a (connect, read) timeout tuple, " jpayne@69: f"or a single float to set both timeouts to the same value." jpayne@69: ) jpayne@69: elif isinstance(timeout, TimeoutSauce): jpayne@69: pass jpayne@69: else: jpayne@69: timeout = TimeoutSauce(connect=timeout, read=timeout) jpayne@69: jpayne@69: try: jpayne@69: resp = conn.urlopen( jpayne@69: method=request.method, jpayne@69: url=url, jpayne@69: body=request.body, jpayne@69: headers=request.headers, jpayne@69: redirect=False, jpayne@69: assert_same_host=False, jpayne@69: preload_content=False, jpayne@69: decode_content=False, jpayne@69: retries=self.max_retries, jpayne@69: timeout=timeout, jpayne@69: chunked=chunked, jpayne@69: ) jpayne@69: jpayne@69: except (ProtocolError, OSError) as err: jpayne@69: raise ConnectionError(err, request=request) jpayne@69: jpayne@69: except MaxRetryError as e: jpayne@69: if isinstance(e.reason, ConnectTimeoutError): jpayne@69: # TODO: Remove this in 3.0.0: see #2811 jpayne@69: if not isinstance(e.reason, NewConnectionError): jpayne@69: raise ConnectTimeout(e, request=request) jpayne@69: jpayne@69: if isinstance(e.reason, ResponseError): jpayne@69: raise RetryError(e, request=request) jpayne@69: jpayne@69: if isinstance(e.reason, _ProxyError): jpayne@69: raise ProxyError(e, request=request) jpayne@69: jpayne@69: if isinstance(e.reason, _SSLError): jpayne@69: # This branch is for urllib3 v1.22 and later. jpayne@69: raise SSLError(e, request=request) jpayne@69: jpayne@69: raise ConnectionError(e, request=request) jpayne@69: jpayne@69: except ClosedPoolError as e: jpayne@69: raise ConnectionError(e, request=request) jpayne@69: jpayne@69: except _ProxyError as e: jpayne@69: raise ProxyError(e) jpayne@69: jpayne@69: except (_SSLError, _HTTPError) as e: jpayne@69: if isinstance(e, _SSLError): jpayne@69: # This branch is for urllib3 versions earlier than v1.22 jpayne@69: raise SSLError(e, request=request) jpayne@69: elif isinstance(e, ReadTimeoutError): jpayne@69: raise ReadTimeout(e, request=request) jpayne@69: elif isinstance(e, _InvalidHeader): jpayne@69: raise InvalidHeader(e, request=request) jpayne@69: else: jpayne@69: raise jpayne@69: jpayne@69: return self.build_response(request, resp)