B
    @)H[‘A  ã               @   s  d dl mZ d dlZd dlZd dlZddlmZ ddlmZm	Z	 ddlm
Z
 ddl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dgZe e¡ZdZdZe de¡Zdd„ Ze  ee¡e  ee¡dœZ!ee	dœZ"G dd„ deƒZ#G dd„ de#ƒZ$dd„ Z%dS )é    )Úabsolute_importNé   )ÚRecentlyUsedContainer)ÚHTTPConnectionPoolÚHTTPSConnectionPool)Úport_by_scheme)ÚLocationValueErrorÚMaxRetryErrorÚProxySchemeUnknown)Úurljoin)ÚRequestMethods)Ú	parse_url)ÚRetryÚPoolManagerÚProxyManagerÚproxy_from_url)Úkey_fileÚ	cert_fileÚ	cert_reqsÚca_certsÚssl_versionÚca_cert_dirÚssl_context)Ú
key_schemeÚkey_hostÚkey_portÚkey_timeoutÚkey_retriesÚ
key_strictÚ	key_blockÚkey_source_addressÚkey_key_fileÚkey_cert_fileÚkey_cert_reqsÚkey_ca_certsÚkey_ssl_versionÚkey_ca_cert_dirÚkey_ssl_contextÚkey_maxsizeÚkey_headersÚ
key__proxyÚkey__proxy_headersÚkey_socket_optionsÚkey__socks_optionsÚkey_assert_hostnameÚkey_assert_fingerprintÚPoolKeyc             C   sÎ   |  ¡ }|d  ¡ |d< |d  ¡ |d< x4dD ],}||kr.|| dk	r.t||  ¡ ƒ||< q.W | d¡}|dk	r|t|ƒ|d< x&t| ¡ ƒD ]}| |¡|d| < qŠW x| j	D ]}||kr¬d||< q¬W | f |Ž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_)
ÚcopyÚlowerÚ	frozensetÚitemsÚgetÚtupleÚlistÚkeysÚpopÚ_fields)Ú	key_classÚrequest_contextÚcontextÚkeyÚsocket_optsÚfield© rH   ú`C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-qz7kpjmn\pip\pip\_vendor\urllib3\poolmanager.pyÚ_default_key_normalizer9   s    

rJ   )ÚhttpÚhttpsc               @   sx   e Zd ZdZdZddd„Zdd„ Zdd	„ Zdd
d„Zdd„ Z	ddd„Z
dd„ Zddd„Zd dd„Zdd„ Zd!dd„ZdS )"r   a$  
    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::

        >>> manager = PoolManager(num_pools=2)
        >>> r = manager.request('GET', 'http://google.com/')
        >>> r = manager.request('GET', 'http://google.com/mail')
        >>> r = manager.request('GET', 'http://yahoo.com/')
        >>> len(manager.pools)
        2

    Né
   c             K   s8   t  | |¡ || _t|dd„ d�| _t| _t ¡ | _d S )Nc             S   s   |   ¡ S )N)Úclose)ÚprH   rH   rI   Ú<lambda>›   s    z&PoolManager.__init__.<locals>.<lambda>)Údispose_func)r   Ú__init__Úconnection_pool_kwr   ÚpoolsÚpool_classes_by_schemeÚkey_fn_by_schemer8   )ÚselfÚ	num_poolsr3   rS   rH   rH   rI   rR   —   s    zPoolManager.__init__c             C   s   | S )NrH   )rW   rH   rH   rI   Ú	__enter__¢   s    zPoolManager.__enter__c             C   s   |   ¡  dS )NF)Úclear)rW   Úexc_typeÚexc_valÚexc_tbrH   rH   rI   Ú__exit__¥   s    zPoolManager.__exit__c             C   sf   | j | }|dkr| j ¡ }xdD ]}| |d¡ q"W |dkrXxtD ]}| |d¡ qDW |||f|ŽS )a·  
        Create a new :class:`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)r1   r2   ÚportrK   )rU   rS   r8   r@   ÚSSL_KEYWORDS)rW   r1   r2   r_   rC   Úpool_clsrE   ÚkwrH   rH   rI   Ú	_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)rT   rZ   )rW   rH   rH   rI   rZ   Å   s    zPoolManager.clearrK   c             C   sT   |st dƒ‚|  |¡}|pd|d< |s:t |d  ¡ d¡}||d< ||d< |  |¡S )a�  
        Get a :class:`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.rK   r1   éP   r_   r2   )r   Ú_merge_pool_kwargsr   r<   r9   Úconnection_from_context)rW   r2   r_   r1   Úpool_kwargsrC   rH   rH   rI   Úconnection_from_hostÎ   s    
z PoolManager.connection_from_hostc             C   s,   |d   ¡ }| j| }||ƒ}| j||d�S )zâ
        Get a :class:`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.
        r1   )rC   )r9   rV   Úconnection_from_pool_key)rW   rC   r1   Úpool_key_constructorÚpool_keyrH   rH   rI   rf   å   s    
z#PoolManager.connection_from_contextc          	   C   s`   | j j�N | j  |¡}|r|S |d }|d }|d }| j||||d�}|| j |< W dQ R X |S )zû
        Get a :class:`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.
        r1   r2   r_   )rC   N)rT   Úlockr<   rc   )rW   rk   rC   Úpoolr1   r2   r_   rH   rH   rI   ri   ò   s    
z$PoolManager.connection_from_pool_keyc             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_   r1   rg   )r   rh   r2   r_   r1   )rW   Úurlrg   ÚurH   rH   rI   Úconnection_from_url
  s    zPoolManager.connection_from_urlc          	   C   sZ   | j  ¡ }|rVxF| ¡ D ]:\}}|dkrJy
||= W qR tk
rF   Y qRX q|||< qW |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)rS   r8   r;   ÚKeyError)rW   ÚoverrideÚbase_pool_kwargsrE   ÚvaluerH   rH   rI   re     s    

zPoolManager._merge_pool_kwargsTc       
      K   s(  t |ƒ}| j|j|j|jd�}d|d< d|d< d|kr@| j|d< | jdk	rf|jdkrf|j||f|Ž}n|j||jf|Ž}|o‚| 	¡ }|sŒ|S t
||ƒ}|jdkr¤d	}| d
¡}	t|	tƒsÆtj|	|d�}	y|	j||||d�}	W n tk
rø   |	jrô‚ |S X |	|d
< ||d< t d||¡ | j||f|ŽS )a]  
        Same as :meth:`urllib3.connectionpool.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.
        )r_   r1   FÚassert_same_hostÚredirectr3   NrK   i/  ÚGETÚretries)rv   )ÚresponseÚ_poolzRedirecting %s -> %s)r   rh   r2   r_   r1   r3   ÚproxyÚurlopenÚrequest_uriÚget_redirect_locationr   Ústatusr<   Ú
isinstancer   Úfrom_intÚ	incrementr	   Úraise_on_redirectÚlogÚinfo)
rW   Úmethodrn   rv   rb   ro   Úconnry   Úredirect_locationrx   rH   rH   rI   r|   -  s8    	




zPoolManager.urlopen)rM   N)N)NrK   N)N)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r{   rR   rY   r^   rc   rZ   rh   rf   ri   rp   re   r|   rH   rH   rH   rI   r   y   s   

	


c                   sH   e Zd ZdZd‡ fdd„	Zd‡ fdd„	Zdd	d
„Zd‡ fdd„	Z‡  ZS )r   aw  
    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 contaning 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.

    Example:
        >>> proxy = urllib3.ProxyManager('http://localhost:3128/')
        >>> r1 = proxy.request('GET', 'http://google.com/')
        >>> r2 = proxy.request('GET', 'http://httpbin.org/')
        >>> len(proxy.pools)
        1
        >>> r3 = proxy.request('GET', 'https://httpbin.org/')
        >>> r4 = proxy.request('GET', 'https://twitter.com/')
        >>> len(proxy.pools)
        3

    rM   Nc                s˜   t |tƒrd|j|j|jf }t|ƒ}|jsFt |jd¡}|j|d�}|jdkrZt	|jƒ‚|| _
|pfi | _| j
|d< | j|d< tt| ƒj||f|Ž d S )Nz
%s://%s:%ird   )r_   )rK   rL   Ú_proxyr4   )r€   r   r1   r2   r_   r   r   r<   Ú_replacer
   r{   Úproxy_headersÚsuperr   rR   )rW   Ú	proxy_urlrX   r3   r�   rS   r{   r_   )Ú	__class__rH   rI   rR   {  s    








zProxyManager.__init__rK   c                sD   |dkr t t| ƒj||||d�S t t| ƒj| jj| jj| jj|d�S )NrL   )rg   )r�   r   rh   r{   r2   r_   r1   )rW   r2   r_   r1   rg   )r’   rH   rI   rh   ’  s
    

z!ProxyManager.connection_from_hostc             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)rW   rn   r3   Úheaders_r•   rH   rH   rI   Ú_set_proxy_headersš  s    

zProxyManager._set_proxy_headersTc                sN   t |ƒ}|jdkr0| d| j¡}|  ||¡|d< tt| ƒj||fd|i|—ŽS )z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.rK   r3   rv   )r   r1   r<   r3   r˜   r�   r   r|   )rW   r†   rn   rv   rb   ro   r3   )r’   rH   rI   r|   ©  s
    
zProxyManager.urlopen)rM   NN)NrK   N)N)T)	r‰   rŠ   r‹   rŒ   rR   rh   r˜   r|   Ú__classcell__rH   rH   )r’   rI   r   `  s    
c             K   s   t f d| i|—ŽS )Nr‘   )r   )rn   rb   rH   rH   rI   r   ·  s    )&Ú
__future__r   ÚcollectionsÚ	functoolsÚloggingÚ_collectionsr   Úconnectionpoolr   r   r   Ú
exceptionsr   r	   r
   Zpackages.six.moves.urllib.parser   Úrequestr   Zutil.urlr   Z
util.retryr   Ú__all__Ú	getLoggerr‰   r„   r`   Ú_key_fieldsÚ
namedtupler0   rJ   ÚpartialrV   rU   r   r   r   rH   rH   rH   rI   Ú<module>   s2   

6
 hW