B ½sÁ\ dã@sddlmZddlmZddlZddlZddlZddlmZ ddlm Z ddl m Z ddlmZmZmZmZmZmZmZdd lmZmZdd lmZdd lmZmZdd l m!Z!m"Z"e #e$¡Z%Gd d„de&ƒZ'Gdd„de&ƒZ(Gdd„de&ƒZ)Gdd„de&ƒZ*dd„Z+Gdd„dej,ƒZ-dS)é)Úabsolute_import)ÚcontextmanagerN)Útimeout)Úerroré)ÚHTTPHeaderDict)ÚBodyNotHttplibCompatibleÚ ProtocolErrorÚ DecodeErrorÚReadTimeoutErrorÚResponseNotChunkedÚIncompleteReadÚ InvalidHeader)Ú string_typesÚPY3)Ú http_client)Ú HTTPExceptionÚ BaseSSLError)Ú is_fp_closedÚis_response_to_headc@s$eZdZdd„Zdd„Zdd„ZdS)ÚDeflateDecodercCsd|_d|_t ¡|_dS)NTó)Ú _first_tryÚ_dataÚzlibÚ decompressobjÚ_obj)Úself©rúi/private/var/folders/fg/f5fxz_wx3cv_637gxj8bfql5sxrnvq/T/pip-install-t7evjugg/urllib3/urllib3/response.pyÚ__init__szDeflateDecoder.__init__cCs t|j|ƒS)N)Úgetattrr)rÚnamerrrÚ __getattr__szDeflateDecoder.__getattr__c Cs|s|S|js|j |¡S|j|7_y |j |¡}|rFd|_d|_|Stjk rŠd|_t tj ¡|_z | |j¡Sd|_XYnXdS)NF)rrÚ decompressrrrrÚ MAX_WBITS)rÚdataÚ decompressedrrrr$ s"   zDeflateDecoder.decompressN)Ú__name__Ú __module__Ú __qualname__r r#r$rrrrrsrc@seZdZdZdZdZdS)ÚGzipDecoderStaterréN)r(r)r*Ú FIRST_MEMBERÚ OTHER_MEMBERSÚ SWALLOW_DATArrrrr+7sr+c@s$eZdZdd„Zdd„Zdd„ZdS)Ú GzipDecodercCst dtj¡|_tj|_dS)Né)rrr%rr+r-Ú_state)rrrrr @szGzipDecoder.__init__cCs t|j|ƒS)N)r!r)rr"rrrr#DszGzipDecoder.__getattr__cCs¤tƒ}|jtjks|st|ƒSx€y||j |¡7}Wn8tjk rl|j}tj|_|tj krft|ƒS‚YnX|jj }|s‚t|ƒStj |_t  dtj ¡|_q WdS)Nr1) Ú bytearrayr2r+r/Úbytesrr$rrr.Ú unused_datarr%)rr&ÚretÚprevious_staterrrr$Gs" zGzipDecoder.decompressN)r(r)r*r r#r$rrrrr0>sr0c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú MultiDecodera 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. cCsdd„| d¡Dƒ|_dS)NcSsg|]}t| ¡ƒ‘qSr)Ú _get_decoderÚstrip)Ú.0Úmrrrú gsz)MultiDecoder.__init__..ú,)ÚsplitÚ _decoders)rÚmodesrrrr fszMultiDecoder.__init__cCs|jd ¡S)Nr)r@Úflush)rrrrrBiszMultiDecoder.flushcCs"xt|jƒD]}| |¡}q W|S)N)Úreversedr@r$)rr&Údrrrr$lszMultiDecoder.decompressN)r(r)r*Ú__doc__r rBr$rrrrr8]sr8cCs$d|krt|ƒS|dkrtƒStƒS)Nr>Úgzip)r8r0r)Úmoderrrr9rs r9c@s,eZdZdZddgZdddddgZdIdd„Zdd„Zdd„Ze dd„ƒZ e dd„ƒZ dd„Z dd„Z dd„Zdd„Zd d!„Zd"d#„Zed$d%„ƒZdJd&d'„ZdKd)d*„Zed+d,„ƒZd-d.„ZdLd/d0„Zd1d2„Zd3d4„Ze d5d6„ƒZd7d8„Zd9d:„Zd;d<„Zd=d>„Z d?d@„Z!dAdB„Z"dCdD„Z#dMdEdF„Z$dGdH„Z%d S)NÚ HTTPResponseaº 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. rFÚdeflatei-i.i/i3i4ÚNrTFcCst|tƒr||_n t|ƒ|_||_||_||_||_||_| |_||_ d|_ d|_ d|_ | |_ d|_| |_||_|rˆt|ttfƒrˆ||_ | |_| |_t|dƒr¤||_ d|_d|_|j dd¡ ¡}dd„| d¡Dƒ}d |kräd |_| |¡|_|r |j s |j|d |_ dS) NrÚreadFztransfer-encodingrJcss|]}| ¡VqdS)N)r:)r;Úencrrrú Èsz(HTTPResponse.__init__..r>ÚchunkedT)Údecode_content)Ú isinstancerÚheadersÚstatusÚversionÚreasonÚstrictrOÚretriesÚenforce_content_lengthÚ_decoderÚ_bodyÚ_fpÚ_original_responseÚ_fp_bytes_readÚmsgÚ _request_urlÚ basestringr4Ú_poolÚ _connectionÚhasattrrNÚ chunk_leftÚgetÚlowerr?Ú _init_lengthÚlength_remainingrK)rÚbodyrQrRrSrTrUÚpreload_contentrOÚoriginal_responseÚpoolÚ connectionr]rVrWÚrequest_methodÚ request_urlÚtr_encÚ encodingsrrrr  s@    zHTTPResponse.__init__cCs|j|jkr|j d¡SdS)a 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. ÚlocationF)rRÚREDIRECT_STATUSESrQrd)rrrrÚget_redirect_locationÓs  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j |j¡d|_dS)N)r`raÚ _put_conn)rrrrÚ release_connàs zHTTPResponse.release_conncCs"|jr |jS|jr|jddSdS)NT)Ú cache_content)rYrZrK)rrrrr&çszHTTPResponse.datacCs|jS)N)ra)rrrrrlðszHTTPResponse.connectioncCs t|jƒS)N)rrZ)rrrrÚisclosedôszHTTPResponse.isclosedcCs|jS)zÛ 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). )r\)rrrrÚtell÷szHTTPResponse.tellcCsà|j d¡}|dk rŠ|jr(t d¡dSy.r>rz8Content-Length contained multiple unmatching values (%s)r)éÌi0édéÈÚHEAD) rQrdrNÚlogÚwarningÚsetr?ÚlenrÚpopÚ ValueErrorryrR)rrmÚlengthÚlengthsrRrrrrfÿs,      &zHTTPResponse._init_lengthcshˆj dd¡ ¡}ˆjdkrd|ˆjkr2t|ƒˆ_n2d|krd‡fdd„| d¡Dƒ}t|ƒrdt|ƒˆ_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrJNr>cs"g|]}| ¡ˆjkr| ¡‘qSr)r:ÚCONTENT_DECODERS)r;Úe)rrrr=:sz.HTTPResponse._init_decoder..)rQrdrerXr‡r9r?r‚)rÚcontent_encodingrpr)rrÚ _init_decoder/s   zHTTPResponse._init_decoderc Cs|y|r|jr|j |¡}WnHttjfk rb}z$|j dd¡ ¡}td||ƒ‚Wdd}~XYnX|rx|rx||  ¡7}|S)zN Decode the data passed in and potentially flush the decoder. zcontent-encodingrJzEReceived response with content-encoding: %s, but failed to decode it.N) rXr$ÚIOErrorrrrQrdrer Ú_flush_decoder)rr&rOÚ flush_decoderrˆr‰rrrÚ_decode>s  zHTTPResponse._decodecCs$|jr |j d¡}||j ¡SdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)rXr$rB)rÚbufrrrrŒPs zHTTPResponse._flush_decoderc csìd}z¤y dVWntk r2t|jddƒ‚Ynptk rn}z dt|ƒkrP‚t|jddƒ‚Wdd}~XYn4ttfk r }ztd||ƒ‚Wdd}~XYnXd}Wd|sÎ|jr¾|j  ¡|j rÎ|j   ¡|jræ|j  ¡ræ|  ¡XdS)zç 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. FNzRead timed out.zread operation timed outzConnection broken: %rT) Ú SocketTimeoutr r`rÚstrrÚ SocketErrorr r[Úcloserarwru)rÚ clean_exitrˆrrrÚ_error_catcher[s(      zHTTPResponse._error_catcherc Csî| ¡|dkr|j}|jdkr$dSd}d}| ¡f|dkrN|j ¡}d}nHd}|j |¡}|dkr–|s–|j ¡d}|jr–|jdkr–t|j |jƒ‚WdQRX|rê|j t |ƒ7_ |jdk rÒ|jt |ƒ8_|  |||¡}|rê||_ |S)aP 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.) NFTr)rN) rŠrOrZr•rKr“rWrgr r\r‚rŽrY)rÚamtrOrvrr&rrrrK”s4       zHTTPResponse.readéccsZ|jr.| ¡r.xF|j||dD] }|VqWn(x&t|jƒsT|j||d}|r0|Vq0WdS)a_ 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. )rO)r–rON)rNÚsupports_chunked_readsÚ read_chunkedrrZrK)rr–rOÚliner&rrrÚstreamÙs  zHTTPResponse.streamc Ks`|j}t|tƒs,tr"t| ¡ƒ}n t |¡}t|ddƒ}|f|||j|j|j ||dœ|—Ž}|S)a  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``. rUr)rhrQrRrSrTrUrj) r]rPrrÚitemsÚ from_httplibr!rRrSrT)Ú ResponseClsÚrÚ response_kwrQrUÚresprrrrós    zHTTPResponse.from_httplibcCs|jS)N)rQ)rrrrÚ getheadersszHTTPResponse.getheaderscCs|j ||¡S)N)rQrd)rr"ÚdefaultrrrÚ getheaderszHTTPResponse.getheadercCs|jS)N)rQ)rrrrÚinfoszHTTPResponse.infocCs$|js|j ¡|jr |j ¡dS)N)ÚclosedrZr“ra)rrrrr“s zHTTPResponse.closecCs@|jdkrdSt|jdƒr$|j ¡St|jdƒr8|jjSdSdS)NTrwr¦)rZrbrwr¦)rrrrr¦#s    zHTTPResponse.closedcCs6|jdkrtdƒ‚nt|jdƒr*|j ¡Stdƒ‚dS)Nz-HTTPResponse has no file to get a fileno fromÚfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)rZr‹rbr§)rrrrr§.s     zHTTPResponse.filenocCs$|jdk r t|jdƒr |j ¡SdS)NrB)rZrbrB)rrrrrB7szHTTPResponse.flushcCsdS)NTr)rrrrÚreadable;szHTTPResponse.readablecCs:| t|ƒ¡}t|ƒdkrdS||dt|ƒ…<t|ƒSdS)Nr)rKr‚)rÚbÚtemprrrÚreadinto?s  zHTTPResponse.readintocCs t|jdƒS)zú 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(). Úfp)rbrZ)rrrrr˜Hsz#HTTPResponse.supports_chunked_readscCsf|jdk rdS|jj ¡}| dd¡d}yt|dƒ|_Wn&tk r`| ¡t  |¡‚YnXdS)Nó;rrr1) rcrZr¬Úreadliner?ryr„r“Úhttplibr )rršrrrÚ_update_chunk_lengthQs  z!HTTPResponse._update_chunk_lengthcCs¬d}|dkr2|j |j¡}|}|j d¡d|_nv||jkrZ|j |¡}|j||_|}nN||jkrˆ|j |¡}|j d¡d|_|}n |j |j¡}|j d¡d|_|S)Nr,)rZÚ _safe_readrc)rr–Úreturned_chunkÚchunkÚvaluerrrÚ _handle_chunk_s&        zHTTPResponse._handle_chunkc csò| ¡|jstdƒ‚| ¡s&tdƒ‚| ¡º|jrNt|jƒrN|j ¡dS|j j dkr^dSx<|  ¡|j dkrtP|  |¡}|j||dd}|r`|Vq`W|r²| ¡}|r²|Vx |j j  ¡}|sÆP|dkr´Pq´W|jrä|j ¡WdQRXdS)aÆ 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. zHResponse is not chunked. Header 'transfer-encoding: chunked' is missing.zgBody should be httplib.HTTPResponse like. It should have have an fp attribute which returns raw chunks.NrF)rOrs )rŠrNr r˜rr•r[rr“rZr¬r°rcrµrŽrŒr®)rr–rOr³Údecodedršrrrr™usD       zHTTPResponse.read_chunkedcCs.|jdk r$t|jjƒr$|jjdjS|jSdS)zÄ 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. Néÿÿÿÿ)rVr‚ÚhistoryÚredirect_locationr^)rrrrÚgeturl¸szHTTPResponse.geturl)rJNrrNrTTNNNNNFNN)NNF)r—N)N)NN)&r(r)r*rEr‡rrr rsruÚpropertyr&rlrwrxrfrŠrŽrŒrr•rKr›Ú classmethodrr¢r¤r¥r“r¦r§rBr¨r«r˜r°rµr™rºrrrrrH|sH /  0 9 E        CrH).Ú __future__rÚ contextlibrrÚioÚloggingÚsocketrrrr’Ú _collectionsrÚ exceptionsrr r r r r rZ packages.sixrr_rZpackages.six.movesrr¯rlrrZ util.responserrÚ getLoggerr(rÚobjectrr+r0r8r9ÚIOBaserHrrrrÚs&     $  !