U c'S@sdZddlZddlZddlmZmZddlmZddlm Z ddlm Z m Z m Z mZddlmZddlmZmZdd lmZdd lmZmZdd lmZdd lmZdd lmZddlmZddl m!Z!ddl"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m Z m*Z*m+Z+m,Z,mZm-Z-m.Z.mZddl/m0Z0ddl1m2Z2ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:zddl;mdZ?dZ@dZAGdddZBGdddeBZCdS) z requests.adapters ~~~~~~~~~~~~~~~~~ This module contains the transport adapters that Requests uses to define and maintain connections. N)ClosedPoolErrorConnectTimeoutError) HTTPError) InvalidHeader)LocationValueError MaxRetryErrorNewConnectionError ProtocolError) ProxyError)ReadTimeoutError ResponseError)SSLError) PoolManagerproxy_from_url) HTTPResponse)Timeout) parse_url)Retry)_basic_auth_str) basestringurlparse)extract_cookies_to_jar) ConnectionErrorConnectTimeoutrInvalidProxyURL InvalidSchema InvalidURLr ReadTimeout RetryErrorr )Response)CaseInsensitiveDict)DEFAULT_CA_BUNDLE_PATHextract_zipped_pathsget_auth_from_urlget_encoding_from_headersprepend_scheme_if_needed select_proxy urldefragauth)SOCKSProxyManagercOs tddS)Nz'Missing dependencies for SOCKS support.)r)argskwargsr,8/tmp/pip-target-9754fe34/lib/python/requests/adapters.pyr)=sr)F cs2eZdZdZfddZd ddZd d ZZS) BaseAdapterzThe Base Transport AdaptercstdSN)super__init__self __class__r,r-r2JszBaseAdapter.__init__FNTcCstdS)aCSends 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 :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)r4requeststreamtimeoutverifycertproxiesr,r,r-sendMszBaseAdapter.sendcCstdS)z!Cleans up adapter specific items.Nr7r3r,r,r-close`szBaseAdapter.close)FNTNN)__name__ __module__ __qualname____doc__r2r?r@ __classcell__r,r,r5r-r/Gs  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_blockcs`|tkrtddd|_n t||_i|_i|_t||_||_ ||_ |j |||ddS)NrF)readblock) DEFAULT_RETRIESrrGfrom_intrH proxy_managerr1r2rIrJrKinit_poolmanager)r4pool_connections pool_maxsizerG pool_blockr5r,r-r2s  zHTTPAdapter.__init__csfddjDS)Ncsi|]}|t|dqSr0)getattr).0attrr3r,r- sz,HTTPAdapter.__getstate__..) __attrs__r3r,r3r- __getstate__szHTTPAdapter.__getstate__cCsDi|_i|_|D]\}}t|||q|j|j|j|jddS)NrM)rQrHitemssetattrrRrIrJrK)r4staterXvaluer,r,r- __setstate__szHTTPAdapter.__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_poolsmaxsizerNstrictN)rIrJrKr poolmanager)r4 connectionsrbrN pool_kwargsr,r,r-rRszHTTPAdapter.init_poolmanagercKs||jkr|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: urllib3.ProxyManager socks)usernamepasswordrarbrN) proxy_headersrarbrN) rQlower startswithr$r)rIrJrKrjr)r4proxy proxy_kwargsmanagerrhrirjr,r,r-proxy_manager_fors6     zHTTPAdapter.proxy_manager_forcCs|drn|rnd}|dk r"|}|s.tt}|r>tj|sLtd|d|_tj |sf||_ q||_ nd|_d|_ d|_ |rt |t s|d|_|d|_n ||_d|_|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. httpsNTzCCould not find a suitable TLS CA certificate bundle, invalid path: CERT_REQUIRED CERT_NONErrz7Could not find the TLS certificate file, invalid path: z/Could not find the TLS key file, invalid path: )rkrlr#r"ospathexistsOSError cert_reqsisdirca_certs ca_cert_dir isinstancer cert_filekey_file)r4connurlr<r=cert_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)r rV status_coder!rr%encodingrawreasonr|rbytesdecodercookiesr9 connection)r4reqrespresponser,r,r-build_response(s    zHTTPAdapter.build_responseNcCsdt||}|rDt|d}t|}|js.td||}||}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: urllib3.ConnectionPool httpzFPlease check proxy URL. It is malformed and could be missing the host.) r'r&rhostrrpconnection_from_urlrgeturlrd)r4rr>rm proxy_urlrQrparsedr,r,r-get_connectionMs     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)rdclearrQvalues)r4rmr,r,r-r@js 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 rqFrg)r'rrschemerkrlpath_urlr() r4r9r>rmris_proxied_http_requestusing_socks_proxy proxy_schemerr,r,r- request_urlts     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,)r4r9r+r,r,r- add_headerss 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 `. :param proxy: The url of the proxy being used for this request. :rtype: dict zProxy-Authorization)r$r)r4rmrrhrir,r,r-rjs  zHTTPAdapter.proxy_headersFTc Csz||j|}Wn.tk r@}zt||dW5d}~XYnX|||j|||||} |j||||||d|jdkpd|jk } t |t rz|\} } t | | d}Wqt k rt d|dYqXnt |t rn t ||d}z2| s |j |j| |j|jdddd|j|d } nt|d r2|j}|jtd }zd |jk}|j|j| d |d|jD]\}}|||qh||jD]D}|tt|ddd|d|||dq|d|}tj|||ddd} Wn tk r| YnXWnt!t"fk rX}zt#||dW5d}~XYntt$k r}zt |j%t&rt |j%t'st(||dt |j%t)rt*||dt |j%t+rt,||dt |j%t-rt.||dt#||dW5d}~XYnt/k r*}zt#||dW5d}~XYnt+k rT}z t,|W5d}~XYnxt-t0fk r}zTt |t-rt.||dn6t |t1rt2||dnt |t3rt4||dnW5d}~XYnX|5|| 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 )r9N)r:r;r<r=r>zContent-Length)connectrLzInvalid timeout za. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same value.F) methodrbodyrredirectassert_same_hostpreload_contentdecode_contentretriesr; proxy_pool)r;HostT)skip_accept_encoding skip_hostrs s0 )poolrrr)6rrrrrrrrrr|tuple TimeoutSauce ValueErrorurlopenrrGhasattrr _get_connDEFAULT_POOL_TIMEOUT putrequestr\ putheader endheadersr?hexlenencode getresponser from_httplib Exceptionr@r rwrrrrrrr r _ProxyErrorr _SSLErrorr r _HTTPErrorr r_InvalidHeaderrr)r4r9r:r;r<r=r>rerchunkedrrLrlow_connrheaderr_irerrr,r,r-r?s                      zHTTPAdapter.send)N)FNTNN)rArBrCrDrZDEFAULT_POOLSIZErODEFAULT_POOLBLOCKr2r[r`rRrprrrr@rrrjr?rEr,r,r5r-rFes<  &9%  rF)DrDos.pathrtsocketZurllib3.exceptionsrrrrrrrrrr r rr r r rZurllib3.poolmanagerrrZurllib3.responserZ urllib3.utilrrrZurllib3.util.retryrauthrcompatrrrr exceptionsrrrrrrrmodelsr structuresr!utilsr"r#r$r%r&r'r(Zurllib3.contrib.socksr) ImportErrorrrrOrr/rFr,r,r,r-s>          0  $