ó  fH^c@sgdZddlZddlZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1m2Z2mZmZm3Z3m4Z4m5Z5m6Z6ddl7m8Z8yddl9m:Z:Wne;k rd„Z:nXe<Z=dZ>dZ?dZAdeBfd „ƒYZCd!eCfd"„ƒYZDdS(#s‰ requests.adapters ~~~~~~~~~~~~~~~~~ This module contains the transport adapters that Requests uses to define and maintain connections. iÿÿÿÿN(t PoolManagertproxy_from_url(t HTTPResponse(t parse_url(tTimeout(tRetry(tClosedPoolError(tConnectTimeoutError(t HTTPError(t MaxRetryError(tNewConnectionError(t ProxyError(t ProtocolError(tReadTimeoutError(tSSLError(t ResponseError(tLocationValueErrori(tResponse(turlparset basestring(tDEFAULT_CA_BUNDLE_PATHtextract_zipped_pathstget_encoding_from_headerstprepend_scheme_if_neededtget_auth_from_urlt urldefragautht select_proxy(tCaseInsensitiveDict(textract_cookies_to_jar( tConnectionErrortConnectTimeoutt ReadTimeoutRR t RetryErrort InvalidSchematInvalidProxyURLt InvalidURL(t_basic_auth_str(tSOCKSProxyManagercOstdƒ‚dS(Ns'Missing dependencies for SOCKS support.(R!(targstkwargs((srequests/adapters.pyR%.si it BaseAdaptercBs8eZdZd„Zededdd„Zd„ZRS(sThe Base Transport AdaptercCstt|ƒjƒdS(N(tsuperR(t__init__(tself((srequests/adapters.pyR*:scCs t‚dS(sCSends 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. N(tNotImplementedError(R+trequesttstreamttimeouttverifytcerttproxies((srequests/adapters.pytsend=scCs t‚dS(s!Cleans up adapter specific items.N(R,(R+((srequests/adapters.pytcloseOsN( t__name__t __module__t__doc__R*tFalsetNonetTrueR3R4(((srequests/adapters.pyR(7s   t HTTPAdaptercBs¹eZdZdddddgZeeeed„Zd„Zd„Z ed „Z d „Z d „Z d „Z dd „Zd„Zd„Zd„Zd„Zededdd„ZRS(sThe 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) t max_retriestconfigt_pool_connectionst _pool_maxsizet _pool_blockcCs|tkr$tddtƒ|_ntj|ƒ|_i|_i|_tt|ƒj ƒ||_ ||_ ||_ |j ||d|ƒdS(Nitreadtblock(tDEFAULT_RETRIESRR8R<tfrom_intR=t proxy_managerR)R;R*R>R?R@tinit_poolmanager(R+tpool_connectionst pool_maxsizeR<t pool_block((srequests/adapters.pyR*qs      cs‡fd†ˆjDƒS(Ncs%i|]}tˆ|dƒ|“qS(N(tgetattrR9(t.0tattr(R+(srequests/adapters.pys „s (t __attrs__(R+((R+srequests/adapters.pyt __getstate__ƒscCsbi|_i|_x*|jƒD]\}}t|||ƒqW|j|j|jd|jƒdS(NRB(RER=titemstsetattrRFR>R?R@(R+tstateRLtvalue((srequests/adapters.pyt __setstate__†s   c KsF||_||_||_td|d|d|dt||_dS(sInitializes 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_poolstmaxsizeRBtstrictN(R>R?R@RR:t poolmanager(R+t connectionsRURBt pool_kwargs((srequests/adapters.pyRF’s   c KsØ||jkr|j|}nµ|jƒjdƒrŠt|ƒ\}}t|d|d|d|jd|jd|j|}|j|`. :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 tsockstusernametpasswordRTRURBt proxy_headers( REtlowert startswithRR%R>R?R@R]R(R+tproxyt proxy_kwargstmanagerR[R\R]((srequests/adapters.pytproxy_manager_for¦s*     cCsŠ|jƒjdƒr°|r°d }|tk r6|}n|sKttƒ}n| setjj|ƒ r}t dj |ƒƒ‚nd|_ tjj |ƒs¤||_ qË||_nd|_ d |_ d |_|r†t|tƒsý|d|_|d|_n||_d |_|jrItjj|jƒ rIt dj |jƒƒ‚n|jr†tjj|jƒ r†t dj |jƒƒ‚q†nd S( sAVerify 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. thttpssECould not find a suitable TLS CA certificate bundle, invalid path: {}t CERT_REQUIREDt CERT_NONEiis9Could not find the TLS certificate file, invalid path: {}s1Could not find the TLS key file, invalid path: {}N(R^R_R9R:RRtostpathtexiststIOErrortformatt cert_reqstisdirtca_certst ca_cert_dirt isinstanceRt cert_filetkey_file(R+tconnturlR0R1tcert_loc((srequests/adapters.pyt cert_verifyËs8               cCsÂtƒ}t|ddƒ|_tt|diƒƒ|_t|jƒ|_||_|jj |_ t |j t ƒr|j j dƒ|_ n |j |_ t|j||ƒ||_||_|S(s¶Builds 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 tstatustheaderssutf-8N(RRJR9t status_codeRRxRtencodingtrawtreasonRpRttbytestdecodeRtcookiesR-t connection(R+treqtresptresponse((srequests/adapters.pytbuild_responseÿs     cCs—t||ƒ}|rit|dƒ}t|ƒ}|jsHtdƒ‚n|j|ƒ}|j|ƒ}n*t|ƒ}|jƒ}|j j|ƒ}|S(sŒ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 `. :param url: The URL to connect to. :param proxies: (optional) A Requests-style dictionary of proxies used on this request. :rtype: urllib3.ConnectionPool thttpsFPlease check proxy URL. It is malformed and could be missing the host.( RRRthostR"Rctconnection_from_urlRtgeturlRW(R+RtR2R`t proxy_urlRERstparsed((srequests/adapters.pytget_connection$s     cCs5|jjƒx!|jjƒD]}|jƒqWdS(s¢Disposes of any internal state. Currently, this closes the PoolManager and any active ProxyManager, which closes any pooled connections. N(RWtclearREtvalues(R+R`((srequests/adapters.pyR4?s c Cs•t|j|ƒ}t|jƒj}|o3|dk}t}|rit|ƒjjƒ}|jdƒ}n|j}|r‘| r‘t|jƒ}n|S(s?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 RdRZ( RRtRtschemeR8R^R_tpath_urlR( R+R-R2R`RŽtis_proxied_http_requesttusing_socks_proxyt proxy_schemeRt((srequests/adapters.pyt request_urlIs  cKsdS(s"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(). N((R+R-R'((srequests/adapters.pyt add_headersfs cCs8i}t|ƒ\}}|r4t||ƒ|d`. :param proxy: The url of the proxy being used for this request. :rtype: dict sProxy-Authorization(RR$(R+R`RxR[R\((srequests/adapters.pyR]ts cCsÇy|j|j|ƒ}Wn%tk r@}t|d|ƒ‚nX|j||j||ƒ|j||ƒ} |j|d|d|d|d|d|ƒ|jdkp²d|j k } t |t ƒry%|\} } t d| d | ƒ}WqEt k r}d j|ƒ} t | ƒ‚qEXn't |t ƒr0nt d|d |ƒ}yÇ| s¥|jd |jd | d |jd|j dtdtdtdtd|jd|ƒ }nft|dƒrÀ|j}n|jdtƒ}y"|j|j| dtƒx-|j jƒD]\}}|j||ƒqþW|jƒx^|jD]S}|jtt|ƒƒdjdƒƒ|jdƒ|j|ƒ|jdƒq2W|jdƒy|j dtƒ}Wnt!k rË|j ƒ}nXt"j#|d|d|dtdtƒ}Wn|j$ƒ‚nXWn¨t%t&j'fk r<} t(| d|ƒ‚n{t)k r}t |j*t+ƒr‡t |j*t,ƒs‡t-|d|ƒ‚q‡nt |j*t.ƒr®t/|d|ƒ‚nt |j*t0ƒrÕt1|d|ƒ‚nt |j*t2ƒrüt3|d|ƒ‚nt(|d|ƒ‚n¦t4k r5}t(|d|ƒ‚n‚t0k rS}t1|ƒ‚ndt2t5fk r¶}t |t2ƒrŒt3|d|ƒ‚q·t |t6ƒr°t7|d|ƒ‚q·‚nX|j8||ƒS(sSends 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 R-R.R/R0R1R2sContent-LengthtconnectRAsrInvalid timeout {}. Pass a (connect, read) timeout tuple, or a single float to set both timeouts to the same valuetmethodRttbodyRxtredirecttassert_same_hosttpreload_contenttdecode_contenttretriest proxy_pooltskip_accept_encodingisutf-8s s0 t bufferingtpoolR€N(9R‹RtRR#RvR“R”R—R9RxRpttuplet TimeoutSaucet ValueErrorRkturlopenR–R8R<thasattrRt _get_conntDEFAULT_POOL_TIMEOUTt putrequestR:ROt putheadert endheadersR3thextlentencodet getresponset TypeErrorRt from_httplibR4R tsocketterrorRR R|RR RRR t _ProxyErrorR t _SSLErrorRRt _HTTPErrorR RR„(R+R-R.R/R0R1R2RsteRttchunkedR•RAterrR‚tlow_conntheaderRRtitr((srequests/adapters.pyR3Šsª+           &       N(R5R6R7RMtDEFAULT_POOLSIZERCtDEFAULT_POOLBLOCKR*RNRSRFRcRvR„R9R‹R4R“R”R]R8R:R3(((srequests/adapters.pyR;Ts$      % 4 %    (ER7tos.pathRgR±turllib3.poolmanagerRRturllib3.responseRt urllib3.utilRRR¢turllib3.util.retryRturllib3.exceptionsRRRRµR R R R³R R RR´RRtmodelsRtcompatRRtutilsRRRRRRRt structuresRRRt exceptionsRRRR R!R"R#tauthR$turllib3.contrib.socksR%t ImportErrorR8R¾R½RCR9R§tobjectR(R;(((srequests/adapters.pyt sF  4@