<s> import logging <EOL> import zlib <EOL> import io <EOL> from . _collections import HTTPHeaderDict <EOL> from . exceptions import DecodeError <EOL> from . packages . six import string_types as basestring , binary_type <EOL> from . util import is_fp_closed <EOL> log = logging . getLogger ( __name__ ) <EOL> class DeflateDecoder ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _first_try = True <EOL> self . _data = binary_type ( ) <EOL> self . _obj = zlib . decompressobj ( ) <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . _obj , name ) <EOL> def decompress ( self , data ) : <EOL> if not self . _first_try : <EOL> return self . _obj . decompress ( data ) <EOL> self . _data += data <EOL> try : <EOL> return self . _obj . decompress ( data ) <EOL> except zlib . error : <EOL> self . _first_try = False <EOL> self . _obj = zlib . decompressobj ( - zlib . MAX_WBITS ) <EOL> try : <EOL> return self . decompress ( self . _data ) <EOL> finally : <EOL> self . _data = None <EOL> def _get_decoder ( mode ) : <EOL> if mode == '<STR_LIT>' : <EOL> return zlib . decompressobj ( <NUM_LIT:16> + zlib . MAX_WBITS ) <EOL> return DeflateDecoder ( ) <EOL> class HTTPResponse ( io . IOBase ) : <EOL> """<STR_LIT>""" <EOL> CONTENT_DECODERS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> REDIRECT_STATUSES = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def __init__ ( self , body = '<STR_LIT>' , headers = None , status = <NUM_LIT:0> , version = <NUM_LIT:0> , reason = None , <EOL> strict = <NUM_LIT:0> , preload_content = True , decode_content = True , <EOL> original_response = None , pool = None , connection = None ) : <EOL> self . headers = HTTPHeaderDict ( ) <EOL> if headers : <EOL> self . headers . update ( headers ) <EOL> self . status = status <EOL> self . version = version <EOL> self . reason = reason <EOL> self . strict = strict <EOL> self . decode_content = decode_content <EOL> self . _decoder = None <EOL> self . _body = body if body and isinstance ( body , basestring ) else None <EOL> self . _fp = None <EOL> self . _original_response = original_response <EOL> self . _fp_bytes_read = <NUM_LIT:0> <EOL> self . _pool = pool <EOL> self . _connection = connection <EOL> if hasattr ( body , '<STR_LIT>' ) : <EOL> self . _fp = body <EOL> if preload_content and not self . _body : <EOL> self . _body = self . read ( decode_content = decode_content ) <EOL> def get_redirect_location ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . status in self . REDIRECT_STATUSES : <EOL> return self . headers . get ( '<STR_LIT:location>' ) <EOL> return False <EOL> def release_conn ( self ) : <EOL> if not self . _pool or not self . _connection : <EOL> return <EOL> self . _pool . _put_conn ( self . _connection ) <EOL> self . _connection = None <EOL> @ property <EOL> def data ( self ) : <EOL> if self . _body : <EOL> return self . _body <EOL> if self . _fp : <EOL> return self . read ( cache_content = True ) <EOL> def tell ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fp_bytes_read <EOL> def read ( self , amt = None , decode_content = None , cache_content = False ) : <EOL> """<STR_LIT>""" <EOL> content_encoding = self . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> if self . _decoder is None : <EOL> if content_encoding in self . CONTENT_DECODERS : <EOL> self . _decoder = _get_decoder ( content_encoding ) <EOL> if decode_content is None : <EOL> decode_content = self . decode_content <EOL> if self . _fp is None : <EOL> return <EOL> flush_decoder = False <EOL> try : <EOL> if amt is None : <EOL> data = self . _fp . read ( ) <EOL> flush_decoder = True <EOL> else : <EOL> cache_content = False <EOL> data = self . _fp . read ( amt ) <EOL> if amt != <NUM_LIT:0> and not data : <EOL> self . _fp . close ( ) <EOL> flush_decoder = True <EOL> self . _fp_bytes_read += len ( data ) <EOL> try : <EOL> if decode_content and self . _decoder : <EOL> data = self . _decoder . decompress ( data ) <EOL> except ( IOError , zlib . error ) as e : <EOL> raise DecodeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % content_encoding , <EOL> e ) <EOL> if flush_decoder and decode_content and self . _decoder : <EOL> buf = self . _decoder . decompress ( binary_type ( ) ) <EOL> data += buf + self . _decoder . flush ( ) <EOL> if cache_content : <EOL> self . _body = data <EOL> return data <EOL> finally : <EOL> if self . _original_response and self . _original_response . isclosed ( ) : <EOL> self . release_conn ( ) <EOL> def stream ( self , amt = <NUM_LIT:2> ** <NUM_LIT:16> , decode_content = None ) : <EOL> """<STR_LIT>""" <EOL> while not is_fp_closed ( self . _fp ) : <EOL> data = self . read ( amt = amt , decode_content = decode_content ) <EOL> if data : <EOL> yield data <EOL> @ classmethod <EOL> def from_httplib ( ResponseCls , r , ** response_kw ) : <EOL> """<STR_LIT>""" <EOL> headers = HTTPHeaderDict ( ) <EOL> for k , v in r . getheaders ( ) : <EOL> headers . add ( k , v ) <EOL> strict = getattr ( r , '<STR_LIT:strict>' , <NUM_LIT:0> ) <EOL> return ResponseCls ( body = r , <EOL> headers = headers , <EOL> status = r . status , <EOL> version = r . version , <EOL> reason = r . reason , <EOL> strict = strict , <EOL> original_response = r , <EOL> ** response_kw ) <EOL> def getheaders ( self ) : <EOL> return self . headers <EOL> def getheader ( self , name , default = None ) : <EOL> return self . headers . get ( name , default ) <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . _fp . close ( ) <EOL> @ property <EOL> def closed ( self ) : <EOL> if self . _fp is None : <EOL> return True <EOL> elif hasattr ( self . _fp , '<STR_LIT>' ) : <EOL> return self . _fp . closed <EOL> elif hasattr ( self . _fp , '<STR_LIT>' ) : <EOL> return self . _fp . isclosed ( ) <EOL> else : <EOL> return True <EOL> def fileno ( self ) : <EOL> if self . _fp is None : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> elif hasattr ( self . _fp , "<STR_LIT>" ) : <EOL> return self . _fp . fileno ( ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def flush ( self ) : <EOL> if self . _fp is not None and hasattr ( self . _fp , '<STR_LIT>' ) : <EOL> return self . _fp . flush ( ) <EOL> def readable ( self ) : <EOL> return True </s>
