Esempio n. 1
0
 def _write_frame(self, fin, opcode, data, flags=0):
     if fin:
         finbit = self.FIN
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode | flags)
     l = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if l < 126:
         frame += struct.pack("B", l | mask_bit)
     elif l <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, l)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, l)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self._wire_bytes_out += len(frame)
     try:
         self.stream.write(frame)
     except StreamClosedError:
         self._abort()
Esempio n. 2
0
 def _write_frame(self, fin, opcode, data, flags=0):
     if fin:
         finbit = self.FIN
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode | flags)
     l = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if l < 126:
         frame += struct.pack("B", l | mask_bit)
     elif l <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, l)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, l)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self._wire_bytes_out += len(frame)
     try:
         return self.stream.write(frame)
     except StreamClosedError:
         self._abort()
Esempio n. 3
0
 def _write_frame(self, fin, opcode, data, flags=0):
     data_len = len(data)
     if opcode & 0x8:
         # All control frames MUST have a payload length of 125
         # bytes or less and MUST NOT be fragmented.
         if not fin:
             raise ValueError("control frames may not be fragmented")
         if data_len > 125:
             raise ValueError("control frame payloads may not exceed 125 bytes")
     if fin:
         finbit = self.FIN
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode | flags)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if data_len < 126:
         frame += struct.pack("B", data_len | mask_bit)
     elif data_len <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, data_len)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, data_len)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self._wire_bytes_out += len(frame)
     return self.stream.write(frame)
 def _write_frame(self, fin, opcode, data):
     if fin:
         finbit = 0x80
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode)
     l = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if l < 126:
         frame += struct.pack("B", l | mask_bit)
     elif l <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, l)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, l)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self.stream.write(frame)
Esempio n. 5
0
 def _write_frame(self, fin, opcode, data):
     if fin:
         finbit = 0x80
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode)
     l = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if l < 126:
         frame += struct.pack("B", l | mask_bit)
     elif l <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, l)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, l)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self.stream.write(frame)
Esempio n. 6
0
 def _write_frame(self, fin, opcode, data, flags=0):
     if fin:
         finbit = self.FIN
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode | flags)
     data_len = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if data_len < 126:
         frame += struct.pack("B", data_len | mask_bit)
     elif data_len <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, data_len)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, data_len)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self._wire_bytes_out += len(frame)
     return self.stream.write(frame)
Esempio n. 7
0
 def _write_frame(self, fin, opcode, data, flags=0):
     if fin:
         finbit = self.FIN
     else:
         finbit = 0
     frame = struct.pack("B", finbit | opcode | flags)
     data_len = len(data)
     if self.mask_outgoing:
         mask_bit = 0x80
     else:
         mask_bit = 0
     if data_len < 126:
         frame += struct.pack("B", data_len | mask_bit)
     elif data_len <= 0xFFFF:
         frame += struct.pack("!BH", 126 | mask_bit, data_len)
     else:
         frame += struct.pack("!BQ", 127 | mask_bit, data_len)
     if self.mask_outgoing:
         mask = os.urandom(4)
         data = mask + _websocket_mask(mask, data)
     frame += data
     self._wire_bytes_out += len(frame)
     return self.stream.write(frame)
 def _on_masked_frame_data(self, data):
     self._on_frame_data(_websocket_mask(self._frame_mask, data))
Esempio n. 9
0
 def _on_masked_frame_data(self, data):
     # Don't touch _wire_bytes_in; we'll do it in _on_frame_data.
     self._on_frame_data(_websocket_mask(self._frame_mask, data))
Esempio n. 10
0
 def _on_masked_frame_data(self, data):
     # Don't touch _wire_bytes_in; we'll do it in _on_frame_data.
     self._on_frame_data(_websocket_mask(self._frame_mask, data))
Esempio n. 11
0
 def _on_masked_frame_data(self, data):
     self._on_frame_data(_websocket_mask(self._frame_mask, data))
Esempio n. 12
0
    def _receive_frame(self):
        # Read the frame header.
        data = yield self._read_bytes(2)
        header, mask_payloadlen = struct.unpack("BB", data)
        is_final_frame = header & self.FIN
        reserved_bits = header & self.RSV_MASK
        opcode = header & self.OPCODE_MASK
        opcode_is_control = opcode & 0x8
        if self._decompressor is not None and opcode != 0:
            # Compression flag is present in the first frame's header,
            # but we can't decompress until we have all the frames of
            # the message.
            self._frame_compressed = bool(reserved_bits & self.RSV1)
            reserved_bits &= ~self.RSV1
        if reserved_bits:
            # client is using as-yet-undefined extensions; abort
            self._abort()
            return
        is_masked = bool(mask_payloadlen & 0x80)
        payloadlen = mask_payloadlen & 0x7f

        # Parse and validate the length.
        if opcode_is_control and payloadlen >= 126:
            # control frames must have payload < 126
            self._abort()
            return
        if payloadlen < 126:
            self._frame_length = payloadlen
        elif payloadlen == 126:
            data = yield self._read_bytes(2)
            payloadlen = struct.unpack("!H", data)[0]
        elif payloadlen == 127:
            data = yield self._read_bytes(8)
            payloadlen = struct.unpack("!Q", data)[0]
        new_len = payloadlen
        if self._fragmented_message_buffer is not None:
            new_len += len(self._fragmented_message_buffer)
        if new_len > (self.handler.max_message_size or 10 * 1024 * 1024):
            self.close(1009, "message too big")
            self._abort()
            return

        # Read the payload, unmasking if necessary.
        if is_masked:
            self._frame_mask = yield self._read_bytes(4)
        data = yield self._read_bytes(payloadlen)
        if is_masked:
            data = _websocket_mask(self._frame_mask, data)

        # Decide what to do with this frame.
        if opcode_is_control:
            # control frames may be interleaved with a series of fragmented
            # data frames, so control frames must not interact with
            # self._fragmented_*
            if not is_final_frame:
                # control frames must not be fragmented
                self._abort()
                return
        elif opcode == 0:  # continuation frame
            if self._fragmented_message_buffer is None:
                # nothing to continue
                self._abort()
                return
            self._fragmented_message_buffer += data
            if is_final_frame:
                opcode = self._fragmented_message_opcode
                data = self._fragmented_message_buffer
                self._fragmented_message_buffer = None
        else:  # start of new data message
            if self._fragmented_message_buffer is not None:
                # can't start new message until the old one is finished
                self._abort()
                return
            if not is_final_frame:
                self._fragmented_message_opcode = opcode
                self._fragmented_message_buffer = data

        if is_final_frame:
            handled_future = self._handle_message(opcode, data)
            if handled_future is not None:
                yield handled_future