3

Z'ícJMã@s8ddlmZddlZddlZddlZddlmZddlmZm	Z	m
Z
ddlmZm
Z
mZmZmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZd
ddgZejeƒZ d<Z!d=Z"ej#d3e"ƒZ$d>Z%ej#d5e%ƒZ&d6d7„Z'ej(e'e$ƒej(e'e$ƒd8œZ)ee	d8œZ*Gd9d
„d
eƒZ+Gd:d„de+ƒZ,d;d„Z-dS)?é)Úabsolute_importNé)ÚRecentlyUsedContainer)ÚHTTPConnectionPoolÚHTTPSConnectionPoolÚport_by_scheme)ÚLocationValueErrorÚ
MaxRetryErrorÚProxySchemeUnknownÚProxySchemeUnsupportedÚURLSchemeUnknown)Úsix)Úurljoin)ÚRequestMethods)Úconnection_requires_http_tunnel)ÚRetry)Ú	parse_urlÚPoolManagerÚProxyManagerÚproxy_from_urlÚkey_fileÚ	cert_fileÚ	cert_reqsÚca_certsÚssl_versionÚca_cert_dirÚssl_contextÚkey_passwordÚserver_hostnameÚ
key_schemeÚkey_hostÚkey_portÚkey_timeoutÚkey_retriesÚ
key_strictÚ	key_blockÚkey_source_addressÚkey_key_fileÚkey_key_passwordÚ
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__proxy_configÚkey_socket_optionsÚkey__socks_optionsÚkey_assert_hostnameÚkey_assert_fingerprintÚkey_server_hostnameÚPoolKeyÚuse_forwarding_for_httpsÚProxyConfigcCsÎ|jƒ}|djƒ|d<|djƒ|d<x4d	D],}||kr.||dk	r.t||jƒƒ||<q.W|jdƒ}|dk	r|t|ƒ|d<x&t|jƒƒD]}|j|ƒ|d|<qŠWx|j	D]}||kr¬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_)r>r?r@)
ÚcopyÚlowerÚ	frozensetÚitemsÚgetÚtupleÚlistÚkeysÚpopÚ_fields)Ú	key_classÚrequest_contextÚcontextÚkeyÚsocket_optsÚfield©rSú6/tmp/pip-build-gk9425m9/urllib3/urllib3/poolmanager.pyÚ_default_key_normalizerOs

rU)ÚhttpÚhttpsc@sŒeZdZdZdZdZd dd„Zdd„Zdd	„Zd!d
d„Z	dd
„Z
d"dd„Zdd„Zd#dd„Z
d$dd„Zdd„Zdd„Zdd„Zd%dd„ZdS)&ra$
    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

cKs8tj||ƒ||_t|dd„d|_t|_tjƒ|_dS)NcSs|jƒS)N)Úclose)ÚprSrSrTÚ<lambda>®sz&PoolManager.__init__.<locals>.<lambda>)Údispose_func)rÚ__init__Úconnection_pool_kwrÚpoolsÚpool_classes_by_schemeÚkey_fn_by_schemerC)ÚselfÚ	num_poolsr>r^rSrSrTr]«s
zPoolManager.__init__cCs|S)NrS)rbrSrSrTÚ	__enter__µszPoolManager.__enter__cCs|jƒdS)NF)Úclear)rbÚexc_typeÚexc_valÚexc_tbrSrSrTÚ__exit__¸szPoolManager.__exit__cCsf|j|}|dkr|jjƒ}xdD]}|j|dƒq"W|dkrXxtD]}|j|dƒqDW|||f|Ž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.
        Nr<r=ÚportrV)r<r=rj)r`r^rCrKÚSSL_KEYWORDS)rbr<r=rjrNÚpool_clsrPÚkwrSrSrTÚ	_new_pool½s




zPoolManager._new_poolcCs|jjƒdS)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_re)rbrSrSrTreØszPoolManager.clearrVcCsT|stdƒ‚|j|ƒ}|pd|d<|s:tj|djƒdƒ}||d<||d<|j|ƒ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.
        zNo host specified.rVr<éPrjr=)rÚ_merge_pool_kwargsrrGrDÚconnection_from_context)rbr=rjr<Úpool_kwargsrNrSrSrTÚconnection_from_hostás
z PoolManager.connection_from_hostcCs:|djƒ}|jj|ƒ}|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.
        r<)rN)rDrarGrÚconnection_from_pool_key)rbrNr<Úpool_key_constructorÚpool_keyrSrSrTrqøsz#PoolManager.connection_from_contextc
Cs`|jjN|jj|ƒ}|r|S|d}|d}|d}|j||||d}||j|<WdQRX|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.
        r<r=rj)rNN)r_ÚlockrGrn)rbrvrNÚpoolr<r=rjrSrSrTrts
z$PoolManager.connection_from_pool_keycCs 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.
        )rjr<rr)rrsr=rjr<)rbÚurlrrÚurSrSrTÚconnection_from_urlszPoolManager.connection_from_urlcCsZ|jjƒ}|rVxF|jƒD]:\}}|dkrJy
||=WqRtk
rFYqRXq|||<qW|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)r^rCrFÚKeyError)rbÚoverrideÚbase_pool_kwargsrPÚvaluerSrSrTrp/s

zPoolManager._merge_pool_kwargscCs"|jdkrdSt|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)ÚproxyrÚproxy_configr<)rbÚ
parsed_urlrSrSrTÚ!_proxy_requires_url_absolute_formCs
z-PoolManager._proxy_requires_url_absolute_formcCsB|jdks|dkrdS|jjdkr&dStjr>|jjr>tdƒ‚dS)z
        Validates that were not attempting to do TLS in TLS connections on
        Python2 or with unsupported SSL implementations.
        NrWzfContacting HTTPS destinations through HTTPS proxies 'via CONNECT tunnels' is not supported in Python 2)r€r<r
ÚPY2rr:r)rbZ
url_schemerSrSrTÚ$_validate_proxy_scheme_url_selectionPsz0PoolManager._validate_proxy_scheme_url_selectionTcKs”t|ƒ}|j|jƒ|j|j|j|jd}d|d<d|d<d|krP|jjƒ|d<|j|ƒrl|j	||f|Ž}n|j	||j
f|Ž}|oˆ|jƒ}|s’|St||ƒ}|j
dkrªd}|jdƒ}	t|	tƒsÌtj|	|d	}	|	joÜ|j|ƒrttj|dƒƒ}
x*|
D]"}|jƒ|	jkrø|dj|d
ƒqøWy|	j||||d}	Wn(tk
r\|	jrX|jƒ‚|SX|	|d<||d<tjd||ƒ|jƒ|j	||f|Ž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.
        )rjr<FÚassert_same_hostÚredirectr>i/ÚGETÚretries)r‡N)ÚresponseÚ_poolzRedirecting %s -> %s)rr…r<rsr=rjr>rCrƒÚurlopenÚrequest_uriÚget_redirect_locationrÚstatusrGÚ
isinstancerÚfrom_intÚremove_headers_on_redirectÚis_same_hostrIr
ÚiterkeysrDrKÚ	incrementr	Úraise_on_redirectZ
drain_connÚlogÚinfo)rbÚmethodryr‡rmrzÚconnrŠÚredirect_locationr‰r>ÚheaderrSrSrTrŒasJ	







zPoolManager.urlopen)rXN)N)NrVN)N)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r€rr]rdrirnrersrqrtr{rprƒr…rŒrSrSrSrTrŒs 


	



csHeZdZdZd‡fdd„	Zd‡fdd	„	Zdd
d„Zd‡fd
d„	Z‡ZS)ra>
    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.

    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

    rXNFc
s´t|tƒrd|j|j|jf}t|ƒ}|jd	kr:t|jƒ‚|jsZtj|jdƒ}	|j	|	d}||_
|pfi|_||_t
||ƒ|_|j
|d<|j|d<|j|d<tt|ƒj||f|ŽdS)
Nz
%s://%s:%irVrWro)rjÚ_proxyr?Z
_proxy_config)rVrW)rrr<r=rjrr
rrGÚ_replacer€Ú
proxy_headersÚproxy_ssl_contextr;rÚsuperrr])
rbÚ	proxy_urlrcr>r£r¤r:r^r€rj)Ú	__class__rSrTr]Ís&







zProxyManager.__init__rVcsD|dkr tt|ƒj||||dStt|ƒj|jj|jj|jj|dS)NrW)rr)r¥rrsr€r=rjr<)rbr=rjr<rr)r§rSrTrsòs


z!ProxyManager.connection_from_hostcCs0ddi}t|ƒj}|r||d<|r,|j|ƒ|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)rbryr>Úheaders_rªrSrSrTÚ_set_proxy_headersüs

zProxyManager._set_proxy_headersTcsVt|ƒ}t|j|j|jƒs8|jd|jƒ}|j||ƒ|d<tt	|ƒj
||fd|i|—ŽS)z@Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute.r>r‡)rrr€rr<rGr>r­r¥rrŒ)rbr™ryr‡rmrzr>)r§rSrTrŒs
zProxyManager.urlopen)rXNNNF)NrVN)N)T)	rržrŸr r]rsr­rŒÚ
__classcell__rSrS)r§rTr¥s&

cKstfd|i|—ŽS)Nr¦)r)ryrmrSrSrTrs)	rrrrrrrrr)rr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8)rr:).Ú
__future__rÚcollectionsÚ	functoolsÚloggingÚ_collectionsrÚconnectionpoolrrrÚ
exceptionsrr	r
rrÚpackagesr
Zpackages.six.moves.urllib.parserÚrequestrZ
util.proxyrZ
util.retryrZutil.urlrÚ__all__Ú	getLoggerrr—rkÚ_key_fieldsÚ
namedtupler9Z_proxy_config_fieldsr;rUÚpartialrar`rrrrSrSrSrTÚ<module>sz

6

s