
) XH                 @   s7  d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z
 d d l m Z d d l m Z m Z m Z m Z d d	 l m Z m Z m Z d d
 l m Z d d l m Z m Z d d l m Z m Z Gd d   d e   Z! Gd d   d e   Z" d d   Z# Gd d   d e j$  Z% d S)    )absolute_import)contextmanagerN)timeout)error   )HTTPHeaderDict)ProtocolErrorDecodeErrorReadTimeoutErrorResponseNotChunked)string_typesbinary_typePY3)http_client)HTTPExceptionBaseSSLError)is_fp_closedis_response_to_headc               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)DeflateDecoderc             C   s(   d |  _  t   |  _ t j   |  _ d  S)NT)
_first_tryr   _datazlibdecompressobj_obj)self r   R/home/ubuntu/projects/ifolica/build/requests/requests/packages/urllib3/response.py__init__   s    	zDeflateDecoder.__init__c             C   s   t  |  j |  S)N)getattrr   )r   namer   r   r   __getattr__   s    zDeflateDecoder.__getattr__c             C   s   | s
 | S|  j  s# |  j j |  S|  j | 7_ y |  j j |  SWnU t j k
 r d |  _  t j t j  |  _ z |  j |  j  SWd  d  |  _ XYn Xd  S)NF)r   r   
decompressr   r   r   r   	MAX_WBITS)r   datar   r   r   r!      s    		zDeflateDecoder.decompressN)__name__
__module____qualname__r   r    r!   r   r   r   r   r      s   r   c               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)GzipDecoderc             C   s   t  j d t  j  |  _ d  S)N   )r   r   r"   r   )r   r   r   r   r   1   s    zGzipDecoder.__init__c             C   s   t  |  j |  S)N)r   r   )r   r   r   r   r   r    4   s    zGzipDecoder.__getattr__c             C   s   | s
 | S|  j  j |  S)N)r   r!   )r   r#   r   r   r   r!   7   s    zGzipDecoder.decompressN)r$   r%   r&   r   r    r!   r   r   r   r   r'   /   s   r'   c             C   s   |  d k r t    St   S)Ngzip)r'   r   )moder   r   r   _get_decoder=   s    r+   c               @   s  e  Z d  Z d Z d d g Z d d d d d g Z d	 d
 d d d
 d d d d
 d
 d
 d d  Z d d   Z d d   Z e	 d d    Z
 e	 d d    Z d d   Z d d   Z d d   Z d d   Z e d d     Z d
 d
 d! d" d#  Z d@ d
 d& d'  Z e d( d)    Z d* d+   Z d
 d, d-  Z d. d/   Z e	 d0 d1    Z d2 d3   Z d4 d5   Z d6 d7   Z d8 d9   Z d: d;   Z d< d=   Z d
 d
 d> d?  Z  d
 S)A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, attempts to decode specific content-encoding's based on headers
        (like 'gzip' and 'deflate') will be skipped and raw data will be used
        instead.

    :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.
    r)   deflatei-  i.  i/  i3  i4   Nr   Tc             C   se  t  | t  r | |  _ n t |  |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  |  _ d  |  _	 d  |  _
 |	 |  _ d |  _ | r t  | t t f  r | |  _	 n  |
 |  _ | |  _ t | d  r | |  _
 n  d |  _ d  |  _ |  j j d d  j   } d d   | j d  D } d	 | k r9d
 |  _ n  | ra|  j	 ra|  j d |  |  _	 n  d  S)Nr   readFztransfer-encodingr.   c             s   s   |  ] } | j    Vq d  S)N)strip).0encr   r   r   	<genexpr>   s    z(HTTPResponse.__init__.<locals>.<genexpr>,chunkedTdecode_content)
isinstancer   headersstatusversionreasonstrictr6   _decoder_body_fp_original_response_fp_bytes_read
basestringr   _pool_connectionhasattrr5   
chunk_leftgetlowersplitr/   )r   bodyr8   r9   r:   r;   r<   preload_contentr6   original_responsepool
connectiontr_enc	encodingsr   r   r   r   a   s6    														zHTTPResponse.__init__c             C   s&   |  j  |  j k r" |  j j d  Sd S)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)r9   REDIRECT_STATUSESr8   rG   )r   r   r   r   get_redirect_location   s    z"HTTPResponse.get_redirect_locationc             C   s8   |  j  s |  j r d  S|  j  j |  j  d  |  _ d  S)N)rC   rD   	_put_conn)r   r   r   r   release_conn   s    zHTTPResponse.release_connc             C   s-   |  j  r |  j  S|  j r) |  j d d  Sd  S)Ncache_contentT)r>   r?   r/   )r   r   r   r   r#      s    		zHTTPResponse.datac             C   s   |  j  S)N)rD   )r   r   r   r   rN      s    zHTTPResponse.connectionc             C   s   |  j  S)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).
        )rA   )r   r   r   r   tell   s    zHTTPResponse.tellc             C   sO   |  j  j d d  j   } |  j d k rK | |  j k rK t |  |  _ n  d S)z<
        Set-up the _decoder attribute if necessar.
        zcontent-encodingr.   N)r8   rG   rH   r=   CONTENT_DECODERSr+   )r   content_encodingr   r   r   _init_decoder   s    zHTTPResponse._init_decoderc             C   s   y( | r' |  j  r' |  j  j |  } n  Wn\ t t j f k
 r } z3 |  j j d d  j   } t d | |   WYd d } ~ Xn X| r | r | |  j	   7} n  | S)zN
        Decode the data passed in and potentially flush the decoder.
        zcontent-encodingr.   zEReceived response with content-encoding: %s, but failed to decode it.N)
r=   r!   IOErrorr   r   r8   rG   rH   r	   _flush_decoder)r   r#   r6   flush_decodererY   r   r   r   _decode   s     zHTTPResponse._decodec             C   s0   |  j  r, |  j  j d  } | |  j  j   Sd S)zk
        Flushes the decoder. Should only be called if the decoder is actually
        being used.
            )r=   r!   flush)r   bufr   r   r   r\      s    	zHTTPResponse._flush_decoderc             c   s9  d } z y	 d VWn t  k
 r; t |  j d d   Yn t k
 r } z2 d t |  k re   n  t |  j d d   WYd d } ~ Xn> t t f k
 r } z t d | |   WYd d } ~ Xn Xd } Wd | s|  j r |  j j	   n  |  j
 r|  j
 j	   qn  |  j r4|  j j   r4|  j   n  Xd S)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.
        FNzRead timed out.zread operation timed outzConnection broken: %rT)SocketTimeoutr
   rC   r   strr   SocketErrorr   r@   closerD   isclosedrU   )r   Z
clean_exitr^   r   r   r   _error_catcher   s(    		'&
		zHTTPResponse._error_catcherFc          
   C   s  |  j    | d k r" |  j } n  |  j d k r5 d Sd } d } |  j   j | d k rr |  j j   } d } nA d } |  j j |  } | d k r | r |  j j   d } n  Wd QX| r |  j t |  7_ |  j | | |  } | r | |  _	 q n  | 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   )
rZ   r6   r?   rh   r/   rf   rA   lenr_   r>   )r   amtr6   rV   r]   r#   r   r   r   r/     s,    
	zHTTPResponse.read   r(   c             c   sq   |  j  r1 xa |  j | d | D] } | Vq Wn< x9 t |  j  sl |  j d | d |  } | r4 | Vq4 q4 Wd S)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.
        r6   rj   N)r5   read_chunkedr   r?   r/   )r   rj   r6   liner#   r   r   r   streamP  s    	zHTTPResponse.streamc             K   s   | j  } t | t  sE t r3 t | j    } qE t j |  } n  t | d d  } |  d | d | d | j d | j d | j	 d | 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   rJ   r8   r9   r:   r;   rL   )
msgr7   r   r   itemsfrom_httplibr   r9   r:   r;   )ResponseClsrresponse_kwr8   r<   respr   r   r   rq   j  s    						zHTTPResponse.from_httplibc             C   s   |  j  S)N)r8   )r   r   r   r   
getheaders  s    zHTTPResponse.getheadersc             C   s   |  j  j | |  S)N)r8   rG   )r   r   defaultr   r   r   	getheader  s    zHTTPResponse.getheaderc             C   s6   |  j  s |  j j   n  |  j r2 |  j j   n  d  S)N)closedr?   rf   rD   )r   r   r   r   rf     s    		zHTTPResponse.closec             C   sV   |  j  d  k r d St |  j  d  r/ |  j  j St |  j  d  rN |  j  j   Sd Sd  S)NTry   rg   )r?   rE   ry   rg   )r   r   r   r   ry     s    
zHTTPResponse.closedc             C   sM   |  j  d  k r t d   n+ t |  j  d  r= |  j  j   St d   d  S)Nz-HTTPResponse has no file to get a fileno fromfilenozOThe file-like object this HTTPResponse is wrapped around has no file descriptor)r?   r[   rE   rz   )r   r   r   r   rz     s
    zHTTPResponse.filenoc             C   s2   |  j  d  k	 r. t |  j  d  r. |  j  j   Sd  S)Nra   )r?   rE   ra   )r   r   r   r   ra     s    !zHTTPResponse.flushc             C   s   d S)NTr   )r   r   r   r   readable  s    zHTTPResponse.readablec             C   sO   |  j  t |   } t |  d k r+ d S| | d  t |   <t |  Sd  S)Nr   )r/   ri   )r   btempr   r   r   readinto  s
    zHTTPResponse.readintoc             C   s   |  j  d  k	 r d  S|  j j j   } | j d d  d } y t | d  |  _  Wn+ t k
 r~ |  j   t j	 |   Yn Xd  S)N   ;r   r   r(   )
rF   r?   fpreadlinerI   int
ValueErrorrf   httplibIncompleteRead)r   rm   r   r   r   _update_chunk_length  s    
z!HTTPResponse._update_chunk_lengthc             C   s   d  } | d  k rI |  j  j |  j  } | } |  j  j d  d  |  _ n | |  j k  r |  j  j |  } |  j | |  _ | } nq | |  j k r |  j  j |  } |  j  j d  d  |  _ | } n. |  j  j |  j  } |  j  j d  d  |  _ | S)Nrk   )r?   
_safe_readrF   )r   rj   Zreturned_chunkchunkvaluer   r   r   _handle_chunk  s&    				zHTTPResponse._handle_chunkc             c   s-  |  j    |  j s" t d   n  |  j rK t |  j  rK |  j j   d S|  j    xY |  j   |  j d k rx Pn  |  j	 |  } |  j
 | d | d d } | r[ | Vq[ q[ W| r |  j   } | r | Vq n  x0 |  j j j   } | s Pn  | d k r Pq q W|  j r#|  j j   n  Wd QXd S)z
        Similar to :meth:`HTTPResponse.read`, but with an additional
        parameter: ``decode_content``.

        :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.Nr   r6   r]   Fs   
)rZ   r5   r   r@   r   rf   rh   r   rF   r   r_   r\   r?   r   r   )r   rj   r6   r   decodedrm   r   r   r   rl     s:    	
	
		zHTTPResponse.read_chunkedi   )!r$   r%   r&   __doc__rX   rR   r   rS   rU   propertyr#   rN   rW   rZ   r_   r\   r   rh   r/   rn   classmethodrq   rv   rx   rf   ry   rz   ra   r{   r~   r   r   rl   r   r   r   r   r,   D   s:   	(	
9<		r,   )&
__future__r   
contextlibr   r   iosocketr   rc   r   re   _collectionsr   
exceptionsr   r	   r
   r   Zpackages.sixr   rB   r   r   Zpackages.six.movesr   r   rN   r   r   Zutil.responser   r   objectr   r'   r+   IOBaser,   r   r   r   r   <module>   s   "