o
    'hY                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 ddl
mZmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZmZmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ej'rd dl(Z(d dl)m*Z* g dZ+e,e-Z.dZ/dZ0G dd dej1Z2d)ddZ3e4e3e2e4e3e2dZ5eedZ6G dd  d eZ7G d!d" d"e7Z8d*d'd(Z9dS )+    )annotationsN)TracebackType)urljoin   )HTTPHeaderDictRecentlyUsedContainer)RequestMethods)ProxyConfig)HTTPConnectionPoolHTTPSConnectionPoolport_by_scheme)LocationValueErrorMaxRetryErrorProxySchemeUnknownURLSchemeUnknown)BaseHTTPResponse)_TYPE_SOCKET_OPTIONS)connection_requires_http_tunnel)Retry)Timeout)Url	parse_url)Self)PoolManagerProxyManagerproxy_from_url)key_file	cert_file	cert_reqsca_certsca_cert_datassl_versionssl_minimum_versionssl_maximum_versionca_cert_dirssl_contextkey_passwordserver_hostnamei @  c                   @  s   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< d ed!< d"ed#< d ed$< d%ed&< d'ed(< d ed)< d*ed+< ded,< ded-< ded.< d/S )0PoolKeyz
    All known keyword arguments that could be provided to the pool manager, its
    pools, or the underlying connections.

    All custom key schemes should include the fields in this key at a minimum.
    str
key_schemekey_host
int | Nonekey_portzTimeout | float | int | Nonekey_timeoutzRetry | bool | int | Nonekey_retrieszbool | None	key_blockztuple[str, int] | Nonekey_source_address
str | Nonekey_key_filekey_key_passwordkey_cert_filekey_cert_reqskey_ca_certszstr | bytes | Nonekey_ca_cert_datazint | str | Nonekey_ssl_versionzssl.TLSVersion | Nonekey_ssl_minimum_versionkey_ssl_maximum_versionkey_ca_cert_dirssl.SSLContext | Nonekey_ssl_contextkey_maxsizez!frozenset[tuple[str, str]] | Nonekey_headers
Url | None
key__proxykey__proxy_headersProxyConfig | Nonekey__proxy_configz_TYPE_SOCKET_OPTIONS | Nonekey_socket_optionskey__socks_optionszbool | str | Nonekey_assert_hostnamekey_assert_fingerprintkey_server_hostnamekey_blocksizeN)__name__
__module____qualname____doc____annotations__ rQ   rQ   O/var/www/html/olx_land/venv/lib/python3.10/site-packages/urllib3/poolmanager.pyr(   8   s>   
 r(   	key_classtype[PoolKey]request_contextdict[str, typing.Any]returnc                 C  s   |  }|d  |d< |d  |d< dD ]}||v r,|| dur,t||  ||< q|d}|dur<t||d< t| D ]}|||d| < qB| j	D ]
}||vr[d||< qQ|ddu rgt
|d< | di |S )	a  
    Create a pool key out of a request context dictionary.

    According to RFC 3986, both the scheme and host are case-insensitive.
    Therefore, this function normalizes both before constructing the pool
    key for an HTTPS request. If you wish to change this behaviour, provide
    alternate callables to ``key_fn_by_scheme``.

    :param key_class:
        The class to use when constructing the key. This should be a namedtuple
        with the ``scheme`` and ``host`` keys at a minimum.
    :type  key_class: namedtuple
    :param request_context:
        A dictionary-like object that contain the context for a request.
    :type  request_context: dict

    :return: A namedtuple that can be used as a connection pool key.
    :rtype:  PoolKey
    schemehost)headers_proxy_headers_socks_optionsNsocket_optionskey_rK   rQ   )copylower	frozensetitemsgettuplelistkeyspop_fields_DEFAULT_BLOCKSIZE)rS   rU   contextkeysocket_optsfieldrQ   rQ   rR   _default_key_normalizer_   s&   

rn   httphttpsc                      s   e Zd ZU dZdZded< dZded< 		dHdI fddZdJddZdKddZ		dLdMd%d&Z
dNd'd(Z		)	dOdPd-d.ZdQd0d1ZdRd4d5Z	dLdSd7d8ZdTd:d;ZdUd?d@Z	AdVdWdFdGZ  ZS )Xr   aV  
    Allows for arbitrary requests while transparently keeping track of
    necessary connection pools for you.

    :param num_pools:
        Number of connection pools to cache before discarding the least
        recently used pool.

    :param headers:
        Headers to include with all requests, unless other headers are given
        explicitly.

    :param \**connection_pool_kw:
        Additional parameters are used to create fresh
        :class:`urllib3.connectionpool.ConnectionPool` instances.

    Example:

    .. code-block:: python

        import urllib3

        http = urllib3.PoolManager(num_pools=2)

        resp1 = http.request("GET", "https://google.com/")
        resp2 = http.request("GET", "https://google.com/mail")
        resp3 = http.request("GET", "https://yahoo.com/")

        print(len(http.pools))
        # 2

    NrA   proxyrD   proxy_config
   	num_poolsintrZ   typing.Mapping[str, str] | Noneconnection_pool_kw
typing.AnyrW   Nonec                   s4   t  | || _|  t|| _t| _t | _d S N)super__init__rx   r   poolspool_classes_by_schemekey_fn_by_schemer_   )selfru   rZ   rx   	__class__rQ   rR   r}      s   
zPoolManager.__init__r   c                 C  s   | S r{   rQ   r   rQ   rQ   rR   	__enter__   s   zPoolManager.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonetyping.Literal[False]c                 C  s   |    dS )NF)clear)r   r   r   r   rQ   rQ   rR   __exit__   s   zPoolManager.__exit__rX   r)   rY   portrU   dict[str, typing.Any] | Noner
   c                 C  sx   | j | }|du r| j }|ddu rt|d< dD ]}||d q|dkr3tD ]}||d q*|||fi |S )a  
        Create a new :class:`urllib3.connectionpool.ConnectionPool` based on host, port, scheme, and
        any additional pool keyword arguments.

        If ``request_context`` is provided, it is provided as keyword arguments
        to the pool class used. This method is used to actually create the
        connection pools handed out by :meth:`connection_from_url` and
        companion methods. It is intended to be overridden for customization.
        N	blocksize)rX   rY   r   rp   )r   rx   r_   rc   ri   rg   SSL_KEYWORDS)r   rX   rY   r   rU   pool_clsrk   kwrQ   rQ   rR   	_new_pool   s   

zPoolManager._new_poolc                 C  s   | j   dS )z
        Empty our store of pools and direct them all to close.

        This will not affect in-flight connections, but they will not be
        re-used after completion.
        N)r~   r   r   rQ   rQ   rR   r     s   zPoolManager.clearrp   r2   r,   pool_kwargsc                 C  sT   |st d| |}|pd|d< |st|d  d}||d< ||d< | |S )a  
        Get a :class:`urllib3.connectionpool.ConnectionPool` based on the host, port, and scheme.

        If ``port`` isn't given, it will be derived from the ``scheme`` using
        ``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
        provided, it is merged with the instance's ``connection_pool_kw``
        variable and used to create the new connection pool, if one is
        needed.
        zNo host specified.rp   rX   P   r   rY   )r   _merge_pool_kwargsr   rc   r`   connection_from_context)r   rY   r   rX   r   rU   rQ   rQ   rR   connection_from_host  s   

z PoolManager.connection_from_hostrV   c                 C  sX   d|v rt dt |d |d  }| j|}|s!t|||}| j||dS )z
        Get a :class:`urllib3.connectionpool.ConnectionPool` based on the request context.

        ``request_context`` must at least contain the ``scheme`` key and its
        value must be a key in ``key_fn_by_scheme`` instance variable.
        strictzdThe 'strict' parameter is no longer needed on Python 3+. This will raise an error in urllib3 v2.1.0.rX   rU   )	warningswarnDeprecationWarningrg   r`   r   rc   r   connection_from_pool_key)r   rU   rX   pool_key_constructorpool_keyrQ   rQ   rR   r   1  s   	
z#PoolManager.connection_from_contextr   r(   c                 C  s   | j j4 | j |}|r|W  d   S |d }|d }|d }| j||||d}|| j |< W d   |S 1 s;w   Y  |S )a  
        Get a :class:`urllib3.connectionpool.ConnectionPool` based on the provided pool key.

        ``pool_key`` should be a namedtuple that only contains immutable
        objects. At a minimum it must have the ``scheme``, ``host``, and
        ``port`` fields.
        NrX   rY   r   r   )r~   lockrc   r   )r   r   rU   poolrX   rY   r   rQ   rQ   rR   r   J  s   


z$PoolManager.connection_from_pool_keyurlc                 C  s    t |}| j|j|j|j|dS )a  
        Similar to :func:`urllib3.connectionpool.connection_from_url`.

        If ``pool_kwargs`` is not provided and a new pool needs to be
        constructed, ``self.connection_pool_kw`` is used to initialize
        the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
        is provided, it is used instead. Note that if a new pool does not
        need to be created for the request, the provided ``pool_kwargs`` are
        not used.
        )r   rX   r   )r   r   rY   r   rX   )r   r   r   urQ   rQ   rR   connection_from_urld  s   zPoolManager.connection_from_urloverridec              	   C  sR   | j  }|r'| D ]\}}|du r"z||= W q ty!   Y qw |||< q|S )a  
        Merge a dictionary of override values for self.connection_pool_kw.

        This does not modify self.connection_pool_kw and returns a new dict.
        Any keys in the override dictionary with a value of ``None`` are
        removed from the merged dictionary.
        N)rx   r_   rb   KeyError)r   r   base_pool_kwargsrk   valuerQ   rQ   rR   r   v  s   



zPoolManager._merge_pool_kwargs
parsed_urlr   boolc                 C  s"   | j du rdS t| j | j|j S )z
        Indicates if the proxy requires the complete destination URL in the
        request.  Normally this is only needed when not using an HTTP CONNECT
        tunnel.
        NF)rr   r   rs   rX   )r   r   rQ   rQ   rR   !_proxy_requires_url_absolute_form  s
   
z-PoolManager._proxy_requires_url_absolute_formTmethodredirectr   r   c                 K  s  t |}|jdu rtjdtdd | j|j|j|jd}d|d< d|d< d	|vr-| j|d	< | 	|r=|j
||fi |}n|j
||jfi |}|oM| }|sR|S t||}|jd
krld}d|d< t|d	  |d	< |d}	t|	ts}tj|	|d}	|	jr||s|d	  }
|d	 D ]}| |	jv r|
|d q|
|d	< z|	j||||d}	W n ty   |	jr|   | Y S w |	|d< ||d< td|| |  | j
||fi |S )aN  
        Same as :meth:`urllib3.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        Na	  URLs without a scheme (ie 'https://') are deprecated and will raise an error in a future version of urllib3. To avoid this DeprecationWarning ensure all URLs start with 'https://' or 'http://'. Read more in this issue: https://github.com/urllib3/urllib3/issues/2920   )category
stacklevel)r   rX   Fassert_same_hostr   rZ   i/  GETbodyretries)r   )response_poolzRedirecting %s -> %s) r   rX   r   r   r   r   rY   r   rZ   r   urlopenrequest_uriget_redirect_locationr   statusr   _prepare_for_method_changerc   
isinstancer   from_intremove_headers_on_redirectis_same_hostr_   r`   rg   	incrementr   raise_on_redirect
drain_connloginfo)r   r   r   r   r   r   connr   redirect_locationr   new_headersheaderrQ   rQ   rR   r     s`   
	






zPoolManager.urlopen)rt   N)ru   rv   rZ   rw   rx   ry   rW   rz   )rW   r   )r   r   r   r   r   r   rW   r   r{   )
rX   r)   rY   r)   r   rv   rU   r   rW   r
   )rW   rz   Nrp   N
rY   r2   r   r,   rX   r2   r   r   rW   r
   )rU   rV   rW   r
   )r   r(   rU   rV   rW   r
   )r   r)   r   r   rW   r
   )r   r   rW   rV   )r   r   rW   r   T
r   r)   r   r)   r   r   r   ry   rW   r   )rL   rM   rN   rO   rr   rP   rs   r}   r   r   r   r   r   r   r   r   r   r   r   __classcell__rQ   rQ   r   rR   r      s0   
 !


&



r   c                      sh   e Zd ZdZ							d/d0 fddZ			d1d2 fd"d#Z	d3d4d&d'Z	(d5d6 fd-d.Z  ZS )7r   a>  
    Behaves just like :class:`PoolManager`, but sends all requests through
    the defined proxy, using the CONNECT method for HTTPS URLs.

    :param proxy_url:
        The URL of the proxy to be used.

    :param proxy_headers:
        A dictionary containing headers that will be sent to the proxy. In case
        of HTTP they are being sent with each request, while in the
        HTTPS/CONNECT case they are sent only once. Could be used for proxy
        authentication.

    :param proxy_ssl_context:
        The proxy SSL context is used to establish the TLS connection to the
        proxy when using HTTPS proxies.

    :param use_forwarding_for_https:
        (Defaults to False) If set to True will forward requests to the HTTPS
        proxy to be made on behalf of the client instead of creating a TLS
        tunnel via the CONNECT method. **Enabling this flag means that request
        and response headers and content will be visible from the HTTPS proxy**
        whereas tunneling keeps request and response headers and content
        private.  IP address, target hostname, SNI, and port are always visible
        to an HTTPS proxy even when this flag is disabled.

    :param proxy_assert_hostname:
        The hostname of the certificate to verify against.

    :param proxy_assert_fingerprint:
        The fingerprint of the certificate to verify against.

    Example:

    .. code-block:: python

        import urllib3

        proxy = urllib3.ProxyManager("https://localhost:3128/")

        resp1 = proxy.request("GET", "https://google.com/")
        resp2 = proxy.request("GET", "https://httpbin.org/")

        print(len(proxy.pools))
        # 1

        resp3 = proxy.request("GET", "https://httpbin.org/")
        resp4 = proxy.request("GET", "https://twitter.com/")

        print(len(proxy.pools))
        # 3

    rt   NF	proxy_urlr)   ru   rv   rZ   rw   proxy_headersproxy_ssl_contextr=   use_forwarding_for_httpsr   proxy_assert_hostname"None | str | typing.Literal[False]proxy_assert_fingerprintr2   rx   ry   rW   rz   c	                   s   t |tr|j d|j d|j }
n|}
t|
}|jdvr#t|j|js3t|jd}|j	|d}|| _
|p9i | _|| _t||||| _| j
|	d< | j|	d< | j|	d< t j||fi |	 d S )	Nz://:ro   r   )r   _proxyr[   _proxy_config)r   r
   rX   rY   r   r   r   r   rc   _replacerr   r   r   r	   rs   r|   r}   )r   r   ru   rZ   r   r   r   r   r   rx   str_proxy_urlrr   r   r   rQ   rR   r}   #  s,   






zProxyManager.__init__rp   rY   r   r,   rX   r   r   r
   c                   s<   |dkrt  j||||dS t  j| jj| jj| jj|dS )Nrq   )r   )r|   r   rr   rY   r   rX   )r   rY   r   rX   r   r   rQ   rR   r   L  s   z!ProxyManager.connection_from_hostr   typing.Mapping[str, str]c                 C  s0   ddi}t |j}|r||d< |r|| |S )z
        Sets headers needed by proxies: specifically, the Accept and Host
        headers. Only sets headers not provided by the user.
        Acceptz*/*Host)r   netlocupdate)r   r   rZ   headers_r   rQ   rQ   rR   _set_proxy_headers\  s   

zProxyManager._set_proxy_headersTr   r   r   r   c                   sR   t |}t| j| j|js|d| j}| |||d< t j	||fd|i|S )z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rZ   r   )
r   r   rr   rs   rX   rc   rZ   r   r|   r   )r   r   r   r   r   r   rZ   r   rQ   rR   r   m  s
   zProxyManager.urlopen)rt   NNNFNN)r   r)   ru   rv   rZ   rw   r   rw   r   r=   r   r   r   r   r   r2   rx   ry   rW   rz   r   r   r{   )r   r)   rZ   rw   rW   r   r   r   )	rL   rM   rN   rO   r}   r   r   r   r   rQ   rQ   r   rR   r     s$    9,r   r   r)   r   ry   c                 K  s   t dd| i|S )Nr   rQ   )r   )r   r   rQ   rQ   rR   r   |  s   r   )rS   rT   rU   rV   rW   r(   )r   r)   r   ry   rW   r   ):
__future__r   	functoolsloggingtypingr   typesr   urllib.parser   _collectionsr   r   _request_methodsr   
connectionr	   connectionpoolr
   r   r   
exceptionsr   r   r   r   r   r   util.connectionr   
util.proxyr   
util.retryr   util.timeoutr   util.urlr   r   TYPE_CHECKINGssltyping_extensionsr   __all__	getLoggerrL   r   r   ri   
NamedTupler(   rn   partialr   r   r   r   r   rQ   rQ   rQ   rR   <module>   sJ    

'
<

  L 