3

V'ícTã@sÜdZddlZddlZddlmZmZddlmZddl	m
Z
ddl	mZddl
mZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl"m#Z#m$Z$ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,ddl-m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4mZmZm5Z5m6Z6m7Z7m8Z8mZddl9m:Z:yddl;m<Z<Wne=k
r¦dd„Z<YnXdZ>d Z?dZ@dZAGd!d"„d"eBƒZCGd#d$„d$eCƒZDdS)%z‰
requests.adapters
~~~~~~~~~~~~~~~~~

This module contains the transport adapters that Requests uses to define
and maintain connections.
éN)ÚPoolManagerÚproxy_from_url)ÚHTTPResponse)Ú	parse_url)ÚTimeout)ÚRetry)ÚClosedPoolError)ÚConnectTimeoutError)Ú	HTTPError)Ú
InvalidHeader)Ú
MaxRetryError)ÚNewConnectionError)Ú
ProxyError)Ú
ProtocolError)ÚReadTimeoutError)ÚSSLError)Ú
ResponseError)ÚLocationValueErroré)ÚResponse)ÚurlparseÚ
basestring)ÚDEFAULT_CA_BUNDLE_PATHÚextract_zipped_pathsÚget_encoding_from_headersÚprepend_scheme_if_neededÚget_auth_from_urlÚ
urldefragauthÚselect_proxy)ÚCaseInsensitiveDict)Úextract_cookies_to_jar)
ÚConnectionErrorÚConnectTimeoutÚReadTimeoutrrÚ
RetryErrorÚ
InvalidSchemaÚInvalidProxyURLÚ
InvalidURLr)Ú_basic_auth_str)ÚSOCKSProxyManagercOstdƒ‚dS)Nz'Missing dependencies for SOCKS support.)r%)ÚargsÚkwargs©r,ú5/tmp/pip-build-gk9425m9/requests/requests/adapters.pyr)/sr)Fé
cs2eZdZdZ‡fdd„Zddd„Zd	d
„Z‡ZS)ÚBaseAdapterzThe Base Transport Adaptercstt|ƒjƒdS)N)Úsuperr/Ú__init__)Úself)Ú	__class__r,r-r1;szBaseAdapter.__init__FNTcCst‚dS)aCSends PreparedRequest object. Returns Response object.

        :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
        :param stream: (optional) Whether to stream the request content.
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use
        :param cert: (optional) Any user-provided SSL certificate to be trusted.
        :param proxies: (optional) The proxies dictionary to apply to the request.
        N)ÚNotImplementedError)r2ÚrequestÚstreamÚtimeoutÚverifyÚcertÚproxiesr,r,r-Úsend>szBaseAdapter.sendcCst‚dS)z!Cleans up adapter specific items.N)r4)r2r,r,r-ÚclosePszBaseAdapter.close)FNTNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1r;r<Ú
__classcell__r,r,)r3r-r/8s

r/cs eZdZdZdddddgZeeeef‡fdd„	Zd	d
„Z	dd„Z
efd
d„Zdd„Zdd„Z
dd„Zd$dd„Zdd„Zdd„Zdd„Zdd„Zd%d"d#„Z‡ZS)&ÚHTTPAdapteraThe built-in HTTP Adapter for urllib3.

    Provides a general-case interface for Requests sessions to contact HTTP and
    HTTPS urls by implementing the Transport Adapter interface. This class will
    usually be created by the :class:`Session <Session>` class under the
    covers.

    :param pool_connections: The number of urllib3 connection pools to cache.
    :param pool_maxsize: The maximum number of connections to save in the pool.
    :param max_retries: The maximum number of retries each connection
        should attempt. Note, this applies only to failed DNS lookups, socket
        connections and connection timeouts, never to requests where data has
        made it to the server. By default, Requests does not retry failed
        connections. If you need granular control over the conditions under
        which we retry a request, import urllib3's ``Retry`` class and pass
        that instead.
    :param pool_block: Whether the connection pool should block for connections.

    Usage::

      >>> import requests
      >>> s = requests.Session()
      >>> a = requests.adapters.HTTPAdapter(max_retries=3)
      >>> s.mount('http://', a)
    Úmax_retriesÚconfigÚ_pool_connectionsÚ
_pool_maxsizeÚ_pool_blockcsd|tkrtddd|_ntj|ƒ|_i|_i|_tt|ƒjƒ||_	||_
||_|j|||ddS)NrF)Úread)Úblock)
ÚDEFAULT_RETRIESrrCÚfrom_intrDÚ
proxy_managerr0rBr1rErFrGÚinit_poolmanager)r2Úpool_connectionsÚpool_maxsizerCÚ
pool_block)r3r,r-r1rszHTTPAdapter.__init__cs‡fdd„ˆjDƒS)Ncsi|]}tˆ|dƒ|“qS)N)Úgetattr)Ú.0Úattr)r2r,r-ú
<dictcomp>…sz,HTTPAdapter.__getstate__.<locals>.<dictcomp>)Ú	__attrs__)r2r,)r2r-Ú__getstate__„szHTTPAdapter.__getstate__cCsHi|_i|_x |jƒD]\}}t|||ƒqW|j|j|j|jddS)N)rI)rLrDÚitemsÚsetattrrMrErFrG)r2ÚstaterSÚvaluer,r,r-Ú__setstate__‡szHTTPAdapter.__setstate__cKs0||_||_||_tf|||ddœ|—Ž|_dS)aInitializes a urllib3 PoolManager.

        This method should not be called from user code, and is only
        exposed for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param connections: The number of urllib3 connection pools to cache.
        :param maxsize: The maximum number of connections to save in the pool.
        :param block: Block when no free connections are available.
        :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager.
        T)Ú	num_poolsÚmaxsizerIÚstrictN)rErFrGrÚpoolmanager)r2Úconnectionsr]rIÚpool_kwargsr,r,r-rM“s

zHTTPAdapter.init_poolmanagercKs–||jkr|j|}n||jƒjdƒr^t|ƒ\}}t|f|||j|j|jdœ|—Ž}|j|<n4|j|ƒ}t	|f||j|j|jdœ|—Ž}|j|<|S)aÏReturn urllib3 ProxyManager for the given proxy.

        This method should not be called from user code, and is only
        exposed for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param proxy: The proxy to return a urllib3 ProxyManager for.
        :param proxy_kwargs: Extra keyword arguments used to configure the Proxy Manager.
        :returns: ProxyManager
        :rtype: urllib3.ProxyManager
        Úsocks)ÚusernameÚpasswordr\r]rI)Ú
proxy_headersr\r]rI)
rLÚlowerÚ
startswithrr)rErFrGrer)r2ÚproxyÚproxy_kwargsÚmanagerrcrdrer,r,r-Úproxy_manager_for§s*

zHTTPAdapter.proxy_manager_forcCs|jƒjdƒrr|rrd}|dk	r"|}|s.ttƒ}|sBtjj|ƒrPtdj|ƒƒ‚d|_	tjj
|ƒsj||_q„||_nd|_	d|_d|_|rt
|tƒsª|d|_|d|_n||_d|_|jrÜtjj|jƒrÜtd	j|jƒƒ‚|joðtjj|jƒrtd
j|jƒƒ‚dS)aAVerify a SSL certificate. This method should not be called from user
        code, and is only exposed for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param conn: The urllib3 connection object associated with the cert.
        :param url: The requested URL.
        :param verify: Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use
        :param cert: The SSL certificate to verify.
        ÚhttpsNTzECould not find a suitable TLS CA certificate bundle, invalid path: {}Ú
CERT_REQUIREDÚ	CERT_NONErrz9Could not find the TLS certificate file, invalid path: {}z1Could not find the TLS key file, invalid path: {})rfrgrrÚosÚpathÚexistsÚIOErrorÚformatÚ	cert_reqsÚisdirÚca_certsÚca_cert_dirÚ
isinstancerÚ	cert_fileÚkey_file)r2ÚconnÚurlr8r9Úcert_locr,r,r-Úcert_verifyÌs8


zHTTPAdapter.cert_verifycCs„tƒ}t|ddƒ|_tt|diƒƒ|_t|jƒ|_||_|jj|_t	|j
tƒr^|j
jdƒ|_
n|j
|_
t
|j||ƒ||_||_|S)a¶Builds a :class:`Response <requests.Response>` object from a urllib3
        response. This should not be called from user code, and is only exposed
        for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`

        :param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
        :param resp: The urllib3 response object.
        :rtype: requests.Response
        ÚstatusNÚheaderszutf-8)rrQÚstatus_coderr€rÚencodingÚrawÚreasonrxr|ÚbytesÚdecoder Úcookiesr5Ú
connection)r2ÚreqÚrespÚresponser,r,r-Úbuild_responses

zHTTPAdapter.build_responseNcCsdt||ƒ}|rDt|dƒ}t|ƒ}|js.tdƒ‚|j|ƒ}|j|ƒ}nt|ƒ}|jƒ}|j	j|ƒ}|S)aŒReturns a urllib3 connection for the given URL. This should not be
        called from user code, and is only exposed for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param url: The URL to connect to.
        :param proxies: (optional) A Requests-style dictionary of proxies used on this request.
        :rtype: urllib3.ConnectionPool
        ÚhttpzFPlease check proxy URL. It is malformed and could be missing the host.)
rrrÚhostr&rkÚconnection_from_urlrÚgeturlr_)r2r|r:rhÚ	proxy_urlrLr{Úparsedr,r,r-Úget_connection%s	


zHTTPAdapter.get_connectioncCs*|jjƒx|jjƒD]}|jƒqWdS)z¢Disposes of any internal state.

        Currently, this closes the PoolManager and any active ProxyManager,
        which closes any pooled connections.
        N)r_ÚclearrLÚvalues)r2rhr,r,r-r<@s
zHTTPAdapter.closec	Csbt|j|ƒ}t|jƒj}|o"|dk}d}|rDt|ƒjjƒ}|jdƒ}|j}|r^|r^t|jƒ}|S)a?Obtain the url to use when making the final request.

        If the message is being sent through a HTTP proxy, the full URL has to
        be used. Otherwise, we should only use the path portion of the URL.

        This should not be called from user code, and is only exposed for use
        when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
        :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs.
        :rtype: str
        rlFrb)rr|rÚschemerfrgÚpath_urlr)	r2r5r:rhr–Úis_proxied_http_requestÚusing_socks_proxyÚproxy_schemer|r,r,r-Úrequest_urlJs


zHTTPAdapter.request_urlcKsdS)a"Add any headers needed by the connection. As of v2.0 this does
        nothing by default, but is left for overriding by users that subclass
        the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        This should not be called from user code, and is only exposed for use
        when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
        :param kwargs: The keyword arguments from the call to send().
        Nr,)r2r5r+r,r,r-Úadd_headersgszHTTPAdapter.add_headerscCs&i}t|ƒ\}}|r"t||ƒ|d<|S)aReturns a dictionary of the headers to add to any request sent
        through a proxy. This works with urllib3 magic to ensure that they are
        correctly sent to the proxy, rather than in a tunnelled request if
        CONNECT is being used.

        This should not be called from user code, and is only exposed for use
        when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param proxy: The url of the proxy being used for this request.
        :rtype: dict
        zProxy-Authorization)rr()r2rhr€rcrdr,r,r-reus

zHTTPAdapter.proxy_headersFTc7Csy|j|j|ƒ}Wn.tk
r@}zt||d‚WYdd}~XnX|j||j||ƒ|j||ƒ}	|j||||||d|jdkpˆd|jk}
t	|t
ƒræy|\}}t||d}Wqþtk
râ}zdj
|ƒ}
t|
ƒ‚WYdd}~XqþXnt	|tƒrònt||d}yZ|
s2|j|j|	|j|jdddd|j|d
}n$t|d	ƒrD|j}|jtd
}yîd|jk}|j|j|	d|d
x$|jjƒD]\}}|j||ƒq|W|jƒxN|jD]D}|jtt|ƒƒdd…jdƒƒ|jdƒ|j|ƒ|jdƒq¦W|jdƒy|jdd}Wntk
r&|jƒ}YnXtj |||ddd}Wn|j!ƒ‚YnXWnªt"t#j$fk
r’}
zt%|
|d‚WYdd}
~
Xntt&k
r6}zˆt	|j't(ƒrÌt	|j't)ƒsÌt*||d‚t	|j't+ƒræt,||d‚t	|j't-ƒrt.||d‚t	|j't/ƒrt0||d‚t%||d‚WYdd}~XnÐt1k
rd}zt%||d‚WYdd}~Xn¢t-k
rŽ}zt.|ƒ‚WYdd}~Xnxt/t2fk
r}zVt	|t/ƒr¾t0||d‚n6t	|t3ƒrØt4||d‚nt	|t5ƒròt6||d‚n‚WYdd}~XnX|j7||ƒS)aSends PreparedRequest object. Returns Response object.

        :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
        :param stream: (optional) Whether to stream the request content.
        :param timeout: (optional) How long to wait for the server to send
            data before giving up, as a float, or a :ref:`(connect timeout,
            read timeout) <timeouts>` tuple.
        :type timeout: float or tuple or urllib3 Timeout object
        :param verify: (optional) Either a boolean, in which case it controls whether
            we verify the server's TLS certificate, or a string, in which case it
            must be a path to a CA bundle to use
        :param cert: (optional) Any user-provided SSL certificate to be trusted.
        :param proxies: (optional) The proxies dictionary to apply to the request.
        :rtype: requests.Response
        )r5N)r6r7r8r9r:zContent-Length)ÚconnectrHzrInvalid timeout {}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valueF)
Úmethodr|Úbodyr€ÚredirectÚassert_same_hostÚpreload_contentÚdecode_contentÚretriesr7Ú
proxy_pool)r7ÚHostT)Úskip_accept_encodingÚ	skip_hostézutf-8s
s0

)Ú	buffering)Úpoolrˆr¢r£)8r“r|rr'r~r›rœrŸr€rxÚtupleÚTimeoutSauceÚ
ValueErrorrsÚurlopenržrCÚhasattrr¥Ú	_get_connÚDEFAULT_POOL_TIMEOUTÚ
putrequestrWÚ	putheaderÚ
endheadersr;ÚhexÚlenÚencodeÚgetresponseÚ	TypeErrorrÚfrom_httplibr<rÚsocketÚerrorr!rr„r	r
r"rr$Ú_ProxyErrorrÚ	_SSLErrorrrÚ
_HTTPErrorrr#Ú_InvalidHeaderrrŒ)r2r5r6r7r8r9r:r{Úer|ÚchunkedrrHÚerrrŠÚlow_connr¨ÚheaderrZÚiÚrr,r,r-r;‹s²


 


zHTTPAdapter.send)N)FNTNN)r=r>r?r@rUÚDEFAULT_POOLSIZErJÚDEFAULT_POOLBLOCKr1rVr[rMrkr~rŒr“r<r›rœrer;rAr,r,)r3r-rBUs$%4%

rB)Er@Úos.pathror¼Zurllib3.poolmanagerrrZurllib3.responserZurllib3.utilrrr­Zurllib3.util.retryrZurllib3.exceptionsrr	r
rÀrrÁrr
rr¾rrrr¿rrÚmodelsrÚcompatrrÚutilsrrrrrrrÚ
structuresrr‡r Ú
exceptionsr!r"r#r$r%r&r'Úauthr(Zurllib3.contrib.socksr)ÚImportErrorrÊrÉrJr²Úobjectr/rBr,r,r,r-Ú<module>	sH$0