Exemplo n.º 1
0
 def close(self, code=1000, reason=''):
     """
     Returns a close control message built from
     a :class:`ws4py.messaging.CloseControlMessage` instance,
     using the given status ``code`` and ``reason`` message.
     """
     return CloseControlMessage(code=code, reason=reason)
Exemplo n.º 2
0
    def close(self, code=1000, reason=''):
        """
        Returns a close control message built from
        a messaging.CloseControlMessage instance.

        @param code: closing status code
        @param reason: status message
        @return: bytes representing a close control single framed message
        """
        return CloseControlMessage(code=code, reason=reason).single()
Exemplo n.º 3
0
    def test_closing_message_received(self):
        s = MagicMock()
        m = MagicMock()
        c = MagicMock()

        ws = WebSocket(sock=m)
        with patch.multiple(ws, close=c):
            ws.stream = s
            ws.stream.closing = CloseControlMessage(code=1000,
                                                    reason='test closing')
            ws.process(b'unused for this test')
            c.assert_called_once_with(1000, b'test closing')
Exemplo n.º 4
0
    def test_terminate_with_closing(self):
        m = MagicMock()
        s = MagicMock()
        c = MagicMock()
        cc = MagicMock()

        ws = WebSocket(sock=m)
        with patch.multiple(ws, closed=c, close_connection=cc):
            ws.stream = s
            ws.stream.closing = CloseControlMessage(code=1000,
                                                    reason='test closing')
            ws.terminate()
            self.assertTrue(ws.client_terminated)
            self.assertTrue(ws.server_terminated)
            self.assertTrue(ws.terminated)
            c.assert_called_once_with(1000, b'test closing')
            cc.assert_called_once_with()
            self.assertIsNone(ws.stream)
            self.assertIsNone(ws.environ)
Exemplo n.º 5
0
    def receiver(self):
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a framing.Frame object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises StopIteration, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """
        running = True
        while running:
            frame = Frame()
            while True:
                try:
                    bytes = (yield frame.parser.next())
                    if bytes is None:
                        raise InvalidBytesError()
                    
                    frame.parser.send(bytes)
                except StopIteration:
                    bytes = frame.body or ''
                    if frame.masking_key and bytes:
                        bytes = frame.unmask(bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            self.errors.append(CloseControlMessage(code=1002))
                            break
                            
                        try:
                            m = TextMessage(bytes.decode("utf-8"))
                            m.completed = (frame.fin == 1)
                            self.message = m
                        except UnicodeDecodeError:
                            self.errors.append(CloseControlMessage(code=1007))
                            break

                    elif frame.opcode == OPCODE_BINARY:
                        m = BinaryMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                    elif frame.opcode == OPCODE_CONTINUATION:
                        m = self.message
                        if m is None:
                            self.errors.append(CloseControlMessage(code=1002))
                            break
                        
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            try:
                                m.extend(bytes.decode("utf-8"))
                            except UnicodeDecodeError:
                                self.errors.append(CloseControlMessage(code=1007))
                                break
                        else:
                            m.extend(bytes)

                    elif frame.opcode == OPCODE_CLOSE:
                        self.closing = CloseControlMessage(reason=bytes.decode("utf-8", "replace"))
                        
                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(bytes))
                    
                    else:
                        self.errors.append(CloseControlMessage(code=1003))

                    # When the frame's payload is empty, we must yield
                    # once more so that the caller is properly aligned
                    if not bytes:
                        yield 0

                    break

                except ProtocolException:
                    self.errors.append(CloseControlMessage(code=1002))
                except FrameTooLargeException:
                    self.errors.append(CloseControlMessage(code=1004))
                except StreamClosed:
                    running = False
                    break
                
            frame.parser.close()
Exemplo n.º 6
0
    def receiver(self):
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a :class:`wspy.framing.Frame` object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises :exc:`StopIteration`, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """
        utf8validator = Utf8Validator()
        running = True
        frame = None
        while running:
            frame = Frame()
            while 1:
                try:
                    some_bytes = (yield next(frame.parser))
                    frame.parser.send(some_bytes)
                except GeneratorExit:
                    running = False
                    break
                except StopIteration:
                    frame._cleanup()
                    some_bytes = frame.body

                    # Let's avoid unmasking when there is no payload
                    if some_bytes:
                        if frame.masking_key and self.expect_masking:
                            some_bytes = frame.unmask(some_bytes)
                        elif not frame.masking_key and self.expect_masking:
                            msg = CloseControlMessage(
                                code=1002,
                                reason='Missing masking when expected')
                            self.errors.append(msg)
                            break
                        elif frame.masking_key and not self.expect_masking:
                            msg = CloseControlMessage(
                                code=1002, reason='Masked when not expected')
                            self.errors.append(msg)
                            break
                        else:
                            # If we reach this stage, it's because
                            # the frame wasn't masked and we didn't expect
                            # it anyway. Therefore, on py2k, the bytes
                            # are actually a str object and can't be used
                            # in the utf8 validator as we need integers
                            # when we get each byte one by one.
                            # Our only solution here is to convert our
                            # string to a bytearray.
                            some_bytes = bytearray(some_bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(
                                code=1002,
                                reason=
                                'Received a new message before completing previous'
                            )
                            self.errors.append(msg)
                            break

                        m = TextMessage(some_bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                        if some_bytes:
                            is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                some_bytes)

                            if not is_valid or (m.completed
                                                and not end_on_code_point):
                                self.errors.append(
                                    CloseControlMessage(
                                        code=1007,
                                        reason='Invalid UTF-8 bytes'))
                                break

                    elif frame.opcode == OPCODE_BINARY:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(
                                code=1002,
                                reason=
                                'Received a new message before completing previous'
                            )
                            self.errors.append(msg)
                            break

                        m = BinaryMessage(some_bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                    elif frame.opcode == OPCODE_CONTINUATION:
                        m = self.message
                        if m is None:
                            self.errors.append(
                                CloseControlMessage(
                                    code=1002,
                                    reason='Message not started yet'))
                            break

                        m.extend(some_bytes)
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            if some_bytes:
                                is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                    some_bytes)

                                if not is_valid or (m.completed
                                                    and not end_on_code_point):
                                    self.errors.append(
                                        CloseControlMessage(
                                            code=1007,
                                            reason='Invalid UTF-8 bytes'))
                                    break

                    elif frame.opcode == OPCODE_CLOSE:
                        code = 1005
                        reason = ""
                        if frame.payload_length == 0:
                            self.closing = CloseControlMessage(code=1005)
                        elif frame.payload_length == 1:
                            self.closing = CloseControlMessage(
                                code=1005, reason='Payload has invalid length')
                        else:
                            try:
                                # at this stage, some_bytes have been unmasked
                                # so actually are held in a bytearray
                                code = int(
                                    unpack("!H", bytes(some_bytes[0:2]))[0])
                            except struct.error:
                                reason = 'Failed at decoding closing code'
                            else:
                                # Those codes are reserved or plainly forbidden
                                if code not in VALID_CLOSING_CODES and not (
                                        2999 < code < 5000):
                                    reason = 'Invalid Closing Frame Code: %d' % code
                                    code = 1005
                                elif frame.payload_length > 1:
                                    reason = some_bytes[
                                        2:] if frame.masking_key else frame.body[
                                            2:]

                                    if not py3k: reason = bytearray(reason)
                                    is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                        reason)
                                    if not is_valid or not end_on_code_point:
                                        self.errors.append(
                                            CloseControlMessage(
                                                code=1007,
                                                reason='Invalid UTF-8 bytes'))
                                        break
                                    reason = bytes(reason)
                            self.closing = CloseControlMessage(code=code,
                                                               reason=reason)

                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(some_bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(some_bytes))

                    else:
                        self.errors.append(CloseControlMessage(code=1003))

                    break

                except ProtocolException:
                    self.errors.append(CloseControlMessage(code=1002))
                    break
                except FrameTooLargeException:
                    self.errors.append(
                        CloseControlMessage(code=1002,
                                            reason="Frame was too large"))
                    break

            frame._cleanup()
            frame.body = None
            frame = None

            if self.message is not None and self.message.completed:
                utf8validator.reset()

        utf8validator.reset()
        utf8validator = None

        self._cleanup()
Exemplo n.º 7
0
    def receiver(self):
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a framing.Frame object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises StopIteration, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """
        utf8validator = Utf8Validator()
        
        running = True
        while running:
            frame = Frame()
            while True:
                try:
                    bytes = (yield frame.parser.next())
                    if bytes is None:
                        raise InvalidBytesError()
                    
                    frame.parser.send(bytes)
                except StopIteration:
                    bytes = frame.body or ''
                    if frame.masking_key and bytes:
                        bytes = frame.unmask(bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            self.errors.append(CloseControlMessage(code=1002))
                            break

                        is_valid, _, _, _ = utf8validator.validate(bytes)
                        
                        if is_valid or (not is_valid and frame.fin == 0):
                            m = TextMessage(bytes)
                            m.completed = (frame.fin == 1)
                            self.message = m
                        elif not is_valid and frame.fin == 1:
                            self.errors.append(CloseControlMessage(code=1007))

                    elif frame.opcode == OPCODE_BINARY:
                        m = BinaryMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                    elif frame.opcode == OPCODE_CONTINUATION:
                        m = self.message
                        if m is None:
                            self.errors.append(CloseControlMessage(code=1002))
                            break
                        
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            is_valid, _, _, _ = utf8validator.validate(bytes)
                            if is_valid:
                                m.extend(bytes)
                            else:
                                self.errors.append(CloseControlMessage(code=1007))
                            #except UnicodeDecodeError:
                            #    self.errors.append(CloseControlMessage(code=1007))
                            #    break
                        else:
                            m.extend(bytes)

                    elif frame.opcode == OPCODE_CLOSE:
                        code = 1000
                        reason = ""
                        if len(bytes) == 0:
                            self.errors.append(CloseControlMessage(code=1000))
                        elif 1 < len(bytes) < 126:
                            code = struct.unpack("!H", str(bytes[0:2]))[0]
                            try:
                                code = int(code)
                            except TypeError:
                                code = 1002
                                reason = 'Invalid Closing Frame Code Type'
                            else:
                                # Those codes are reserved or plainly forbidden
                                if code < 1000 or code in [1004, 1005, 1006, 1012, 1013, 1014, 1015,
                                                           1016, 1100, 2000, 2999, 5000, 65536]:
                                    code = 1002
                                    reason = 'Invalid Closing Frame Code'
                                else:    
                                    if len(bytes) > 2:
                                        try:
                                            reason = frame.body[2:].decode("utf-8")
                                        except UnicodeDecodeError:
                                            code = 1007
                                            reason = ''                                
                            self.closing = CloseControlMessage(code=code, reason=reason)
                        else:
                            self.errors.append(CloseControlMessage(code=1002))
                        
                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(bytes))
                    
                    else:
                        self.errors.append(CloseControlMessage(code=1003))

                    # When the frame's payload is empty, we must yield
                    # once more so that the caller is properly aligned
                    if not bytes:
                        yield 0

                    break

                except ProtocolException:
                    self.errors.append(CloseControlMessage(code=1002))
                except FrameTooLargeException:
                    self.errors.append(CloseControlMessage(code=1002))
                except StreamClosed:
                    running = False
                    break

            frame.parser.close()

        utf8validator.reset()
        utf8validator = None
Exemplo n.º 8
0
    def receiver(self):
        """
        Parser that keeps trying to interpret bytes it is fed with as
        incoming frames part of a message.

        Control message are single frames only while data messages, like text
        and binary, may be fragmented accross frames.

        The way it works is by instanciating a :class:`wspy.framing.Frame` object,
        then running its parser generator which yields how much bytes
        it requires to performs its task. The stream parser yields this value
        to its caller and feeds the frame parser.

        When the frame parser raises :exc:`StopIteration`, the stream parser
        tries to make sense of the parsed frame. It dispatches the frame's bytes
        to the most appropriate message type based on the frame's opcode.

        Overall this makes the stream parser totally agonstic to
        the data provider.
        """
        utf8validator = Utf8Validator()
        running = True
        frame = None
        while running:
            frame = Frame()
            while 1:
                try:
                    bytes = (yield frame.parser.next())
                    frame.parser.send(bytes)
                except StopIteration:
                    frame._cleanup()
                    bytes = frame.body or ''

                    # Let's avoid unmasking when there is no payload
                    if bytes:
                        if frame.masking_key and self.expect_masking:
                            bytes = frame.unmask(bytes)
                        elif not frame.masking_key and self.expect_masking:
                            msg = CloseControlMessage(
                                code=1002,
                                reason='Missing masking when expected')
                            self.errors.append(msg)
                            break
                        elif frame.masking_key and not self.expect_masking:
                            msg = CloseControlMessage(
                                code=1002, reason='Masked when not expected')
                            self.errors.append(msg)
                            break
                        else:
                            bytes = bytearray(bytes)

                    if frame.opcode == OPCODE_TEXT:
                        if self.message and not self.message.completed:
                            # We got a text frame before we completed the previous one
                            msg = CloseControlMessage(
                                code=1002,
                                reason=
                                'Received a new message before completing previous'
                            )
                            self.errors.append(msg)
                            break

                        m = TextMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                        if bytes:
                            is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                bytes)

                            if not is_valid or (m.completed
                                                and not end_on_code_point):
                                self.errors.append(
                                    CloseControlMessage(
                                        code=1007,
                                        reason='Invalid UTF-8 bytes'))
                                break

                    elif frame.opcode == OPCODE_BINARY:
                        m = BinaryMessage(bytes)
                        m.completed = (frame.fin == 1)
                        self.message = m

                    elif frame.opcode == OPCODE_CONTINUATION:
                        m = self.message
                        if m is None:
                            self.errors.append(
                                CloseControlMessage(
                                    code=1002,
                                    reason='Message not started yet'))
                            break

                        m.extend(bytes)
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            if bytes:
                                is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                    bytes)

                                if not is_valid or (m.completed
                                                    and not end_on_code_point):
                                    self.errors.append(
                                        CloseControlMessage(
                                            code=1007,
                                            reason='Invalid UTF-8 bytes'))
                                    break

                    elif frame.opcode == OPCODE_CLOSE:
                        code = 1000
                        reason = ""
                        if frame.payload_length == 0:
                            self.closing = CloseControlMessage(code=1000)
                        elif frame.payload_length == 1:
                            self.closing = CloseControlMessage(
                                code=1002, reason='Payload has invalid length')
                        else:
                            try:
                                code = int(
                                    struct.unpack("!H", str(bytes[0:2]))[0])
                            except TypeError:
                                code = 1002
                                reason = 'Invalid Closing Frame Code Type'
                            except struct.error, sr:
                                code = 1002
                                reason = 'Failed at decoding closing code'
                            else:
                                # Those codes are reserved or plainly forbidden
                                if code not in VALID_CLOSING_CODES and not (
                                        2999 < code < 5000):
                                    reason = 'Invalid Closing Frame Code: %d' % code
                                    code = 1002
                                elif frame.payload_length > 1:
                                    reason = bytes[
                                        2:] if frame.masking_key else bytearray(
                                            frame.body[2:])
                                    is_valid, end_on_code_point, _, _ = utf8validator.validate(
                                        reason)
                                    if not is_valid or not end_on_code_point:
                                        self.errors.append(
                                            CloseControlMessage(
                                                code=1007,
                                                reason='Invalid UTF-8 bytes'))
                                        break
                            self.closing = CloseControlMessage(
                                code=code, reason=reason.decode('utf-8'))

                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(bytes))
Exemplo n.º 9
0
                                        self.errors.append(
                                            CloseControlMessage(
                                                code=1007,
                                                reason='Invalid UTF-8 bytes'))
                                        break
                            self.closing = CloseControlMessage(
                                code=code, reason=reason.decode('utf-8'))

                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(bytes))

                    else:
                        self.errors.append(CloseControlMessage(code=1003))

                    break

                except ProtocolException:
                    self.errors.append(CloseControlMessage(code=1002))
                except FrameTooLargeException:
                    self.errors.append(CloseControlMessage(code=1002))
                except StreamClosed:
                    running = False
                    break

            frame.body = None
            frame = None

            if self.message and self.message.completed:
Exemplo n.º 10
0
                                        self.errors.append(
                                            CloseControlMessage(
                                                code=1007,
                                                reason='Invalid UTF-8 bytes'))
                                        break
                            self.closing = CloseControlMessage(
                                code=code, reason=reason.decode('utf-8'))

                    elif frame.opcode == OPCODE_PING:
                        self.pings.append(PingControlMessage(bytes))

                    elif frame.opcode == OPCODE_PONG:
                        self.pongs.append(PongControlMessage(bytes))

                    else:
                        self.errors.append(CloseControlMessage(code=1003))

                    break

                except ProtocolException:
                    self.errors.append(CloseControlMessage(code=1002))
                    break
                except FrameTooLargeException:
                    self.errors.append(
                        CloseControlMessage(code=1002,
                                            reason="Frame was too large"))
                    break
                except StreamClosed:
                    running = False
                    break