@@ -375,23 +375,83 @@ def build_response(self, req, resp):
375375
376376 return response
377377
378+ def build_connection_pool_key_attributes (self , request , verify , cert = None ):
379+ """Build the PoolKey attributes used by urllib3 to return a connection.
380+
381+ This looks at the PreparedRequest, the user-specified verify value,
382+ and the value of the cert parameter to determine what PoolKey values
383+ to use to select a connection from a given urllib3 Connection Pool.
384+
385+ The SSL related pool key arguments are not consistently set. As of
386+ this writing, use the following to determine what keys may be in that
387+ dictionary:
388+
389+ * If ``verify`` is ``True``, ``"ssl_context"`` will be set and will be the
390+ default Requests SSL Context
391+ * If ``verify`` is ``False``, ``"ssl_context"`` will not be set but
392+ ``"cert_reqs"`` will be set
393+ * If ``verify`` is a string, (i.e., it is a user-specified trust bundle)
394+ ``"ca_certs"`` will be set if the string is not a directory recognized
395+ by :py:func:`os.path.isdir`, otherwise ``"ca_certs_dir"`` will be
396+ set.
397+ * If ``"cert"`` is specified, ``"cert_file"`` will always be set. If
398+ ``"cert"`` is a tuple with a second item, ``"key_file"`` will also
399+ be present
400+
401+ To override these settings, one may subclass this class, call this
402+ method and use the above logic to change parameters as desired. For
403+ example, if one wishes to use a custom :py:class:`ssl.SSLContext` one
404+ must both set ``"ssl_context"`` and based on what else they require,
405+ alter the other keys to ensure the desired behaviour.
406+
407+ :param request:
408+ The PreparedReqest being sent over the connection.
409+ :type request:
410+ :class:`~requests.models.PreparedRequest`
411+ :param verify:
412+ Either a boolean, in which case it controls whether
413+ we verify the server's TLS certificate, or a string, in which case it
414+ must be a path to a CA bundle to use.
415+ :param cert:
416+ (optional) Any user-provided SSL certificate for client
417+ authentication (a.k.a., mTLS). This may be a string (i.e., just
418+ the path to a file which holds both certificate and key) or a
419+ tuple of length 2 with the certificate file path and key file
420+ path.
421+ :returns:
422+ A tuple of two dictionaries. The first is the "host parameters"
423+ portion of the Pool Key including scheme, hostname, and port. The
424+ second is a dictionary of SSLContext related parameters.
425+ """
426+ return _urllib3_request_context (request , verify , cert )
427+
378428 def get_connection_with_tls_context (self , request , verify , proxies = None , cert = None ):
379429 """Returns a urllib3 connection for the given request and TLS settings.
380430 This should not be called from user code, and is only exposed for use
381431 when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
382432
383- :param request: The :class:`PreparedRequest <PreparedRequest>` object
384- to be sent over the connection.
385- :param verify: Either a boolean, in which case it controls whether
386- we verify the server's TLS certificate, or a string, in which case it
387- must be a path to a CA bundle to use.
388- :param proxies: (optional) The proxies dictionary to apply to the request.
389- :param cert: (optional) Any user-provided SSL certificate to be trusted.
390- :rtype: urllib3.ConnectionPool
433+ :param request:
434+ The :class:`PreparedRequest <PreparedRequest>` object to be sent
435+ over the connection.
436+ :param verify:
437+ Either a boolean, in which case it controls whether we verify the
438+ server's TLS certificate, or a string, in which case it must be a
439+ path to a CA bundle to use.
440+ :param proxies:
441+ (optional) The proxies dictionary to apply to the request.
442+ :param cert:
443+ (optional) Any user-provided SSL certificate to be used for client
444+ authentication (a.k.a., mTLS).
445+ :rtype:
446+ urllib3.ConnectionPool
391447 """
392448 proxy = select_proxy (request .url , proxies )
393449 try :
394- host_params , pool_kwargs = _urllib3_request_context (request , verify , cert )
450+ host_params , pool_kwargs = self .build_connection_pool_key_attributes (
451+ request ,
452+ verify ,
453+ cert ,
454+ )
395455 except ValueError as e :
396456 raise InvalidURL (e , request = request )
397457 if proxy :
0 commit comments