#NEWLINE# def decode( self , data = None ) : #NEWLINE# #INDENT# frame = self . frame #NEWLINE# mask_length = frame . _decode_mask_length #NEWLINE# if data : #NEWLINE# #INDENT# self . buffer . extend ( data ) #NEWLINE# #UNINDENT# if ( frame is None ) : #NEWLINE# #INDENT# if ( len ( self . buffer ) < 2 ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# chunk = self . _chunk ( 2 ) #NEWLINE# ( first_byte , second_byte ) = unpack ( 'BB' , chunk ) #NEWLINE# fin = ( ( first_byte >> 7 ) & 1 ) #NEWLINE# opcode = ( first_byte & 15 ) #NEWLINE# if ( fin not in ( 0 , 1 ) ) : #NEWLINE# #INDENT# raise ProtocolError ( 'FIN must be 0 or 1' ) #NEWLINE# #UNINDENT# if ( bool ( mask_length ) != bool ( ( second_byte & 128 ) ) ) : #NEWLINE# #INDENT# if mask_length : #NEWLINE# #INDENT# raise ProtocolError ( 'unmasked client frame.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ProtocolError ( 'masked server frame.' ) #NEWLINE# #UNINDENT# #UNINDENT# payload_length = ( second_byte & 127 ) #NEWLINE# if ( opcode > 7 ) : #NEWLINE# #INDENT# if ( payload_length > 125 ) : #NEWLINE# #INDENT# raise ProtocolError ( 'WEBSOCKET control frame too large' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not fin ) : #NEWLINE# #INDENT# raise ProtocolError ( 'WEBSOCKET control frame fragmented' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# self . frame = frame = Frame ( opcode , bool ( fin ) , payload_length ) #NEWLINE# #UNINDENT# if ( frame . _masking_key is None ) : #NEWLINE# #INDENT# if ( frame . _payload_length == 126 ) : #NEWLINE# #INDENT# if ( len ( self . buffer ) < ( 2 + mask_length ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# chunk = self . _chunk ( 2 ) #NEWLINE# frame . _payload_length = unpack ( '!H' , chunk ) [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( frame . _payload_length == 127 ) : #NEWLINE# #INDENT# if ( len ( self . buffer ) < ( 8 + mask_length ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# chunk = self . _chunk ( 8 ) #NEWLINE# frame . _payload_length = unpack ( '!Q' , chunk ) [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( self . buffer ) < mask_length ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if mask_length : #NEWLINE# #INDENT# frame . _masking_key = self . _chunk ( mask_length ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# frame . _masking_key = b'' #NEWLINE# #UNINDENT# #UNINDENT# if ( len ( self . buffer ) >= frame . _payload_length ) : #NEWLINE# #INDENT# self . frame = None #NEWLINE# chunk = self . _chunk ( frame . _payload_length ) #NEWLINE# if self . _extensions : #NEWLINE# #INDENT# for extension in self . _extensions : #NEWLINE# #INDENT# chunk = extension . receive ( frame , self . buffer ) #NEWLINE# #UNINDENT# #UNINDENT# if frame . _masking_key : #NEWLINE# #INDENT# chunk = websocket_mask ( chunk , frame . _masking_key ) #NEWLINE# #UNINDENT# if ( frame . opcode == 1 ) : #NEWLINE# #INDENT# frame . _body = chunk . decode ( 'utf-8' , 'replace' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# frame . _body = chunk #NEWLINE# #UNINDENT# return frame