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       $