Your IP : 3.129.71.13


Current Path : /usr/lib/python3.6/site-packages/urllib3/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/urllib3/__pycache__/response.cpython-36.opt-1.pyc

3

nf�\	d�@sddlmZddlmZddlZddlZddlZddlmZ	ddlm
Zddlm
Z
ddlmZmZmZmZmZmZmZdd	lmZmZdd
lmZddlmZmZddl m!Z!m"Z"ej#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|_tj�|_dS)NT�)�
_first_try�_data�zlib�
decompressobj�_obj)�self�r�/usr/lib/python3.6/response.py�__init__szDeflateDecoder.__init__cCst|j|�S)N)�getattrr)r�namerrr�__getattr__szDeflateDecoder.__getattr__cCs�|s|S|js|jj|�S|j|7_y |jj|�}|rFd|_d|_|Stjk
r�d|_tjtj�|_z|j|j�Sd|_XYnXdS)NF)rr�
decompressrrrr�	MAX_WBITS)r�dataZ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)�GzipDecodercCstjdtj�|_tj|_dS)N�)rrr%rr*r,�_state)rrrrr @szGzipDecoder.__init__cCst|j|�S)N)r!r)rr"rrrr#DszGzipDecoder.__getattr__cCs�t�}|jtjks|r t|�Sx�y||jj|�7}Wn8tjk
rn|j}tj|_|tj	krht|�S�YnX|jj
}|s�t|�Stj	|_tjdtj�|_q"WdS)Nr0)
�	bytearrayr1r*r.�bytesrr$rrr-Zunused_datarr%)rr&�retZprevious_staterrrr$Gs"
zGzipDecoder.decompressN)r'r(r)r r#r$rrrrr/>sr/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�|jd�D�|_dS)NcSsg|]}t|j���qSr)�_get_decoder�strip)�.0�mrrr�
<listcomp>gsz)MultiDecoder.__init__.<locals>.<listcomp>�,)�split�	_decoders)rZmodesrrrr fszMultiDecoder.__init__cCs|jdj�S)Nr)r=�flush)rrrrr>iszMultiDecoder.flushcCs"xt|j�D]}|j|�}qW|S)N)�reversedr=r$)rr&�drrrr$lszMultiDecoder.decompressN)r'r(r)�__doc__r r>r$rrrrr5]sr5cCs$d|krt|�S|dkrt�St�S)Nr;�gzip)r5r/r)�moderrrr6rs
r6c@s,eZdZdZddgZdddddgZdJdd�Zdd�Zdd�Ze	dd��Z
e	dd��Zdd�Zdd�Z
dd�Zdd�Zd d!�Zd"d#�Zed$d%��ZdKd&d'�ZdMd*d+�Zed,d-��Zd.d/�ZdNd0d1�Zd2d3�Zd4d5�Ze	d6d7��Zd8d9�Zd:d;�Zd<d=�Zd>d?�Z d@dA�Z!dBdC�Z"dDdE�Z#dOdFdG�Z$dHdI�Z%d
S)P�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.
    rBZdeflatei-i.i/i3i4�NrTFcCst|t�r||_n
t|�|_||_||_||_||_||_|
|_||_	d|_
d|_d|_|	|_
d|_||_||_|r�t|ttf�r�||_|
|_||_t|d�r�||_d|_d|_|jjdd�j�}dd�|jd�D�}d	|kr�d
|_|j|�|_|o�|j�r|j|d�|_dS)Nr�readFztransfer-encodingrEcss|]}|j�VqdS)N)r7)r8�encrrr�	<genexpr>�sz(HTTPResponse.__init__.<locals>.<genexpr>r;�chunkedT)�decode_content)�
isinstancer�headers�status�version�reason�strictrJ�retries�enforce_content_length�_decoder�_body�_fp�_original_response�_fp_bytes_read�msg�_request_url�
basestringr3�_pool�_connection�hasattrrI�
chunk_left�get�lowerr<�_init_length�length_remainingrF)r�bodyrLrMrNrOrPZpreload_contentrJ�original_responseZpool�
connectionrXrQrR�request_methodZrequest_urlZtr_enc�	encodingsrrrr �s@


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)rM�REDIRECT_STATUSESrLr_)rrrr�get_redirect_location�sz"HTTPResponse.get_redirect_locationcCs,|js|jrdS|jj|j�d|_dS)N)r[r\Z	_put_conn)rrrr�release_conn�szHTTPResponse.release_conncCs"|jr|jS|jr|jdd�SdS)NT)�
cache_content)rTrUrF)rrrrr&�szHTTPResponse.datacCs|jS)N)r\)rrrrre�szHTTPResponse.connectioncCs
t|j�S)N)rrU)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).
        )rW)rrrr�tell�szHTTPResponse.tellcCs�|jjd�}|dk	r�|jr(tjd�dSy<tdd�|jd�D��}t|�dkrZtd|��|j	�}Wnt
k
r|d}YnX|d	kr�d}yt|j�}Wnt
k
r�d	}YnX|dks�d|ko�d
kns�|dkr�d	}|S)zM
        Set initial length value for Response content if available.
        zcontent-lengthNz�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.cSsg|]}t|��qSr)�int)r8�valrrrr:sz-HTTPResponse._init_length.<locals>.<listcomp>r;rz8Content-Length contained multiple unmatching values (%s)r���0�d��ZHEAD)rqrr)
rLr_rI�logZwarning�setr<�lenr�pop�
ValueErrorrorM)rrfZlengthZlengthsrMrrrra�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-encodingrENr;cs"g|]}|j��jkr|j��qSr)r7�CONTENT_DECODERS)r8�e)rrrr::sz.HTTPResponse._init_decoder.<locals>.<listcomp>)rLr_r`rSrzr6r<rw)r�content_encodingrgr)rr�
_init_decoder/s

zHTTPResponse._init_decodercCs|y|r|jr|jj|�}WnHttjfk
rb}z&|jjdd�j�}td||��WYdd}~XnX|rx|rx||j	�7}|S)zN
        Decode the data passed in and potentially flush the decoder.
        zcontent-encodingrEzEReceived response with content-encoding: %s, but failed to decode it.N)
rSr$�IOErrorrrrLr_r`r
�_flush_decoder)rr&rJ�
flush_decoderr{r|rrr�_decode>s
zHTTPResponse._decodecCs$|jr |jjd�}||jj�SdS)zk
        Flushes the decoder. Should only be called if the decoder is actually
        being used.
        r)rSr$r>)rZbufrrrrPs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)
�
SocketTimeoutrr[r�strr�SocketErrorr	rV�closer\rmrk)rZ
clean_exitr{rrr�_error_catcher[s(	
 

zHTTPResponse._error_catchercCs�|j�|dkr|j}|jdkr$dSd}d}|j��h|dkrN|jj�}d}nJd}|jj|�}|dkr�|r�|jj�d}|jr�|jdkr�t|j	|j��WdQRX|r�|j	t
|�7_	|jdk	r�|jt
|�8_|j|||�}|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}rJrUr�rFr�rRrbr
rWrwr�rT)r�amtrJrlr�r&rrrrF�s4




zHTTPResponse.readr+r0ccsZ|jr.|j�r.xF|j||d�D]
}|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.
        )rJ)r�rJN)rI�supports_chunked_reads�read_chunkedrrUrF)rr�rJ�liner&rrr�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``.
        rPr)rcrLrMrNrOrPrd)
rXrKrr�items�from_httplibr!rMrNrO)ZResponseCls�rZresponse_kwrLrPZresprrrr��s	

zHTTPResponse.from_httplibcCs|jS)N)rL)rrrr�
getheadersszHTTPResponse.getheaderscCs|jj||�S)N)rLr_)rr"�defaultrrr�	getheaderszHTTPResponse.getheadercCs|jS)N)rL)rrrr�infoszHTTPResponse.infocCs$|js|jj�|jr |jj�dS)N)�closedrUr�r\)rrrrr�s
zHTTPResponse.closecCs@|jdkrdSt|jd�r$|jj�St|jd�r8|jjSdSdS)NTrmr�)rUr]rmr�)rrrrr�#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)rUr~r]r�)rrrrr�.s



zHTTPResponse.filenocCs$|jdk	r t|jd�r |jj�SdS)Nr>)rUr]r>)rrrrr>7szHTTPResponse.flushcCsdS)NTr)rrrr�readable;szHTTPResponse.readablecCs:|jt|��}t|�dkrdS||dt|��<t|�SdS)Nr)rFrw)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]rU)rrrrr�Hsz#HTTPResponse.supports_chunked_readscCsf|jdk	rdS|jjj�}|jdd�d}yt|d�|_Wn&tk
r`|j�tj	|��YnXdS)N�;rrr0)
r^rUr��readliner<roryr��httplibr
)rr�rrr�_update_chunk_lengthQs
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)Nr+)rUZ
_safe_readr^)rr�Zreturned_chunk�chunk�valuerrr�
_handle_chunk_s&

zHTTPResponse._handle_chunkccs�|j�|jstd��|j�s&td��|j���|jrNt|j�rN|jj�dS|j	j
dkr^dSx<|j�|jdkrtP|j
|�}|j||dd�}|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)rJr�s
)r}rIrr�rr�rVrr�rUr�r�r^r�r�rr�)rr�rJr�Zdecodedr�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.
        Nr���)rQrw�historyZredirect_locationrY)rrrr�geturl�szHTTPResponse.geturl)rENrrNrTTNNNNNFNN)NNF�)r�N)N)NN)&r'r(r)rArzrir rjrk�propertyr&rermrnrar}r�rrr�rFr��classmethodr�r�r�r�r�r�r�r>r�r�r�r�r�r�r�rrrrrD|sH
/
	09
E

			
CrD).Z
__future__r�
contextlibrr�ioZloggingZsocketrr�rr��_collectionsr�
exceptionsrr	r
rrr
rZpackages.sixrrZrZpackages.six.movesrr�rerrZ
util.responserrZ	getLoggerr'ru�objectrr*r/r5r6�IOBaserDrrrr�<module>s&$
!

?>