<s> from struct import pack , unpack <EOL> from ws4py . exc import FrameTooLargeException , ProtocolException <EOL> from ws4py . compat import py3k , ord , range <EOL> OPCODE_CONTINUATION = <NUM_LIT> <EOL> OPCODE_TEXT = <NUM_LIT> <EOL> OPCODE_BINARY = <NUM_LIT> <EOL> OPCODE_CLOSE = <NUM_LIT> <EOL> OPCODE_PING = <NUM_LIT> <EOL> OPCODE_PONG = <NUM_LIT> <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Frame ( object ) : <EOL> def __init__ ( self , opcode = None , body = b'<STR_LIT>' , masking_key = None , fin = <NUM_LIT:0> , rsv1 = <NUM_LIT:0> , rsv2 = <NUM_LIT:0> , rsv3 = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( body , bytes ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . opcode = opcode <EOL> self . body = body <EOL> self . masking_key = masking_key <EOL> self . fin = fin <EOL> self . rsv1 = rsv1 <EOL> self . rsv2 = rsv2 <EOL> self . rsv3 = rsv3 <EOL> self . payload_length = len ( body ) <EOL> self . _parser = None <EOL> @ property <EOL> def parser ( self ) : <EOL> if self . _parser is None : <EOL> self . _parser = self . _parsing ( ) <EOL> next ( self . parser ) <EOL> return self . _parser <EOL> def _cleanup ( self ) : <EOL> if self . _parser : <EOL> self . _parser . close ( ) <EOL> self . _parser = None <EOL> def build ( self ) : <EOL> """<STR_LIT>""" <EOL> header = b'<STR_LIT>' <EOL> if self . fin > <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if <NUM_LIT> <= self . opcode <= <NUM_LIT> or <NUM_LIT> <= self . opcode : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> header = pack ( '<STR_LIT>' , ( ( self . fin << <NUM_LIT:7> ) <EOL> | ( self . rsv1 << <NUM_LIT:6> ) <EOL> | ( self . rsv2 << <NUM_LIT:5> ) <EOL> | ( self . rsv3 << <NUM_LIT:4> ) <EOL> | self . opcode ) ) <EOL> if self . masking_key : mask_bit = <NUM_LIT:1> << <NUM_LIT:7> <EOL> else : mask_bit = <NUM_LIT:0> <EOL> length = self . payload_length <EOL> if length < <NUM_LIT> : <EOL> header += pack ( '<STR_LIT>' , ( mask_bit | length ) ) <EOL> elif length < ( <NUM_LIT:1> << <NUM_LIT:16> ) : <EOL> header += pack ( '<STR_LIT>' , ( mask_bit | <NUM_LIT> ) ) + pack ( '<STR_LIT>' , length ) <EOL> elif length < ( <NUM_LIT:1> << <NUM_LIT> ) : <EOL> header += pack ( '<STR_LIT>' , ( mask_bit | <NUM_LIT> ) ) + pack ( '<STR_LIT>' , length ) <EOL> else : <EOL> raise FrameTooLargeException ( ) <EOL> body = self . body <EOL> if not self . masking_key : <EOL> return bytes ( header + body ) <EOL> return bytes ( header + self . masking_key + self . mask ( body ) ) <EOL> def _parsing ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = b'<STR_LIT>' <EOL> some_bytes = b'<STR_LIT>' <EOL> while not some_bytes : <EOL> some_bytes = ( yield <NUM_LIT:1> ) <EOL> first_byte = some_bytes [ <NUM_LIT:0> ] if isinstance ( some_bytes , bytearray ) else ord ( some_bytes [ <NUM_LIT:0> ] ) <EOL> self . fin = ( first_byte >> <NUM_LIT:7> ) & <NUM_LIT:1> <EOL> self . rsv1 = ( first_byte >> <NUM_LIT:6> ) & <NUM_LIT:1> <EOL> self . rsv2 = ( first_byte >> <NUM_LIT:5> ) & <NUM_LIT:1> <EOL> self . rsv3 = ( first_byte >> <NUM_LIT:4> ) & <NUM_LIT:1> <EOL> self . opcode = first_byte & <NUM_LIT> <EOL> if self . rsv1 or self . rsv2 or self . rsv3 : <EOL> raise ProtocolException ( ) <EOL> if <NUM_LIT:2> < self . opcode < <NUM_LIT:8> or self . opcode > <NUM_LIT> : <EOL> raise ProtocolException ( ) <EOL> if self . opcode > <NUM_LIT> and self . fin == <NUM_LIT:0> : <EOL> raise ProtocolException ( ) <EOL> some_bytes = some_bytes [ <NUM_LIT:1> : ] if some_bytes and len ( some_bytes ) > <NUM_LIT:1> else b'<STR_LIT>' <EOL> while not some_bytes : <EOL> some_bytes = ( yield <NUM_LIT:1> ) <EOL> second_byte = some_bytes [ <NUM_LIT:0> ] if isinstance ( some_bytes , bytearray ) else ord ( some_bytes [ <NUM_LIT:0> ] ) <EOL> mask = ( second_byte >> <NUM_LIT:7> ) & <NUM_LIT:1> <EOL> self . payload_length = second_byte & <NUM_LIT> <EOL> if self . opcode > <NUM_LIT> and self . payload_length > <NUM_LIT> : <EOL> raise FrameTooLargeException ( ) <EOL> if some_bytes and len ( some_bytes ) > <NUM_LIT:1> : <EOL> buf = some_bytes [ <NUM_LIT:1> : ] <EOL> some_bytes = buf <EOL> else : <EOL> buf = b'<STR_LIT>' <EOL> some_bytes = b'<STR_LIT>' <EOL> if self . payload_length == <NUM_LIT> : <EOL> if len ( buf ) < <NUM_LIT:8> : <EOL> nxt_buf_size = <NUM_LIT:8> - len ( buf ) <EOL> some_bytes = ( yield nxt_buf_size ) <EOL> some_bytes = buf + ( some_bytes or b'<STR_LIT>' ) <EOL> while len ( some_bytes ) < <NUM_LIT:8> : <EOL> b = ( yield <NUM_LIT:8> - len ( some_bytes ) ) <EOL> if b is not None : <EOL> some_bytes = some_bytes + b <EOL> if len ( some_bytes ) > <NUM_LIT:8> : <EOL> buf = some_bytes [ <NUM_LIT:8> : ] <EOL> some_bytes = some_bytes [ : <NUM_LIT:8> ] <EOL> else : <EOL> some_bytes = buf [ : <NUM_LIT:8> ] <EOL> buf = buf [ <NUM_LIT:8> : ] <EOL> extended_payload_length = some_bytes <EOL> self . payload_length = unpack ( <EOL> '<STR_LIT>' , extended_payload_length ) [ <NUM_LIT:0> ] <EOL> if self . payload_length > <NUM_LIT> : <EOL> raise FrameTooLargeException ( ) <EOL> elif self . payload_length == <NUM_LIT> : <EOL> if len ( buf ) < <NUM_LIT:2> : <EOL> nxt_buf_size = <NUM_LIT:2> - len ( buf ) <EOL> some_bytes = ( yield nxt_buf_size ) <EOL> some_bytes = buf + ( some_bytes or b'<STR_LIT>' ) <EOL> while len ( some_bytes ) < <NUM_LIT:2> : <EOL> b = ( yield <NUM_LIT:2> - len ( some_bytes ) ) <EOL> if b is not None : <EOL> some_bytes = some_bytes + b <EOL> if len ( some_bytes ) > <NUM_LIT:2> : <EOL> buf = some_bytes [ <NUM_LIT:2> : ] <EOL> some_bytes = some_bytes [ : <NUM_LIT:2> ] <EOL> else : <EOL> some_bytes = buf [ : <NUM_LIT:2> ] <EOL> buf = buf [ <NUM_LIT:2> : ] <EOL> extended_payload_length = some_bytes <EOL> self . payload_length = unpack ( <EOL> '<STR_LIT>' , extended_payload_length ) [ <NUM_LIT:0> ] <EOL> if mask : <EOL> if len ( buf ) < <NUM_LIT:4> : <EOL> nxt_buf_size = <NUM_LIT:4> - len ( buf ) <EOL> some_bytes = ( yield nxt_buf_size ) <EOL> some_bytes = buf + ( some_bytes or b'<STR_LIT>' ) <EOL> while not some_bytes or len ( some_bytes ) < <NUM_LIT:4> : <EOL> b = ( yield <NUM_LIT:4> - len ( some_bytes ) ) <EOL> if b is not None : <EOL> some_bytes = some_bytes + b <EOL> if len ( some_bytes ) > <NUM_LIT:4> : <EOL> buf = some_bytes [ <NUM_LIT:4> : ] <EOL> else : <EOL> some_bytes = buf [ : <NUM_LIT:4> ] <EOL> buf = buf [ <NUM_LIT:4> : ] <EOL> self . masking_key = some_bytes <EOL> if len ( buf ) < self . payload_length : <EOL> nxt_buf_size = self . payload_length - len ( buf ) <EOL> some_bytes = ( yield nxt_buf_size ) <EOL> some_bytes = buf + ( some_bytes or b'<STR_LIT>' ) <EOL> while len ( some_bytes ) < self . payload_length : <EOL> l = self . payload_length - len ( some_bytes ) <EOL> b = ( yield l ) <EOL> if b is not None : <EOL> some_bytes = some_bytes + b <EOL> else : <EOL> if self . payload_length == len ( buf ) : <EOL> some_bytes = buf <EOL> else : <EOL> some_bytes = buf [ : self . payload_length ] <EOL> self . body = some_bytes <EOL> yield <EOL> def mask ( self , data ) : <EOL> """<STR_LIT>""" <EOL> masked = bytearray ( data ) <EOL> if py3k : key = self . masking_key <EOL> else : key = map ( ord , self . masking_key ) <EOL> for i in range ( len ( data ) ) : <EOL> masked [ i ] = masked [ i ] ^ key [ i % <NUM_LIT:4> ] <EOL> return masked <EOL> unmask = mask </s>
