3 »nNj‹†ã@sFddlmZddlmZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m ZddlmZddlmZmZmZmZmZmZmZmZdd lmZmZdd lmZ dd l!m"Z"m#Z#dd l$m%Z%m&Z&ej'e(ƒZ)Gd d„de*ƒZ+Gdd„de*ƒZ,Gdd„de*ƒZ-Gdd„de*ƒZ.dd„Z/Gdd„dƒZ0Gdd„dej1ƒZ2dS)é)Úabsolute_import)ÚcontextmanagerN)Útimeout)Úerroré)ÚHTTPHeaderDict)ÚBodyNotHttplibCompatibleÚ ProtocolErrorÚ DecodeErrorÚReadTimeoutErrorÚResponseNotChunkedÚIncompleteReadÚ InvalidHeaderÚDependencyWarning)Ú string_typesÚPY3)Ú http_client)Ú HTTPExceptionÚ BaseSSLError)Ú is_fp_closedÚis_response_to_headc@s2eZdZdd„Zdd„Zd dd„Zedd „ƒZd S) ÚDeflateDecodercCs d|_d|_d|_tjƒ|_dS)NTó)Ú _first_tryÚ_first_try_dataÚ _unfed_dataÚzlibÚ decompressobjÚ_obj)Úself©r ú/usr/lib/python3.6/response.pyÚ__init__szDeflateDecoder.__init__cCs t|j|ƒS)N)Úgetattrr)rÚnamer r r!Ú __getattr__!szDeflateDecoder.__getattr__rcCsä|j|}d|_| r$|jj r$|S|}|dkr6d}n|dkrH||_dS|jsf|jj|jj||d�S|j|7_y$|jj||d�}|r–d|_d|_|Stjk rÞd|_tjtj ƒ|_z|j|j|d�Sd|_XYnXdS)Nrr)Ú max_lengthF) rrÚunconsumed_tailrÚ decompressrrrrÚ MAX_WBITS)rÚdatar&Zoriginal_max_lengthZ decompressedr r r!r($s6  zDeflateDecoder.decompresscCst|jƒpt|jjƒo|j S)N)Úboolrrr'r)rr r r!Úhas_unconsumed_tailNs z"DeflateDecoder.has_unconsumed_tailNéÿÿÿÿ)r-)Ú__name__Ú __module__Ú __qualname__r"r%r(Úpropertyr,r r r r!rs *rc@seZdZdZdZdZdS)ÚGzipDecoderStaterréN)r.r/r0Ú FIRST_MEMBERÚ OTHER_MEMBERSÚ SWALLOW_DATAr r r r!r2Usr2c@s2eZdZdd„Zdd„Zd dd„Zedd „ƒZd S) Ú GzipDecodercCs$tjdtjƒ|_tj|_d|_dS)Nér)rrr)rr2r4Ú_stateÚ_unconsumed_tail)rr r r!r"^szGzipDecoder.__init__cCs t|j|ƒS)N)r#r)rr$r r r!r%cszGzipDecoder.__getattr__rc Cs"tƒ}|jtjkrt|ƒS|dkr4|j|7_dS|j|}| rT|jjrTt|ƒSxÄy&||jj|t |t |ƒdƒd�7}Wn>t j k rº|j}tj|_d|_|tj kr´t|ƒS‚YnX|jjpÊ|jj|_}|dkrèt |ƒ|krèP|sôt|ƒS|jjrVtj |_t jdt jƒ|_qVWt|ƒS)Nrr)r&r8)Ú bytearrayr9r2r6Úbytesr:rÚeofr(ÚmaxÚlenrrr5r'Z unused_datarr))rr*r&ÚretZprevious_stater r r!r(fs:   zGzipDecoder.decompresscCs t|jƒS)N)r+r:)rr r r!r,˜szGzipDecoder.has_unconsumed_tailNr-)r-)r.r/r0r"r%r(r1r,r r r r!r7\s 2r7c@s:eZdZdZdZdd„Zdd„Zddd „Zed d „ƒZ d S)Ú 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. écCsPdd„|jdƒDƒ}t|ƒ|jkr«sz)MultiDecoder.__init__..ú,z)Too many content encodings in the chain: z > cSsg|] }t|ƒ‘qSr )Ú _get_decoder)rDÚer r r!rF±s)Úsplitr?Úmax_decode_linksr Ú _decoders)rZmodesÚ encodingsr r r!r"ªs zMultiDecoder.__init__cCs|jdjƒS)Nr)rLÚflush)rr r r!rN³szMultiDecoder.flushrcCs–|dkr*xt|jƒD]}|j|ƒ}qW|Stƒ}x`d}x0t|jƒD]"}|j||t|ƒd�}|rBd}qBW||7}| s‚t|ƒ|krŠt|ƒSd}q2WdS)NrF)r&Tr)ÚreversedrLr(r;r?r<)rr*r&Údr@Zany_datar r r!r(¶szMultiDecoder.decompresscCstdd„|jDƒƒS)Ncss|] }|jVqdS)N)r,)rDrPr r r!ú Òsz3MultiDecoder.has_unconsumed_tail..)ÚanyrL)rr r r!r,Ðsz MultiDecoder.has_unconsumed_tailNr-)r-) r.r/r0Ú__doc__rKr"rNr(r1r,r r r r!rA�s   rAcCs$d|krt|ƒS|dkrtƒStƒS)NrGÚgzip)rAr7r)Úmoder r r!rHÕs rHc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) ÚBytesQueueBuffera˜Memory-efficient bytes buffer To return decoded data in read() and still follow the BufferedIOBase API, we need a buffer to always return the correct amount of bytes. This buffer should be filled using calls to put() Our maximum memory usage is determined by the sum of the size of: * self.buffer, which contains the full data * the largest chunk that we will copy in get() cCstjƒ|_d|_dS)Nr)Ú collectionsÚdequeÚbufferÚ_size)rr r r!r"ís zBytesQueueBuffer.__init__cCs|jS)N)rZ)rr r r!Ú__len__ñszBytesQueueBuffer.__len__cCs"|jj|ƒ|jt|ƒ7_dS)N)rYÚappendrZr?)rr*r r r!Úputôs zBytesQueueBuffer.putc Cs|dkr dS|jstdƒ‚n|dkr,tdƒ‚t|jdƒ|krft|jdtƒrf|j|8_|jjƒSd}tj ƒ}xš||k�r ||}|jjƒ}t|ƒ}||krâ|d|…||d…}}|j |ƒ|jj |ƒ|j|8_Pn|j |ƒ|j|8_||7}|jstPqtW|j ƒS)Nrrzbuffer is emptyzn should be > 0) rYÚ RuntimeErrorÚ ValueErrorr?Ú isinstancer<rZÚpopleftÚioÚBytesIOÚwriteÚ appendleftÚgetvalue) rÚnZfetchedr@Z remainingÚchunkZ chunk_lengthZ left_chunkZ right_chunkr r r!Úgetøs6 "      zBytesQueueBuffer.getN)r.r/r0rSr"r[r]rir r r r!rVßs  rVc@s8eZdZdZddgZdddddgZdLdd„Zdd„Zdd„Ze dd„ƒZ e dd„ƒZ dd„Z dd„Z dd„Zdd„ZdMd d!„Zd"d#„Zed$d%„ƒZdNd&d'„ZdOd(d)„ZdQd,d-„Zed.d/„ƒZd0d1„ZdRd2d3„Zd4d5„Zd6d7„Ze d8d9„ƒZd:d;„Zdd?„Z d@dA„Z!dBdC„Z"dDdE„Z#dFdG„Z$dSdHdI„Z%dJdK„Z&d S)TÚ 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. rTZdeflatei-i.i/i3i4ÚNrTFcCs t|tƒr||_n t|ƒ|_||_||_||_||_||_d|_| |_ ||_ d|_ d|_ d|_ | |_d|_| |_||_|rŽt|ttfƒrŽ||_ | |_| |_t|dƒrª||_ d|_d|_|jjddƒjƒ}dd„|jdƒDƒ}d |krêd |_|j|ƒ|_tƒ|_|�r|j �r|j |d �|_ dS) NFrÚreadztransfer-encodingrkcss|]}|jƒVqdS)N)rC)rDÚencr r r!rQhsz(HTTPResponse.__init__..rGÚchunkedT)Údecode_content)!r`rÚheadersÚstatusÚversionÚreasonÚstrictroÚ_has_decoded_contentÚretriesÚenforce_content_lengthÚ_decoderÚ_bodyÚ_fpÚ_original_responseÚ_fp_bytes_readÚmsgÚ _request_urlÚ basestringr<Ú_poolÚ _connectionÚhasattrrnÚ chunk_leftriÚlowerrJÚ _init_lengthÚlength_remainingrVÚ_decoded_bufferrl)rÚbodyrprqrrrsrtZpreload_contentroÚoriginal_responseZpoolÚ connectionr}rvrwÚrequest_methodZ request_urlZtr_encrMr r r!r"?sD    zHTTPResponse.__init__cCs|j|jkr|jjdƒ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)rqÚREDIRECT_STATUSESrpri)rr r r!Úget_redirect_locationvs  z"HTTPResponse.get_redirect_locationcCs,|j s|j rdS|jj|jƒd|_dS)N)r€r�Z _put_conn)rr r r!Ú release_connƒszHTTPResponse.release_conncCs"|jr |jS|jr|jdd�SdS)NT)Ú cache_content)ryrzrl)rr r r!r*ŠszHTTPResponse.datacCs|jS)N)r�)rr r r!rŠ“szHTTPResponse.connectioncCs t|jƒS)N)rrz)rr r r!Ú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|)rr r r!ÚtellšszHTTPResponse.tellcCsâ|jjdƒ}|dk rŠ|jr(tjdƒdSy.rGrz8Content-Length contained multiple unmatching values (%s)réÌé0édéÈZHEAD)r•r–) rprirnÚlogZwarningÚsetrJr?rÚpopr_r“rq)rr‹ZlengthZlengthsrqr r r!r…¢s,      (zHTTPResponse._init_lengthcshˆjjddƒjƒ}ˆjdkrd|ˆjkr2t|ƒˆ_n2d|krd‡fdd„|jdƒDƒ}t|ƒrdt|ƒˆ_dS)z= Set-up the _decoder attribute if necessary. zcontent-encodingrkNrGcs"g|]}|jƒˆjkr|jƒ‘qSr )rCÚCONTENT_DECODERS)rDrI)rr r!rFÝsz.HTTPResponse._init_decoder..)rprir„rxrœrHrJr?)rÚcontent_encodingrMr )rr!Ú _init_decoderÒs   zHTTPResponse._init_decodercCsª| r|jrtdƒ‚|dks |r$d }y$|rF|jrF|jj||d�}d|_WnHttjfk r�}z&|jjddƒj ƒ}t d||ƒ‚WYdd}~XnX|r¦|r¦||j ƒ7}|S) zN Decode the data passed in and potentially flush the decoder. z_Calling read(decode_content=False) is not supported after read(decode_content=True) was called.Nr)r&Tzcontent-encodingrkzEReceived response with content-encoding: %s, but failed to decode it.r-) rur^rxr(ÚIOErrorrrrprir„r Ú_flush_decoder)rr*roÚ flush_decoderr&rIr�r r r!Ú_decodeás"     zHTTPResponse._decodecCs$|jr |jjdƒ}||jjƒSdS)zk Flushes the decoder. Should only be called if the decoder is actually being used. r)rxr(rN)rZbufr r r!r ýs zHTTPResponse._flush_decoderccsìd}z¤y dVWn�tk r2t|jddƒ‚Ynptk rn}z"dt|ƒkrP‚t|jddƒ‚WYdd}~Xn4ttfk r }ztd||ƒ‚WYdd}~XnXd}Wd|sÎ|jr¾|jj ƒ|j rÎ|j j ƒ|jræ|jj ƒræ|j ƒ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{Úcloser�r‘r�)rZ clean_exitrIr r r!Ú_error_catchers(      zHTTPResponse._error_catcherc CsÞ|jdkrdSt|jddƒ}|jƒ�~|dkrB|s<|jjƒnd}n|sR|jj|ƒnd}|dk rž|dkrž| rž|jjƒ|jrž|jdk rž|jdkržt|j|jƒ‚WdQRX|rÚ|jt |ƒ7_|jdk rÚ|jt |ƒ8_|S)z7 Reads `amt` of bytes from the socket. NÚclosedFrr) rzr#r§rlr¦rwr†r r|r?)rÚamtZ fp_closedr*r r r!Ú _raw_readAs$      zHTTPResponse._raw_readcCsŒ|jƒ|dkr|j}|dk rrd}|jrX|jjrX|jd|d|t|jƒd�}|jj|ƒt|jƒ|krr|jj|ƒS|j |ƒ}d}|dkrŽd}n|dkr | r d}| rÈt|jƒdkrÈ|joÀ|jj rÈ|S|dkrê|j|||ƒ}|rè||_ nž|�s|j �rt dƒ‚|S|j||||t|jƒd�}|jj|ƒxNt|jƒ|k�rz|�rz|j |ƒ}|j||||t|jƒd�}|jj|ƒ�q.W|jj|ƒ}|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.) NFr)r¡r&Trz_Calling read(decode_content=False) is not supported after read(decode_content=True) was called.)r&) ržrorxr,r¢r?r‡r]rirªryrur^)rr©ror�Z decoded_datar*r¡r r r!rllsb      zHTTPResponse.readr3r8ccsx|jr.|jƒr.xd|j||d�D] }|VqWnFxDt|jƒ sXt|jƒdksX|jrr|jjrr|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. )ror)r©roN) rnÚsupports_chunked_readsÚ read_chunkedrrzr?r‡rxr,rl)rr©roÚliner*r r r!ÚstreamÈs  zHTTPResponse.streamc Ks`|j}t|tƒs,tr"t|jƒƒ}n tj|ƒ}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``. rtr)rˆrprqrrrsrtr‰) r}r`rrÚitemsÚ from_httplibr#rqrrrs)Z ResponseClsÚrZ response_kwrprtZrespr r r!r°æs    zHTTPResponse.from_httplibcCs|jS)N)rp)rr r r!Ú getheadersszHTTPResponse.getheaderscCs|jj||ƒS)N)rpri)rr$Údefaultr r r!Ú getheaderszHTTPResponse.getheadercCs|jS)N)rp)rr r r!Úinfo szHTTPResponse.infocCs$|js|jjƒ|jr |jjƒdS)N)r¨rzr¦r�)rr r r!r¦s zHTTPResponse.closecCs@|jdkrdSt|jdƒr$|jjƒSt|jdƒr8|jjSdSdS)NTr‘r¨)rzr‚r‘r¨)rr r r!r¨s    zHTTPResponse.closedcCs6|jdkrtdƒ‚nt|jdƒr*|jjƒ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Ÿr‚r¶)rr r r!r¶!s     zHTTPResponse.filenocCs$|jdk r t|jdƒr |jjƒSdS)NrN)rzr‚rN)rr r r!rN*szHTTPResponse.flushcCsdS)NTr )rr r r!Úreadable.szHTTPResponse.readablecCs:|jt|ƒƒ}t|ƒdkrdS||dt|ƒ…<t|ƒSdS)Nr)rlr?)rÚbZtempr r r!Úreadinto2s  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‚rz)rr r r!r«;sz#HTTPResponse.supports_chunked_readsc Csf|jdk rdS|jjjƒ}|jddƒd}yt|dƒ|_Wn&tk r`|jƒtj |ƒ‚YnXdS)Nó;rrr8) rƒrzrºÚreadlinerJr“r_r¦Úhttplibr )rr­r r r!Ú_update_chunk_lengthDs  z!HTTPResponse._update_chunk_lengthcCs¬d}|dkr2|jj|jƒ}|}|jjdƒd|_nv||jkrZ|jj|ƒ}|j||_|}nN||jkrˆ|jj|ƒ}|jjdƒd|_|}n |jj|jƒ}|jjdƒd|_|S)Nr3)rzZ _safe_readrƒ)rr©Zreturned_chunkrhÚvaluer r r!Ú _handle_chunkRs&        zHTTPResponse._handle_chunkc csô|jƒ|jstdƒ‚|jƒs&tdƒ‚|jƒ�¼|jrNt|jƒrN|jjƒdS|j j dkr^dSx>|j ƒ|j dkrtP|j |ƒ}|j||d|d�}|r`|Vq`W|r´|jƒ}|r´|Vx |j j jƒ}|sÈP|dkr¶Pq¶W|jræ|jjƒ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)ror¡r&s )ržrnr r«rr§r{rr¦rzrºr¾rƒrÀr¢r r¼)rr©rorhZdecodedr­r r r!r¬hsD        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. Nrr-)rvr?ÚhistoryZredirect_locationr~)rr r r!Úgeturl«szHTTPResponse.geturl)rkNrrNrTTNNNNNFNN)N)N)NNFé)rÃN)N)NN)'r.r/r0rSrœr�r"rŽr�r1r*rŠr‘r’r…ržr¢r rr§rªrlr®Ú classmethodr°r²r´rµr¦r¨r¶rNr·r¹r«r¾rÀr¬rÂr r r r!rjsJ 3  0  9 + \        Crj)3Z __future__rÚ contextlibrrWrrbZloggingÚsysÚwarningsZsocketrr£rr¥Ú _collectionsrÚ exceptionsrr r r r r rrZ packages.sixrrrZpackages.six.movesrr½rŠrrZ util.responserrZ getLoggerr.r™Úobjectrr2r7rArHrVÚIOBaserjr r r r!Ús.     (  <A8 <