<s> import gzip <EOL> import zlib <EOL> import cStringIO <EOL> from base64 import encodestring as b64encode <EOL> import logging <EOL> from . . import dpkt_http_replacement as dpkt_http <EOL> from . . mediatype import MediaType <EOL> from . . import settings <EOL> import common as http <EOL> import message <EOL> try : <EOL> try : <EOL> from BeautifulSoup import UnicodeDammit <EOL> except ImportError : <EOL> from . . BeautifulSoup import UnicodeDammit <EOL> except ImportError : <EOL> UnicodeDammit = None <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class Response ( message . Message ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , tcpdir , pointer ) : <EOL> message . Message . __init__ ( self , tcpdir , pointer , dpkt_http . Response ) <EOL> if '<STR_LIT>' in self . msg . headers : <EOL> self . mediaType = MediaType ( self . msg . headers [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . mediaType = MediaType ( '<STR_LIT>' ) <EOL> self . mimeType = self . mediaType . mimeType ( ) <EOL> self . body_length = len ( self . msg . body ) <EOL> self . compression_amount = None <EOL> self . text = None <EOL> if settings . drop_bodies : <EOL> self . clear_body ( ) <EOL> else : <EOL> self . handle_compression ( ) <EOL> self . handle_text ( ) <EOL> def clear_body ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . body = self . raw_body = None <EOL> self . msg . body = None <EOL> def handle_compression ( self ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in self . msg . headers : <EOL> encoding = self . msg . headers [ '<STR_LIT>' ] . lower ( ) <EOL> self . compression = encoding <EOL> if encoding == '<STR_LIT>' or encoding == '<STR_LIT>' : <EOL> try : <EOL> gzipfile = gzip . GzipFile ( <EOL> fileobj = cStringIO . StringIO ( self . raw_body ) <EOL> ) <EOL> self . body = gzipfile . read ( ) <EOL> except zlib . error : <EOL> raise http . DecodingError ( '<STR_LIT>' ) <EOL> except : <EOL> raise http . DecodingError ( <EOL> '<STR_LIT>' ) <EOL> elif encoding == '<STR_LIT>' : <EOL> try : <EOL> self . body = zlib . decompress ( self . raw_body , - <NUM_LIT:15> ) <EOL> except zlib . error : <EOL> raise http . DecodingError ( <EOL> '<STR_LIT>' ) <EOL> elif encoding == '<STR_LIT>' or encoding == '<STR_LIT>' : <EOL> self . body = self . raw_body <EOL> elif encoding == '<STR_LIT>' : <EOL> self . body = self . raw_body <EOL> elif '<STR_LIT>' in encoding : <EOL> self . body = self . raw_body <EOL> else : <EOL> raise http . DecodingError ( '<STR_LIT>' + encoding ) <EOL> else : <EOL> self . compression = '<STR_LIT>' <EOL> self . body = self . raw_body <EOL> self . body_length = len ( self . body ) <EOL> self . compression_amount = self . body_length - len ( self . raw_body ) <EOL> def handle_text ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . encoding = None <EOL> if ( self . mediaType and <EOL> ( self . mediaType . type == '<STR_LIT:text>' or <EOL> ( self . mediaType . type == '<STR_LIT>' and <EOL> '<STR_LIT>' in self . mediaType . subtype ) ) ) : <EOL> if '<STR_LIT>' in self . mediaType . params : <EOL> override_encodings = [ self . mediaType . params [ '<STR_LIT>' ] ] <EOL> else : <EOL> override_encodings = [ ] <EOL> if self . body != '<STR_LIT>' : <EOL> if UnicodeDammit : <EOL> dammit = UnicodeDammit ( self . body , override_encodings ) <EOL> if dammit . unicode : <EOL> self . text = dammit . unicode <EOL> self . originalEncoding = dammit . originalEncoding <EOL> else : <EOL> pass <EOL> else : <EOL> u = None <EOL> for e in override_encodings + [ '<STR_LIT:utf8>' , '<STR_LIT>' ] : <EOL> try : <EOL> u = self . body . decode ( e , '<STR_LIT:strict>' ) <EOL> self . originalEncoding = e <EOL> break <EOL> except UnicodeError : <EOL> pass <EOL> if not u : <EOL> u = self . body . decode ( '<STR_LIT:utf8>' , '<STR_LIT:replace>' ) <EOL> self . originalEncoding = None <EOL> self . text = u or None <EOL> else : <EOL> self . text = b64encode ( self . body ) <EOL> self . encoding = '<STR_LIT>' <EOL> @ property <EOL> def raw_body_length ( self ) : <EOL> if self . compression_amount is None : <EOL> return self . body_length <EOL> return self . body_length - self . compression_amount </s>
