U ÀÒ.e¼lã@s\ddlmZddlmZddlZddlZddlZddlmZ ddlm Z z ddl Z Wne k rldZ YnXddlmZddlmZmZmZmZmZmZmZdd lmZmZdd lmZdd lm Z m!Z!dd l"m#Z#m$Z$e %e&¡Z'Gd d„de(ƒZ)Gdd„de(ƒZ*Gdd„de(ƒZ+e dk �r.Gdd„de(ƒZ,Gdd„de(ƒZ-dd„Z.Gdd„dej/ƒZ0dS)é)Ú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ú@/usr/lib/python3.8/site-packages/pip/_vendor/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_z"|j |¡}|rFd|_d|_|WStjk r”d|_t tj ¡|_z| |j¡W¢YSd|_XYnXdS)NF)rrÚ decompressrrrrÚ MAX_WBITS)rÚdataZ decompressedrrr r()s"  zDeflateDecoder.decompressN©Ú__name__Ú __module__Ú __qualname__r!r'r(rrrr r src@seZdZdZdZdZdS)ÚGzipDecoderStaterréN)r,r-r.Ú FIRST_MEMBERÚ OTHER_MEMBERSÚ SWALLOW_DATArrrr r/@sr/c@s$eZdZdd„Zdd„Zdd„ZdS)Ú GzipDecodercCst dtj¡|_tj|_dS©Né)rrr)rr/r1Ú_staterrrr r!HszGzipDecoder.__init__cCs t|j|ƒSr"r#r%rrr r'LszGzipDecoder.__getattr__cCs¤tƒ}|jtjks|st|ƒSz||j |¡7}Wn<tjk rn|j}tj|_|tj krht|ƒYS‚YnX|jj }|s„t|ƒStj |_t  dtj ¡|_qdSr5) Ú bytearrayr7r/r3Úbytesrr(rrr2Z unused_datarr))rr*ÚretZprevious_staterrr r(Os   zGzipDecoder.decompressNr+rrrr r4Gsr4c@s$eZdZdd„Zdd„Zdd„ZdS)Ú BrotliDecodercCst ¡|_dSr")ÚbrotliZ Decompressorrrrrr r!kszBrotliDecoder.__init__cCs$t|jdƒr|j |¡S|j |¡S)Nr()Úhasattrrr(Zprocess)rr*rrr r(ns  zBrotliDecoder.decompresscCst|jdƒr|j ¡SdS)NÚflushr)r=rr>rrrr r>ss  zBrotliDecoder.flushN)r,r-r.r!r(r>rrrr r;gsr;c@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 Ú ƒsz)MultiDecoder.__init__..ú,)ÚsplitÚ _decoders)rZmodesrrr r!‚szMultiDecoder.__init__cCs|jd ¡S©Nr)rGr>rrrr r>…szMultiDecoder.flushcCst|jƒD]}| |¡}q |Sr")ÚreversedrGr()rr*Údrrr r(ˆs zMultiDecoder.decompressN)r,r-r.Ú__doc__r!r>r(rrrr r?ysr?cCs:d|krt|ƒS|dkrtƒStdk r4|dkr4tƒStƒS)NrEÚgzipÚbr)r?r4r<r;r)Úmoderrr r@Žsr@c@sdeZdZdZddgZedk r&edg7Zdddd d gZdLdd„Zdd„Zdd„Z e dd„ƒZ e dd„ƒZ dd„Z dd„Zdd„Zdd „ZeejfZedk r¤eejf7Zd!d"„Zd#d$„Zed%d&„ƒZdMd'd(„ZdNd*d+„Zed,d-„ƒZd.d/„ZdOd0d1„Zd2d3„Zd4d5„Z e d6d7„ƒZ!d8d9„Z"d:d;„Z#dd?„Z%d@dA„Z&dBdC„Z'dDdE„Z(dPdFdG„Z)dHdI„Z*dJdK„Z+dS)QÚ 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. rLZdeflateNrMi-i.i/i3i4ÚrTFcCst|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-encodingrPcss|]}| ¡VqdSr")rA)rBÚencrrr Ú ùsz(HTTPResponse.__init__..rEÚchunkedT©Údecode_content)Ú isinstancerÚheadersÚstatusÚversionÚreasonÚstrictrVÚretriesÚenforce_content_lengthÚ auto_closeÚ_decoderÚ_bodyÚ_fpÚ_original_responseÚ_fp_bytes_readÚmsgÚ _request_urlÚ basestringr9Ú_poolÚ _connectionr=rTÚ chunk_leftÚgetÚlowerrFÚ _init_lengthÚlength_remainingrQ)rÚbodyrXrYrZr[r\Zpreload_contentrVÚoriginal_responseZpoolÚ connectionrer]r^Úrequest_methodZ request_urlr_Ztr_encÚ encodingsrrr r!ÁsB    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)rYÚREDIRECT_STATUSESrXrkrrrr Úget_redirect_locations  z"HTTPResponse.get_redirect_locationcCs(|jr |jsdS|j |j¡d|_dSr")rhriZ _put_connrrrr Ú release_conns zHTTPResponse.release_conncCs"|jr |jS|jr|jdd�SdS)NT)Ú cache_content)rarbrQrrrr r*szHTTPResponse.datacCs|jSr")rirrrr rq!szHTTPResponse.connectioncCs t|jƒSr")rrbrrrr Ú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). )rdrrrr Útell(szHTTPResponse.tellcCsà|j d¡}|dk rŠ|jr(t d¡dSz.rErz8Content-Length contained multiple unmatching values (%s)r)éÌi0édéÈZHEAD) rXrkrTÚlogZwarningÚsetrFÚlenrÚpopÚ ValueErrorr{rY)rrrZlengthZlengthsrYrrr rm0s6 ÿ ÿÿ   &zHTTPResponse._init_lengthcshˆj dd¡ ¡}ˆjdkrd|ˆjkr2t|ƒˆ_n2d|krd‡fdd„| d¡Dƒ}t|ƒrdt|ƒˆ_dS)z= Set-up the _decoder attribute if necessary. úcontent-encodingrPNrEcs"g|]}| ¡ˆjkr| ¡‘qSr)rAÚCONTENT_DECODERS)rBÚerrr rDosþz.HTTPResponse._init_decoder..)rXrkrlr`r†r@rFr‚)rÚcontent_encodingrsrrr Ú _init_decoderds    þzHTTPResponse._init_decoderc Csx|s|Sz|jr|j |¡}WnD|jk rb}z$|j dd¡ ¡}td||ƒ‚W5d}~XYnX|rt|| ¡7}|S)zN Decode the data passed in and potentially flush the decoder. r…rPzEReceived response with content-encoding: %s, but failed to decode it.N)r`r(ÚDECODER_ERROR_CLASSESrXrkrlr Ú_flush_decoder)rr*rVÚ 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)r`r(r>)rZbufrrr r‹‘s zHTTPResponse._flush_decoderc csìd}z¤z dVWn�tk r2t|jddƒ‚Ynptk rn}z dt |ƒkrP‚t|jddƒ‚W5d}~XYn4t t fk r }zt d||ƒ‚W5d}~XYnXd}W5|sÎ|jr¾|j ¡|jrÎ|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) rcÚcloseriryrwÚ SocketTimeoutr rhrÚstrrÚ SocketErrorr )rZ clean_exitr‡rrr Ú_error_catcherœs(      zHTTPResponse._error_catcherc Cs | ¡|dkr|j}|jdkr$dSd}t|jddƒ}| ¡�v|dkr`|sV|j ¡nd}d}nPd}|st|j |¡nd}|dkr°|s°|j ¡d}|jr°|jdkr°t |j |jƒ‚W5QRX|�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.) NFÚclosedrTr)rN)r‰rVrbr$r’rQrŽr^rnr rdr‚r�ra)rÚamtrVrxrŒZ fp_closedr*rrr rQÕs:  ÿÿ   zHTTPResponse.readéccsR|jr*| ¡r*|j||d�D] }|Vqn$t|jƒsN|j||d�}|r*|Vq*dS)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. rU)r”rVN)rTÚsupports_chunked_readsÚ read_chunkedrrbrQ)rr”rVÚliner*rrr Ústreams  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``. r\r)rorXrYrZr[r\rp) rerWrrÚitemsÚ from_httplibr$rYrZr[)Z ResponseClsÚrZ response_kwrXr\Zresprrr r›9s$    ùø zHTTPResponse.from_httplibcCs|jSr"©rXrrrr Ú getheadersZszHTTPResponse.getheaderscCs|j ||¡Sr")rXrk)rr&Údefaultrrr Ú getheader]szHTTPResponse.getheadercCs|jSr"r�rrrr ÚinfoaszHTTPResponse.infocCs6|js|j ¡|jr |j ¡|js2tj |¡dSr")r“rbrŽrir_ÚioÚIOBaserrrr rŽes   zHTTPResponse.closecCsT|jstjj |¡S|jdkr"dSt|jdƒr8|j ¡St|jdƒrL|jjSdSdS)NTryr“)r_r¢r£r“Ú__get__rbr=ryrrrr r“os    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)rbÚIOErrorr=r¥rrrr r¥|s    ÿzHTTPResponse.filenocCs2|jdk r.t|jdƒr.t|jddƒs.|j ¡SdS)Nr>r“F)rbr=r$r>rrrr r>‡sÿ þ ýzHTTPResponse.flushcCsdS)NTrrrrr Úreadable�szHTTPResponse.readablecCs:| t|ƒ¡}t|ƒdkrdS||dt|ƒ…<t|ƒSdSrH)rQr‚)rÚbZtemprrr Ú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)r=rbrrrr r–œsz#HTTPResponse.supports_chunked_readscCsf|jdk rdS|jj ¡}| dd¡d}zt|dƒ|_Wn&tk r`| ¡t  |¡‚YnXdS)Nó;rrr6) rjrbrªÚreadlinerFr{r„rŽÚhttplibr )rr˜rrr Ú_update_chunk_length¥s  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)Nr0)rbZ _safe_readrj)rr”Zreturned_chunkÚchunkÚvaluerrr Ú _handle_chunk³s&        zHTTPResponse._handle_chunkc csþ| ¡|jstdƒ‚| ¡s&tdƒ‚| ¡�Æ|jrXt|jƒrX|j ¡W5QR£dS|j j dkrrW5QR£dS|  ¡|j dkr†q¬|  |¡}|j||dd�}|rr|Vqr|rÂ| ¡}|rÂ|V|j j  ¡}|sÔqà|dkrÂqàqÂ|jrð|j ¡W5QRXdS)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)rVrŒs )r‰rTr r–rr’rcrrŽrbrªr®rjr±r�r‹r¬)rr”rVr¯Zdecodedr˜rrr r—ÉsJÿÿ     ÿ 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éÿÿÿÿ)r]r‚ÚhistoryZredirect_locationrfrrrr ÚgeturlszHTTPResponse.geturlccs–dg}|jdd�D]n}d|krv| d¡}d |¡|ddV|dd…D]}|dVqL|drp|dg}q€g}q| |¡q|r’d |¡VdS)NrTrUó rrr²)r™rFÚjoinÚappend)rÚbufferr¯Úxrrr Ú__iter__s    zHTTPResponse.__iter__)rPNrrNrTTNNNNNFNNT)NNF)r•N)N)NN),r,r-r.rKr†r<rur!rvrwÚpropertyr*rqryrzrmr‰r¦rrrŠr�r‹rr’rQr™Ú classmethodr›ržr r¡rŽr“r¥r>r§r©r–r®r±r—r´rºrrrr rO›sx  î C   4    8 J         F rO)1Z __future__rÚ contextlibrrr¢ZloggingZsocketrr�rr‘r<Ú ImportErrorÚ _collectionsrÚ exceptionsrr r r r r rZ packages.sixrrgrZpackages.six.movesrr­rqrrZ util.responserrZ getLoggerr,r€Úobjectrr/r4r;r?r@r£rOrrrr Ús2       $