ó ½z]c@@sÖddlmZddlmZddlZddlZddlZddlmZ ddlm Z yddl Z Wne k rdZ nXddlmZddlmZmZmZmZmZmZmZdd lmZmZdd lmZdd l m!Z!m"Z"dd l#m$Z$m%Z%ej&e'ƒZ(d e)fd„ƒYZ*de)fd„ƒYZ+de)fd„ƒYZ,e dk ršde)fd„ƒYZ-nde)fd„ƒYZ.d„Z/dej0fd„ƒYZ1dS(i(tabsolute_import(tcontextmanagerN(ttimeout(terrori(tHTTPHeaderDict(tBodyNotHttplibCompatiblet ProtocolErrort DecodeErrortReadTimeoutErrortResponseNotChunkedtIncompleteReadt InvalidHeader(t string_typestPY3(t http_client(t HTTPExceptiont BaseSSLError(t is_fp_closedtis_response_to_headtDeflateDecodercB@s#eZd„Zd„Zd„ZRS(cC@s%t|_d|_tjƒ|_dS(Nt(tTruet _first_tryt_datatzlibt decompressobjt_obj(tself((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt__init__s  cC@st|j|ƒS(N(tgetattrR(Rtname((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt __getattr__"scC@sÂ|s |S|js#|jj|ƒS|j|7_y5|jj|ƒ}|rbt|_d|_n|SWnTtjk r½t|_tjtj ƒ|_z|j|jƒSWdd|_XnXdS(N( RRt decompressRtFalsetNoneRRRt MAX_WBITS(Rtdatat decompressed((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR %s"    (t__name__t __module__RRR (((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRs  tGzipDecoderStatecB@seZdZdZdZRS(iii(R&R't FIRST_MEMBERt OTHER_MEMBERSt SWALLOW_DATA(((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR(<st GzipDecodercB@s#eZd„Zd„Zd„ZRS(cC@s)tjdtjƒ|_tj|_dS(Ni(RRR#RR(R)t_state(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyREscC@st|j|ƒS(N(RR(RR((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRIscC@sàtƒ}|jtjks"| r,t|ƒSx­trÛy||jj|ƒ7}WnEtj k r–|j}tj|_|tj krt|ƒS‚nX|jj }|s³t|ƒStj |_tj dtj ƒ|_q/WdS(Ni(t bytearrayR-R(R+tbytesRRR RRR*t unused_dataRR#(RR$trettprevious_state((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR Ls"         (R&R'RRR (((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR,Cs  t BrotliDecodercB@s#eZd„Zd„Zd„ZRS(cC@stjƒ|_dS(N(tbrotlit DecompressorR(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRgscC@s2t|jdƒr"|jj|ƒS|jj|ƒS(NR (thasattrRR tprocess(RR$((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR jscC@s#t|jdƒr|jjƒSdS(NtflushR(R6RR8(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR8os (R&R'RR R8(((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR3cs  t MultiDecodercB@s)eZdZd„Zd„Zd„ZRS(s From RFC7231: If one or more encodings have been applied to a representation, the sender that applied the encodings MUST generate a Content-Encoding header field that lists the content codings in the order in which they were applied. cC@s5g|jdƒD]}t|jƒƒ^q|_dS(Nt,(tsplitt _get_decodertstript _decoders(Rtmodestm((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR~scC@s|jdjƒS(Ni(R>R8(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR8scC@s-x&t|jƒD]}|j|ƒ}qW|S(N(treversedR>R (RR$td((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR „s(R&R't__doc__RR8R (((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR9us  cC@sOd|krt|ƒS|dkr)tƒStdk rH|dkrHtƒStƒS(NR:tgziptbr(R9R,R4R"R3R(tmode((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR<Šs   t HTTPResponsecB@síeZdZddgZed+k r4edg7ZndddddgZd d+d d d+d eed+d+d+d+d+ed+d+d „Z d „Z d „Z e d„ƒZ e d„ƒZd„Zd„Zd„Zd„ZeejfZed+k reejf7Znd„Zd„Zed„ƒZd+d+ed„Zd,d+d„Zed„ƒZd„Zd+d„Z d„Z!d„Z"e d „ƒZ#d!„Z$d"„Z%d#„Z&d$„Z'd%„Z(d&„Z)d'„Z*d+d+d(„Z+d)„Z,d*„Z-RS(-sº HTTP Response container. Backwards-compatible to httplib's HTTPResponse but the response ``body`` is loaded and decoded on-demand when the ``data`` property is accessed. This class is also compatible with the Python standard library's :mod:`io` module, and can hence be treated as a readable object in the context of that framework. Extra parameters for behaviour not present in httplib.HTTPResponse: :param preload_content: If True, the response's body will be preloaded during construction. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param original_response: When this HTTPResponse wrapper is generated from an httplib.HTTPResponse object, it's convenient to include the original for debug purposes. It's otherwise unused. :param retries: The retries contains the last :class:`~urllib3.util.retry.Retry` that was used during the request. :param enforce_content_length: Enforce content length checking. Body returned by server must match value of Content-Length header, if present. Otherwise, raise error. RDtdeflateREi-i.i/i3i4RicC@s˜t|tƒr||_nt|ƒ|_||_||_||_||_||_| |_||_ d|_ d|_ d|_ | |_d|_| |_||_|rÏt|ttfƒrÏ||_ n| |_| |_t|dƒrü||_ nt|_d|_|jjddƒjƒ}d„|jdƒDƒ}d|krZt|_n|j|ƒ|_|r”|j r”|j d|ƒ|_ ndS( Nitreadstransfer-encodingRcs@s|]}|jƒVqdS(N(R=(t.0tenc((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pys åsR:tchunkedtdecode_content(!t isinstanceRtheaderststatustversiontreasontstrictRMtretriestenforce_content_lengthR"t_decodert_bodyt_fpt_original_responset_fp_bytes_readtmsgt _request_urlt basestringR/t_poolt _connectionR6R!RLt chunk_lefttgettlowerR;Rt _init_lengthtlength_remainingRI(RtbodyRORPRQRRRStpreload_contentRMtoriginal_responsetpoolt connectionR[RTRUtrequest_methodt request_urlttr_enct encodings((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR½s@                       cC@s&|j|jkr"|jjdƒStS(s Should we redirect and where to? :returns: Truthy redirect location string if we got a redirect status code and valid location. ``None`` if redirect status and no location. ``False`` if not a redirect status code. tlocation(RPtREDIRECT_STATUSESRORaR!(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytget_redirect_locationðscC@s8|j s|j rdS|jj|jƒd|_dS(N(R^R_t _put_connR"(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt release_connýscC@s-|jr|jS|jr)|jdtƒSdS(Nt cache_content(RWRXRIR(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR$s  cC@s|jS(N(R_(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRi scC@s t|jƒS(N(RRX(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytisclosedscC@s|jS(sÛ Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:``HTTPResponse.read`` if bytes are encoded on the wire (e.g, compressed). (RZ(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyttellscC@s;|jjdƒ}|d k rÍ|jr8tjdƒd Syctg|jdƒD]}t|ƒ^qNƒ}t |ƒdkrŽt d|ƒ‚n|j ƒ}Wnt k r´d }qÍX|dkrÍd }qÍnyt|j ƒ}Wnt k rùd}nX|d ks.d |kod kns.|d kr7d}n|S(sM Set initial length value for Response content if available. scontent-lengthsÒReceived response with both Content-Length and Transfer-Encoding set. This is expressly forbidden by RFC 7230 sec 3.3.2. Ignoring Content-Length and attempting to process response as Transfer-Encoding: chunked.R:is8Content-Length contained multiple unmatching values (%s)iiÌi0idiÈtHEADN(iÌi0(RORaR"RLtlogtwarningtsetR;tinttlenR tpopt ValueErrorRP(RRjtlengthtvaltlengthsRP((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRcs,   .       4 cC@s¼|jjddƒjƒ}|jdkr¸||jkrKt|ƒ|_q¸d|kr¸g|jdƒD]'}|jƒ|jkrg|jƒ^qg}t |ƒrµt|ƒ|_qµq¸ndS(s= Set-up the _decoder attribute if necessary. scontent-encodingRR:N( RORaRbRVR"tCONTENT_DECODERSR<R;R=R{(Rtcontent_encodingteRm((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt _init_decoderLs ' cC@s|s |Sy"|jr+|jj|ƒ}nWnD|jk rr}|jjddƒjƒ}td||ƒ‚nX|rŒ||jƒ7}n|S(sN Decode the data passed in and potentially flush the decoder. scontent-encodingRsEReceived response with content-encoding: %s, but failed to decode it.(RVR tDECODER_ERROR_CLASSESRORaRbRt_flush_decoder(RR$RMt flush_decoderRƒR‚((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt_decodeas cC@s0|jr,|jjdƒ}||jjƒSdS(sk Flushes the decoder. Should only be called if the decoder is actually being used. R(RVR R8(Rtbuf((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR†us cc@st}z¦y dVWntk r:t|jddƒ‚nktk ry}dt|ƒkra‚nt|jddƒ‚n,ttfk r¤}t d||ƒ‚nXt }Wd|sê|j rÎ|j j ƒn|j rê|j j ƒqên|j r|j jƒr|jƒnXdS(sç Catch low-level python exceptions, instead re-raising urllib3 variants, so that low-level exceptions are not leaked in the high-level api. On exit, release the connection back to the pool. NsRead timed out.sread operation timed outsConnection broken: %r(R!t SocketTimeoutRR^R"RtstrRt SocketErrorRRRYtcloseR_RtRr(Rt clean_exitRƒ((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt_error_catcher€s(      cC@sY|jƒ|dkr"|j}n|jdkr5dSt}d}|jƒš|dkrr|jjƒ}t}nqt}|jj|ƒ}|dkrã| rã|jjƒt}|j rã|j dkrãt |j |j ƒ‚qãnWdQX|rU|j t |ƒ7_ |j dk r+|j t |ƒ8_ n|j|||ƒ}|rU||_qUn|S(sP Similar to :meth:`httplib.HTTPResponse.read`, but with two additional parameters: ``decode_content`` and ``cache_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache partial content as the full response. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param cache_content: If True, will save the returned data such that the same result is returned despite of the state of the underlying file object. This is useful if you want the ``.data`` property to continue working after having ``.read()`` the file object. (Overridden if ``amt`` is set.) Ni(iN(R„R"RMRXR!RRIRRRURdR RZR{RˆRW(RtamtRMRsR‡R$((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRI¹s4       !iicc@s}|jr=|jƒr=xa|j|d|ƒD] }|Vq+Wn<x9t|jƒsx|jd|d|ƒ}|r@|Vq@q@WdS(s_ A generator wrapper for the read() method. A call will block until ``amt`` bytes have been read from the connection or until the connection is closed. :param amt: How much of the content to read. The generator will return up to much data per iteration, but may return less. This is particularly likely when using compressed data. However, the empty string will never be returned. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. RMRN(RLtsupports_chunked_readst read_chunkedRRXRI(RRRMtlineR$((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytstreamþs cK@sš|j}t|tƒsEtr3t|jƒƒ}qEtj|ƒ}nt|ddƒ}|d|d|d|jd|jd|j d|d||}|S( s  Given an :class:`httplib.HTTPResponse` instance ``r``, return a corresponding :class:`urllib3.response.HTTPResponse` object. Remaining parameters are passed to the HTTPResponse constructor, along with ``original_response=r``. RSiReRORPRQRRRg( R[RNRR titemst from_httplibRRPRQRR(t ResponseClstrt response_kwRORStresp((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR–s      cC@s|jS(N(RO(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt getheaders7scC@s|jj||ƒS(N(RORa(RRtdefault((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt getheader:scC@s|jS(N(RO(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytinfo>scC@s6|js|jjƒn|jr2|jjƒndS(N(tclosedRXRR_(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRBs  cC@sV|jdkrtSt|jdƒr2|jjƒSt|jdƒrN|jjStSdS(NRtRŸ(RXR"RR6RtRŸ(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRŸIs  cC@sM|jdkrtdƒ‚n+t|jdƒr=|jjƒStdƒ‚dS(Ns-HTTPResponse has no file to get a fileno fromtfilenosOThe file-like object this HTTPResponse is wrapped around has no file descriptor(RXR"tIOErrorR6R (R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR Ts  cC@s2|jdk r.t|jdƒr.|jjƒSdS(NR8(RXR"R6R8(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR8]s!cC@stS(N(R(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytreadableascC@sI|jt|ƒƒ}t|ƒdkr+dS||t|ƒ*t|ƒSdS(Ni(RIR{(Rtbttemp((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytreadintoes cC@st|jdƒS(sú Checks if the underlying file-like object looks like a httplib.HTTPResponse object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked(). tfp(R6RX(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR‘nscC@s‚|jdk rdS|jjjƒ}|jddƒd}yt|dƒ|_Wn*tk r}|jƒt j |ƒ‚nXdS(Nt;iii( R`R"RXR¦treadlineR;RzR}RthttplibR (RR“((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt_update_chunk_lengthws  cC@sød}|dkrI|jj|jƒ}|}|jjdƒd|_n«||jkrƒ|jj|ƒ}|j||_|}nq||jkrÆ|jj|ƒ}|jjdƒd|_|}n.|jj|jƒ}|jjdƒd|_|S(Ni(R"RXt _safe_readR`(RRtreturned_chunktchunktvalue((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt _handle_chunk…s&      c c@sj|jƒ|js"tdƒ‚n|jƒs=tdƒ‚n|jƒ|jrst|jƒrs|jjƒdS|j j dkr‰dSx_t rê|j ƒ|jdkr¯Pn|j|ƒ}|j|d|dtƒ}|rŒ|VqŒqŒW|r|jƒ}|r|Vqnx6t rF|j j jƒ}|s3Pn|dkrPqqW|jr`|jjƒnWdQXdS(sÆ Similar to :meth:`HTTPResponse.read`, but with an additional parameter: ``decode_content``. :param amt: How much of the content to read. If specified, caching is skipped because it doesn't make sense to cache partial content as the full response. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. sHResponse is not chunked. Header 'transfer-encoding: chunked' is missing.sgBody should be httplib.HTTPResponse like. It should have have an fp attribute which returns raw chunks.NiRMR‡s (R„RLR R‘RRRYRRRXR¦R"RRªR`R¯RˆR!R†R¨(RRRMR­tdecodedR“((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyR’›sD                cC@s=|jdk r2t|jjƒr2|jjdjS|jSdS(sÄ Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location. iÿÿÿÿN(RTR"R{thistorytredirect_locationR\(R((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pytgeturlÞs!cc@sÇdg}x |jdtƒD]Œ}d|kr›|jdƒ}dj|ƒ|ddVx|dd!D]}|dVqeW|dr’|dg}q¨g}q|j|ƒqW|rÃdj|ƒVndS(NRRMs iiiÿÿÿÿ(R”RR;tjointappend(RtbufferR­tx((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyt__iter__és     Ni(.R&R'RCRR4R"RoRR!RRpRrtpropertyR$RiRtRuRcR„R¡RRR…RˆR†RRRIR”t classmethodR–R›RRžRRŸR R8R¢R¥R‘RªR¯R’R³R¸(((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyRG—sT    /     0    9E        C (2t __future__Rt contextlibRRtiotloggingtsocketRRŠRRŒR4t ImportErrorR"t _collectionsRt exceptionsRRRRR R R t packages.sixR R]R tpackages.six.movesRR©RiRRt util.responseRRt getLoggerR&RwtobjectRR(R,R3R9R<tIOBaseRG(((s1/tmp/pip-build-kBFYxq/urllib3/urllib3/response.pyts2     4!