a Y Q@sdZddlZddlZddlmZddlmZmZddl m Z ddl m Z ddlmZdd lmZmZdd lmZmZmZmZmZmZdd lmZdd lmZdd lmZddlmZ ddlm!Z!ddlm"Z"ddlm#Z$ddlm%Z%ddlm&Z&ddlm'Z(ddlm)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/m'Z'm#Z#m0Z0m1Z1ddl2m3Z3zddl4m5Z5Wne6ypddZ5Yn0dZ7dZ8dZ9dZ:Gddde;Z` 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) ` 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. NNotImplementedError)r.requeststreamtimeoutverifycertproxiesr&r&r'send9szBaseAdapter.sendcCstdS)z!Cleans up adapter specific items.Nr1r-r&r&r'closeKszBaseAdapter.close)FNTNN)__name__ __module__ __qualname____doc__r,r9r: __classcell__r&r&r/r'r)3s   r)cseZdZdZgdZeeeeffdd ZddZ ddZ efd d Z d d Z d dZ ddZd ddZddZddZddZddZd!ddZZS)" HTTPAdapteraThe 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 ` 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_retriesconfig_pool_connections _pool_maxsize _pool_blockcsd|tkrtddd|_n t||_i|_i|_tt|||_ ||_ ||_ |j |||ddS)NrF)readblock) DEFAULT_RETRIESrrAfrom_intrB proxy_managerr+r@r,rCrDrEinit_poolmanager)r.Zpool_connectionsZ pool_maxsizerAZ pool_blockr/r&r'r,ms zHTTPAdapter.__init__cstfddjDS)Nc3s|]}|t|dfVqdSr*)getattr).0attrr-r&r' z+HTTPAdapter.__getstate__..)dict __attrs__r-r&r-r' __getstate__s zHTTPAdapter.__getstate__cCsDi|_i|_|D]\}}t|||q|j|j|j|jddS)NrG)rKrBitemssetattrrLrCrDrE)r.staterOvaluer&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 `. :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_poolsmaxsizerHstrictN)rCrDrEr poolmanager)r.Z connectionsr[rH pool_kwargsr&r&r'rLs zHTTPAdapter.init_poolmanagercKs||jvr|j|}n||dr^t|\}}t|f|||j|j|jd|}|j|<n4||}t |f||j|j|jd|}|j|<|S)aReturn 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 `. :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: requests.packages.urllib3.ProxyManager socks)usernamepasswordrZr[rH) proxy_headersrZr[rH) rKlower startswithrr#rCrDrErbr)r.proxyZ proxy_kwargsmanagerr`rarbr&r&r'proxy_manager_fors6    zHTTPAdapter.proxy_manager_forcCs|drj|rjd}|dur"|}|s*t}|r:tj|sHtd|d|_tj |sb||_ q|||_ nd|_d|_ d|_ |rt |t s|d|_|d|_n||_|jrtj|jstd |j|jrtj|jstd |jdS) aAVerify a SSL certificate. This method should not be called from user code, and is only exposed for use when subclassing the :class:`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. httpsNTzFCould not find a suitable TLS CA certificate bundle, invalid path: {0} CERT_REQUIRED CERT_NONErrz:Could not find the TLS certificate file, invalid path: {0}z2Could not find the TLS key file, invalid path: {0})rcrdr ospathexistsIOErrorformat cert_reqsisdirca_certs ca_cert_dir isinstancer cert_filekey_file)r.connurlr6r7Zcert_locr&r&r' cert_verifys<     zHTTPAdapter.cert_verifycCst}t|dd|_tt|di|_t|j|_||_|jj|_t |j t r^|j d|_ n|j |_ t |j||||_||_|S)aBuilds a :class:`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 ` :param req: The :class:`PreparedRequest ` used to generate the response. :param resp: The urllib3 response object. :rtype: requests.Response statusNheadersutf-8)rrM status_coderr{r encodingrawreasonrtrxbytesdecodercookiesr3 connection)r.reqrespresponser&r&r'build_responses    zHTTPAdapter.build_responseNcCsNt||}|r.t|d}||}||}nt|}|}|j|}|S)aReturns 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 `. :param url: The URL to connect to. :param proxies: (optional) A Requests-style dictionary of proxies used on this request. :rtype: requests.packages.urllib3.ConnectionPool http)rr rgconnection_from_urlr geturlr])r.rxr8rerKrwparsedr&r&r'get_connection s     zHTTPAdapter.get_connectioncCs&|j|jD] }|qdS)zDisposes of any internal state. Currently, this closes the PoolManager and any active ProxyManager, which closes any pooled connections. N)r]clearrKvalues)r.rer&r&r'r:7s zHTTPAdapter.closec Cs`t|j|}t|jj}|o"|dk}d}|rDt|j}|d}|j}|r\|s\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 `. :param request: The :class:`PreparedRequest ` being sent. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. :rtype: str rhFr_)rrxr schemercrdpath_urlr) r.r3r8rerZis_proxied_http_requestZusing_socks_proxy proxy_schemerxr&r&r' request_urlAs     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 `. This should not be called from user code, and is only exposed for use when subclassing the :class:`HTTPAdapter `. :param request: The :class:`PreparedRequest ` to add headers to. :param kwargs: The keyword arguments from the call to send(). Nr&)r.r3r%r&r&r' add_headers^s zHTTPAdapter.add_headerscCs&i}t|\}}|r"t|||d<|S)a Returns 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 `. :param proxies: The url of the proxy being used for this request. :rtype: dict zProxy-Authorization)rr")r.rer{r`rar&r&r'rbls  zHTTPAdapter.proxy_headersFTc Cs||j|}|||j|||||}|||jdupHd|jv } t|trz|\} } t | | d}Wqt y} zd |} t | WYd} ~ qd} ~ 00nt|t rn t ||d}zB| s|j |j ||j|jdddd|j|d }nt|dr|j}|jtd}z|j|j |d d |jD]\}}|||q.||jD]D}|tt|d dd |d |||d qT|dz|jd d}Wnty|}Yn0tj|||ddd}Wn|Yn0Wnt t!j"fy>} zt#| |dWYd} ~ nPd} ~ 0t$y} ztt| j%t&rvt| j%t'svt(| |dt| j%t)rt*| |dt| j%t+rt,| |dt#| |dWYd} ~ nd} ~ 0t-y} zt#| |dWYd} ~ nd} ~ 0t+y&} zt,| WYd} ~ nhd} ~ 0t.t/fy} zBt| t.rTt0| |dnt| t1rnt2| |dnWYd} ~ n d} ~ 00|3||S)aSends PreparedRequest object. Returns Response object. :param request: The :class:`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) ` 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 NzContent-Length)connectrFzsInvalid timeout {0}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valueF) methodrxbodyr{redirectassert_same_hostpreload_contentdecode_contentretriesr5 proxy_pool)r5T)skip_accept_encodingr|s s0 ) buffering)poolrrr)r3)4rrxryrrrr{rttuple TimeoutSauce ValueErrorrourlopenrrAhasattrr _get_connDEFAULT_POOL_TIMEOUT putrequestrU putheader endheadersr9hexlenencode getresponse TypeErrorr from_httplibr:rsocketerrorrrrrrrrr _ProxyErrorrr _SSLError _HTTPErrorrrrr)r.r3r4r5r6r7r8rwrxchunkedrrFeerrrZlow_connheaderrXirr&r&r'r9s               "       zHTTPAdapter.send)N)FNTNN)r;r<r=r>rSDEFAULT_POOLSIZErIDEFAULT_POOLBLOCKr,rTrYrLrgryrrr:rrrbr9r?r&r&r/r'r@Ps$ %3%  r@)>r>Zos.pathrkrmodelsrZpackages.urllib3.poolmanagerrrZpackages.urllib3.responserZpackages.urllib3.utilrrZpackages.urllib3.util.retryrcompatr r utilsr r r rrr structuresrpackages.urllib3.exceptionsrrrrrrrrrrrrrrr exceptionsrrrr r!authr"Zpackages.urllib3.contrib.socksr# ImportErrorrrrIrobjectr)r@r&r&r&r'sB                 $