3 PfR @sdZddlZddlZddlmZmZddlmZddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZm Z ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/mZmZm0Z0m1Z1ddl2m3Z3yddl4m5Z5Wne6k rrddZ5YnXdZ7dZ8dZ9dZ: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. N)NotImplementedError)r,requeststreamtimeoutverifycertproxiesr&r&r'send:szBaseAdapter.sendcCstdS)z!Cleans up adapter specific items.N)r.)r,r&r&r'closeLszBaseAdapter.close)FNTNN)__name__ __module__ __qualname____doc__r+r5r6 __classcell__r&r&)r-r'r)4s   r)cseZdZdZdddddgZeeeeffdd Zd d Z d d Z efd dZ ddZ ddZ ddZd$ddZddZddZddZddZd%d"d#ZZS)& 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 tj||_i|_i|_tt|j||_ ||_ ||_ |j |||ddS)NrF)read)block) DEFAULT_RETRIESrr=Zfrom_intr> proxy_managerr*r<r+r?r@rAinit_poolmanager)r,Zpool_connectionsZ pool_maxsizer=Z pool_block)r-r&r'r+ns zHTTPAdapter.__init__cstfddjDS)Nc3s|]}|t|dfVqdS)N)getattr).0attr)r,r&r' sz+HTTPAdapter.__getstate__..)dict __attrs__)r,r&)r,r' __getstate__s zHTTPAdapter.__getstate__cCsHi|_i|_x |jD]\}}t|||qW|j|j|j|jddS)N)rC)rEr>itemssetattrrFr?r@rA)r,staterIvaluer&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_poolsmaxsizerCstrictN)r?r@rAr poolmanager)r,Z connectionsrTrCZ pool_kwargsr&r&r'rFs zHTTPAdapter.init_poolmanagercKs||jkr|j|}n||jjdr^t|\}}t|f|||j|j|jd|}|j|<n4|j|}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: urllib3.ProxyManager socks)usernamepasswordrSrTrC) proxy_headersrSrTrC) rElower startswithrr#r?r@rArZr)r,proxyZ proxy_kwargsZmanagerrXrYrZr&r&r'proxy_manager_fors*    zHTTPAdapter.proxy_manager_forcCs|jjdrn|rnd}|dk r"|}|s*t}| s>tjj| rLtdj|d|_tjj |sf||_ q||_ nd|_d|_ d|_ |rt |t s|d|_|d|_n ||_d|_|jrtjj|j rtd j|j|jotjj|j rtd j|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}Z CERT_REQUIREDZ CERT_NONErrz:Could not find the TLS certificate file, invalid path: {0}z2Could not find the TLS key file, invalid path: {0})r[r\rospathexistsIOErrorformatZ cert_reqsisdirZca_certsZ ca_cert_dir isinstancerZ cert_fileZkey_file)r,connurlr2r3Zcert_locr&r&r' cert_verifys8      zHTTPAdapter.cert_verifycCst}t|dd|_tt|di|_t|j|_||_|jj|_t |j t r^|j 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 ZstatusNheaderszutf-8)rrGZ status_coderrjrencodingrawreasonrfrhbytesdecodercookiesr/ connection)r,ZreqrespZresponser&r&r'build_responses    zHTTPAdapter.build_responseNcCsNt||}|r.t|d}|j|}|j|}nt|}|j}|jj|}|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: urllib3.ConnectionPool Zhttp)rrr^Zconnection_from_urlrZgeturlrV)r,rhr4r]rErgZparsedr&r&r'get_connection"s     zHTTPAdapter.get_connectioncCs*|jjx|jjD] }|jqWdS)zDisposes of any internal state. Currently, this closes the PoolManager and any active ProxyManager, which closes any pooled connections. N)rVclearrEvalues)r,r]r&r&r'r69s zHTTPAdapter.closec Csbt|j|}t|jj}|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 `. :param request: The :class:`PreparedRequest ` being sent. :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs. :rtype: str r_FrW)rrhrschemer[r\Zpath_urlr) r,r/r4r]rwZis_proxied_http_requestZusing_socks_proxyZ proxy_schemerhr&r&r' request_urlCs      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,r/r%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,r]rjrXrYr&r&r'rZns   zHTTPAdapter.proxy_headersFTc)Cs|j|j|}|j||j|||j||}|j||jdkpHd|jk } t|try|\} } t | | d}Wqt k r} zdj |} t | WYdd} ~ XqXnt|t rn t ||d}yL| s|j |j ||j|jdddd|j|d }nt|dr|j}|jtd}y|j|j |d d x$|jjD]\}}|j||q.W|jxN|jD]D}|jtt|d djd |jd |j||jd qXW|jdy|jd d}Wntk r|j}YnXtj|||ddd}Wn|jYnXWnt t!j"fk rD} zt#| |dWYdd} ~ XnZt$k r} zt| j%t&r~t| j%t's~t(| |dt| j%t)rt*| |dt| j%t+rt,| |dt| j%t-rt.| |dt#| |dWYdd} ~ Xnt/k r} zt#| |dWYdd} ~ Xnt+k r@} zt,| WYdd} ~ Xn^t-t0fk r} 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 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)connectrBzsInvalid timeout {0}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valueF) methodrhbodyrjZredirectZassert_same_hostpreload_contentdecode_contentZretriesr1 proxy_pool)r1T)Zskip_accept_encodingzutf-8s s0 ) buffering)Zpoolrqr}r~)r/)4rtrhrirxryr|rjrftuple TimeoutSauce ValueErrorrdZurlopenr{r=hasattrrZ _get_connDEFAULT_POOL_TIMEOUTZ putrequestrNZ putheaderZ endheadersr5hexlenencodeZ getresponse TypeErrorrZ from_httplibr6r socketerrorrr rmrr rrr _ProxyErrorr _SSLErrorrr _HTTPErrorrrrs)r,r/r0r1r2r3r4rgrhZchunkedrzrBeerrrrZlow_connheaderrQirr&r&r'r5s                    zHTTPAdapter.send)N)FNTNN)r7r8r9r:rLDEFAULT_POOLSIZErDDEFAULT_POOLBLOCKr+rMrRrFr^rirsrtr6rxryrZr5r;r&r&)r-r'r<Qs$ %4%  r<)>r:Zos.pathr`rZpip._vendor.urllib3.poolmanagerrrZpip._vendor.urllib3.responserZpip._vendor.urllib3.utilrrZpip._vendor.urllib3.util.retryrZpip._vendor.urllib3.exceptionsrrr rr r r rr rrrrZmodelsrcompatrrZutilsrrrrrrZ structuresrrpr exceptionsrrrr r!Zauthr"Z!pip._vendor.urllib3.contrib.socksr# ImportErrorrrrDrobjectr)r<r&r&r&r' sB                 $