def test_text_message_with_continuation_and_ping_in_between(self):
        msg = b'hello there'
        key = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT, body=msg, fin=0, masking_key=os.urandom(4)).build()
        s = Stream()
        self.assertEqual(s.has_message, False)
        s.parser.send(f)
        self.assertEqual(s.message.completed, False)

        for i in range(3):
            f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=0, masking_key=os.urandom(4)).build()
            s.parser.send(f)
            self.assertEqual(s.has_message, False)
            self.assertEqual(s.message.completed, False)
            self.assertEqual(s.message.opcode, OPCODE_TEXT)

            f = Frame(opcode=OPCODE_PING, body=b'ping me', fin=1, masking_key=os.urandom(4)).build()
            self.assertEqual(len(s.pings), i)
            s.parser.send(f)
            self.assertEqual(len(s.pings), i+1)

        f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=1, masking_key=os.urandom(4)).build()
        s.parser.send(f)
        self.assertEqual(s.has_message, True)
        self.assertEqual(s.message.opcode, OPCODE_TEXT)
        self.assertEqual(s.message.completed, True)
Esempio n. 2
0
    def test_binary_message_with_continuation_received(self):
        msg = os.urandom(16)
        key = os.urandom(4)
        f = Frame(opcode=OPCODE_BINARY, body=msg, fin=0,
                  masking_key=key).build()
        s = Stream()
        self.assertEqual(s.has_message, False)
        s.parser.send(enc(f))
        self.assertEqual(s.has_message, False)

        for i in range(3):
            f = Frame(opcode=OPCODE_CONTINUATION,
                      body=msg,
                      fin=0,
                      masking_key=key).build()
            s.parser.send(enc(f))
            self.assertEqual(s.has_message, False)
            self.assertEqual(s.message.completed, False)
            self.assertEqual(s.message.opcode, OPCODE_BINARY)

        f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=1,
                  masking_key=key).build()
        s.parser.send(enc(f))
        self.assertEqual(s.has_message, True)
        self.assertEqual(s.message.completed, True)
        self.assertEqual(s.message.opcode, OPCODE_BINARY)
Esempio n. 3
0
    def test_masking(self):
        mask = "7\xfa!="
        f = Frame(opcode=OPCODE_TEXT,
                  body='Hello', masking_key=mask, fin=1)

        spec_says = '\x81\x857\xfa!=\x7f\x9fMQX'
        self.assertEqual(f.build(), spec_says)
Esempio n. 4
0
    def test_invalid_opcode(self):
        for opcode in range(3, 9):
            f = Frame()
            self.assertRaises(ProtocolException, f.parser.send, chr(opcode))

        f = Frame()
        self.assertRaises(ProtocolException, f.parser.send, chr(10))
    def test_connect_and_close(self, sock):
        
        s = MagicMock()
        sock.socket.return_value = s
        sock.getaddrinfo.return_value = [(socket.AF_INET, socket.SOCK_STREAM, 0, "",
                                          ("127.0.0.1", 80, 0, 0))]

        c = WebSocketBaseClient(url="ws://127.0.0.1/?token=value")
        
        s.recv.return_value = b"\r\n".join([
            b"HTTP/1.1 101 Switching Protocols",
            b"Connection: Upgrade",
            b"Sec-Websocket-Version: 13",
            b"Content-Type: text/plain;charset=utf-8",
            b"Sec-Websocket-Accept: " + b64encode(sha1(c.key + WS_KEY).digest()),
            b"Upgrade: websocket",
            b"Date: Sun, 26 Jul 2015 12:32:55 GMT",
            b"Server: ws4py/test",
            b"\r\n"
        ])

        c.connect()
        s.connect.assert_called_once_with(("127.0.0.1", 80))

        s.reset_mock()
        c.close(code=1006, reason="boom")
        args = s.sendall.call_args_list[0]
        f = Frame()
        f.parser.send(args[0][0])
        f.parser.close()
        self.assertIn(b'boom', f.unmask(f.body))
    def test_63_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65536, fin=1)
        self.assertEqual(len(f.build()), 65546)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65536, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 65550)
Esempio n. 7
0
    def test_connect_and_close(self, sock):

        s = MagicMock()
        sock.socket.return_value = s
        sock.getaddrinfo.return_value = [(socket.AF_INET, socket.SOCK_STREAM,
                                          0, "", ("127.0.0.1", 80, 0, 0))]

        c = WebSocketBaseClient(url="ws://127.0.0.1/?token=value")

        s.recv.return_value = b"\r\n".join([
            b"HTTP/1.1 101 Switching Protocols", b"Connection: Upgrade",
            b"Sec-Websocket-Version: 13",
            b"Content-Type: text/plain;charset=utf-8",
            b"Sec-Websocket-Accept: " +
            b64encode(sha1(c.key + WS_KEY).digest()), b"Upgrade: websocket",
            b"Date: Sun, 26 Jul 2015 12:32:55 GMT", b"Server: ws4py/test",
            b"\r\n"
        ])

        c.connect()
        s.connect.assert_called_once_with(("127.0.0.1", 80))

        s.reset_mock()
        c.close(code=1006, reason="boom")
        args = s.sendall.call_args_list[0]
        f = Frame()
        f.parser.send(args[0][0])
        f.parser.close()
        self.assertIn(b'boom', f.unmask(f.body))
Esempio n. 8
0
    def test_frame_sized_126(self):
        body = b'*' * 256
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1).build()

        f = Frame()
        # determine how the size is stored
        f.parser.send(bytes[:3])
        self.assertTrue(f.masking_key is None)
        # that's a large frame indeed
        self.assertEqual(f.payload_length, 126)

        # this will compute the actual application data size
        # it will also read the first byte of data
        # indeed the length is found from byte 3 to 10
        f.parser.send(bytes[3:11])
        self.assertEqual(f.payload_length, 256)

        # parse the rest of our data
        f.parser.send(bytes[11:])
        self.assertEqual(f.body, body)

        # The same but this time we provide enough
        # bytes so that the application's data length
        # can be computed from the first generator's send call
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 256)

        # parse the rest of our data
        f.parser.send(bytes[10:])
        self.assertEqual(f.body, body)
Esempio n. 9
0
    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEqual(len(manager), 0)

        s = MagicMock()
        s.recv.return_value = Frame(opcode=OPCODE_TEXT,
                                    body=b'hello',
                                    fin=1,
                                    masking_key=os.urandom(4)).build()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEqual(len(manager), 1)
        self.assertTrue(h in manager)

        # the following call to .close() on the
        # websocket object will initiate
        # the closing handshake
        # This next line mocks the response
        # from the client to actually
        # complete the handshake.
        # The manager will then remove the websocket
        # from its pool
        s.recv.return_value = Frame(opcode=OPCODE_CLOSE,
                                    body=b"ok we're done",
                                    fin=1,
                                    masking_key=os.urandom(4)).build()
        h.close()

        # the poller runs a thread, give it time to get there
        time.sleep(1)

        # TODO: Implement a fake poller so that works...
        self.assertEqual(len(manager), 0)
Esempio n. 10
0
    def test_frame_sized_below_127(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'*' * 65536, fin=1).build()

        f = Frame()
        f.parser.send(bytes[:3])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 127)
Esempio n. 11
0
    def test_incremental_parsing_small_7_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=enc('hello'), fin=1).build()

        f = Frame()
        map_on_bytes(f.parser.send, bytes)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 5)
Esempio n. 12
0
    def test_incremental_parsing_63_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'*' * 65536, fin=1).build()

        f = Frame()
        map_on_bytes(f.parser.send, bytes)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)
Esempio n. 13
0
    def test_text_message_with_continuation_received(self):
        msg = 'hello there'
        f = Frame(opcode=OPCODE_TEXT,
                  body=msg,
                  fin=0,
                  masking_key=os.urandom(4)).build()
        s = Stream()
        self.assertEqual(s.has_message, False)
        s.parser.send(enc(f))
        self.assertEqual(s.message.completed, False)

        for i in range(3):
            f = Frame(opcode=OPCODE_CONTINUATION,
                      body=msg,
                      fin=0,
                      masking_key=os.urandom(4)).build()
            s.parser.send(enc(f))
            self.assertEqual(s.has_message, False)
            self.assertEqual(s.message.completed, False)
            self.assertEqual(s.message.opcode, OPCODE_TEXT)

        f = Frame(opcode=OPCODE_CONTINUATION,
                  body=msg,
                  fin=1,
                  masking_key=os.urandom(4)).build()
        s.parser.send(enc(f))
        self.assertEqual(s.has_message, True)
        self.assertEqual(s.message.completed, True)
        self.assertEqual(s.message.opcode, OPCODE_TEXT)
Esempio n. 14
0
    def test_masking(self):
        if py3k: mask = b"7\xfa!="
        else: mask = "7\xfa!="
        f = Frame(opcode=OPCODE_TEXT, body=b'Hello', masking_key=mask, fin=1)

        if py3k: spec_says = b'\x81\x857\xfa!=\x7f\x9fMQX'
        else: spec_says = '\x81\x857\xfa!=\x7f\x9fMQX'
        self.assertEqual(f.build(), spec_says)
    def test_opcodes(self):
        for opcode in [OPCODE_CONTINUATION, OPCODE_TEXT, OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG]:
            f = Frame(opcode=opcode, body=b"", fin=1)
            byte = ord(f.build()[0])
            self.assertTrue(byte & opcode == opcode)

        f = Frame(opcode=0x3, body=b"", fin=1)
        self.assertRaises(ValueError, f.build)
Esempio n. 16
0
    def test_incremental_parsing_16_bit_length(self):
        bytes = Frame(opcode=OPCODE_TEXT, body='*' * 126, fin=1).build()

        f = Frame()
        for byte in bytes:
            f.parser.send(byte)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 126)
    def test_masking(self):
        if py3k: mask = b"7\xfa!="
        else: mask = "7\xfa!="
        f = Frame(opcode=OPCODE_TEXT,
                  body=enc('Hello'),
                  masking_key=mask, fin=1)

        if py3k: spec_says = b'\x81\x857\xfa!=\x7f\x9fMQX'
        else: spec_says = '\x81\x857\xfa!=\x7f\x9fMQX'
        self.assertEqual(f.build(), spec_says)
Esempio n. 18
0
    def test_opcodes(self):
        for opcode in [
                OPCODE_CONTINUATION, OPCODE_TEXT, OPCODE_BINARY, OPCODE_CLOSE,
                OPCODE_PING, OPCODE_PONG
        ]:
            f = Frame(opcode=opcode, body=b'', fin=1)
            byte = ord(f.build()[0])
            self.assertTrue(byte & opcode == opcode)

        f = Frame(opcode=0x3, body=b'', fin=1)
        self.assertRaises(ValueError, f.build)
Esempio n. 19
0
 def received_message(self, m):
     # Assuming type of arraybuffer
     xx = N.fromstring(m.data, N.int16, count=5)
     # xx = N.fromstring(m.data, N.float32, count=4)
     print xx
     xx = xx*2;
     f = Frame(OPCODE_BINARY, xx.tostring(), fin=1)
     b = f.build()
     yy = BinaryMessage(b)
     for conn in SUBSCRIBERS:
     	conn.send(yy)
Esempio n. 20
0
    def test_frame_header_parsing(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()

        f = Frame()
        self.assertEqual(f.parser.send(bytes[0:1]), 1)
        self.assertEqual(f.fin, 1)
        self.assertEqual(f.rsv1, 0)
        self.assertEqual(f.rsv2, 0)
        self.assertEqual(f.rsv3, 0)
        self.assertEqual(f.parser.send(bytes[1:2]), 5)
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 5)
        f.parser.close()
Esempio n. 21
0
    def test_frame_payload_parsing(self):
        bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()

        f = Frame()
        self.assertEqual(f.parser.send(bytes[0:1]), 1)
        self.assertEqual(f.parser.send(bytes[1:2]), 5)
        f.parser.send(bytes[2:])
        self.assertEqual(f.body, b'hello')

        f = Frame()
        f.parser.send(bytes)
        self.assertRaises(StopIteration, next, f.parser)
        self.assertEqual(f.body, b'hello')
 def test_protocol_exception_from_frame_parsing(self):
     payload = struct.pack("!H", 1000) + b'hello'
     f = Frame(opcode=OPCODE_CLOSE, body=payload,
               fin=1, masking_key=os.urandom(4))
     f.rsv1 = 1
     f = f.build()
     s = Stream()
     self.assertEqual(len(s.errors), 0)
     self.assertEqual(s.closing, None)
     s.parser.send(f)
     self.assertEqual(s.closing, None)
     self.assertEqual(type(s.errors[0]), CloseControlMessage)
     self.assertEqual(s.errors[0].code, 1002)
 def test_ping_message_received(self):
     msg = 'ping me'
     f = Frame(opcode=OPCODE_PING, body=msg, fin=1).build()
     s = Stream()
     self.assertEqual(len(s.pings), 0)
     s.parser.send(f)
     self.assertEqual(len(s.pings), 1)
 def test_pong_message_received(self):
     msg = b'pong!'
     f = Frame(opcode=OPCODE_PONG, body=msg, fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(len(s.pongs), 0)
     s.parser.send(f)
     self.assertEqual(len(s.pongs), 1)
    def test_binary_and_text_messages_cannot_interleave(self):
        s = Stream()

        f = Frame(opcode=OPCODE_TEXT, body=b'hello',
                  fin=0, masking_key=os.urandom(4)).build()
        s.parser.send(f)
        next(s.parser)

        f = Frame(opcode=OPCODE_BINARY, body=os.urandom(7),
                  fin=1, masking_key=os.urandom(4)).build()
        s.parser.send(f)
        next(s.parser)

        self.assertNotEqual(s.errors, [])
        self.assertIsInstance(s.errors[0], CloseControlMessage)
        self.assertEqual(s.errors[0].code, 1002)
 def test_binary_message_received(self):
     msg = os.urandom(16)
     f = Frame(opcode=OPCODE_BINARY, body=msg, fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(s.has_message, False)
     s.parser.send(f)
     self.assertEqual(s.message.completed, True)
    def test_invalid_encoded_bytes_on_continuation(self):
        s = Stream()

        f = Frame(opcode=OPCODE_TEXT, body=b'hello',
                  fin=0, masking_key=os.urandom(4)).build()
        s.parser.send(f)
        next(s.parser)

        f = Frame(opcode=OPCODE_CONTINUATION, body=b'h\xc3llo',
                  fin=1, masking_key=os.urandom(4)).build()
        s.parser.send(f)
        next(s.parser)

        self.assertNotEqual(s.errors, [])
        self.assertIsInstance(s.errors[0], CloseControlMessage)
        self.assertEqual(s.errors[0].code, 1007)
 def test_text_message_received(self):
     msg = b'hello there'
     f = Frame(opcode=OPCODE_TEXT, body=msg, fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(s.has_message, False)
     s.parser.send(f)
     self.assertEqual(s.message.completed, True)
 def test_text_message_received(self):
     msg = b'hello there'
     f = Frame(opcode=OPCODE_TEXT, body=msg, fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(len(s.messages), 0)
     s.parser.send(f)
     self.assertEqual(len(s.messages), 1)
Esempio n. 30
0
    def test_frame_sized_127(self):
        body = b'*'*65536
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1).build()

        f = Frame()
        # determine how the size is stored
        f.parser.send(bytes[:3])
        self.assertTrue(f.masking_key is None)
        # that's a large frame indeed
        self.assertEqual(f.payload_length, 127)

        # this will compute the actual application data size
        # it will also read the first byte of data
        # indeed the length is found from byte 3 to 10
        f.parser.send(bytes[3:11])
        self.assertEqual(f.payload_length, 65536)
        
        # parse the rest of our data
        f.parser.send(bytes[11:])
        self.assertEqual(f.body, body)

        
        # The same but this time we provide enough
        # bytes so that the application's data length
        # can be computed from the first generator's send call
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)
        
        # parse the rest of our data
        f.parser.send(bytes[10:])
        self.assertEqual(f.body, body)
        
        
        # The same with masking given out gradually
        mask = os.urandom(4)
        bytes = Frame(opcode=OPCODE_TEXT, body=body, fin=1, masking_key=mask).build()
        f = Frame()
        f.parser.send(bytes[:10])
        self.assertTrue(f.masking_key is None)
        self.assertEqual(f.payload_length, 65536)
        
        # parse the mask gradually
        f.parser.send(bytes[10:12])
        f.parser.send(bytes[12:])
        self.assertEqual(f.unmask(f.body), body)
 def test_incremental_text_message_received(self):
     msg = 'hello there'
     f = Frame(opcode=OPCODE_TEXT, body=msg, fin=1).build()
     s = Stream()
     self.assertEqual(s.has_message, False)
     for byte in f:
         s.parser.send(byte)
     self.assertEqual(s.has_message, True)
 def test_using_masking_key_when_unexpected(self):
     f = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1, masking_key=os.urandom(4)).build()
     s = Stream(expect_masking=False)
     s.parser.send(f)
     next(s.parser)
     self.assertNotEqual(s.errors, [])
     self.assertIsInstance(s.errors[0], CloseControlMessage)
     self.assertEqual(s.errors[0].code, 1002)
Esempio n. 33
0
 def test_closing_parser_should_release_resources(self):
     f = Frame(opcode=OPCODE_TEXT,
               body=b'hello',
               fin=1,
               masking_key=os.urandom(4)).build()
     s = Stream()
     s.parser.send(f)
     s.parser.close()
Esempio n. 34
0
 def single(self, mask=False):
     """
     Returns a frame bytes with the fin bit set and a random mask.
     """
     mask = os.urandom(4) if mask else None
     return Frame(body=self.data or '',
                  opcode=self.opcode,
                  masking_key=mask,
                  fin=1).build()
 def test_incremental_text_message_received(self):
     msg = b'hello there'
     f = Frame(opcode=OPCODE_TEXT, body=msg, fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(s.has_message, False)
     bytes = f
     for index, byte in enumerate(bytes):
         s.parser.send(bytes[index:index+1])
     self.assertEqual(s.has_message, True)
Esempio n. 36
0
 def test_empty_close_message(self):
     f = Frame(opcode=OPCODE_CLOSE,
               body=enc(''),
               fin=1,
               masking_key=os.urandom(4)).build()
     s = Stream()
     self.assertEqual(s.closing, None)
     s.parser.send(enc(f))
     self.assertEqual(type(s.closing), CloseControlMessage)
 def test_continuation_frame_before_message_started_is_invalid(self):
     f = Frame(opcode=OPCODE_CONTINUATION, body=b'hello',
               fin=1, masking_key=os.urandom(4)).build()
     s = Stream()
     s.parser.send(f)
     next(s.parser)
     self.assertNotEqual(s.errors, [])
     self.assertIsInstance(s.errors[0], CloseControlMessage)
     self.assertEqual(s.errors[0].code, 1002)
    def test_7_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT, body=b"", fin=1)
        self.assertEqual(len(f.build()), 2)

        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 125, fin=1)
        self.assertEqual(len(f.build()), 127)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT, body=b"", masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 6)

        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 125, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 131)
    def test_16_bit_length(self):
        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 126, fin=1)
        self.assertEqual(len(f.build()), 130)

        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65535, fin=1)
        self.assertEqual(len(f.build()), 65539)

        mask = os.urandom(4)
        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 126, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 134)

        f = Frame(opcode=OPCODE_TEXT, body=b"*" * 65535, masking_key=mask, fin=1)
        self.assertEqual(len(f.build()), 65543)
Esempio n. 40
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
Esempio n. 41
0
    def receiver(self):
        global logAudio
        """
        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.
        """

        logAudio=settings.getVal("logAudio")
        print("debug: streaming.py in receiver function | logAudio=%s" % logAudio)

        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

                        if logAudio != "0":
                           print("debug:: received a binary frame with %d bytes, log it via localhost listner (port:%s)" % (len(some_bytes),logAudio))
                           #bstr1=""
                           #for i in range(20):
                           #    str1="%d " % some_bytes[i]
                           #    bstr1 = bstr1 + " " + str1
                           #print("binary msg 1st 20 bytes: %s" % bstr1)

                           # Create a TCP/IP socket
                           sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

                           # Connect the socket to the port where the server is listening
                           server_address = ('localhost', int(logAudio))

                           #print >>sys.stderr, 'connecting to %s port %s' % server_address
                           sock.connect(server_address)

                           try:
                              # Send data
                              traceUtf8=""
                              for i in range(10):
                                  traceUtf8 = traceUtf8 + ' ' + str(some_bytes[i])
                              print("debug:send a mp3 frame (len=%d) to a log server [ %s ]" % (len(some_bytes),traceUtf8))

                              #print >>sys.stderr, '"%s"' % message
                              sock.sendall(some_bytes)

                              # Look for the response
                              amount_received = 0
                              amount_expected = 7
    
                              while amount_received < amount_expected:
                                  data = sock.recv(10)
                                  amount_received += len(data)
                                  #print >>sys.stderr, 'received "%s"' % data

                           finally:
                              #print >>sys.stderr, 'closing socket'
                              sock.close()
                        # --- end of logAudio action

                        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 = 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:
                                # 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:
                                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 = 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()
Esempio n. 42
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
                            raise ProtocolException()
                            
                        try:
                            m = TextMessage(bytes.decode("utf-8", "replace"))
                            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:
                            raise ProtocolException()
                        
                        m.completed = (frame.fin == 1)
                        if m.opcode == OPCODE_TEXT:
                            try:
                                m.extend(bytes.decode("utf-8", "replace"))
                            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()
Esempio n. 43
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 next(frame.parser))
                    frame.parser.send(bytes)
                except StopIteration:
                    frame._cleanup()
                    bytes = frame.body

                    # 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(unpack("!H", enc(bytes[0:2]))[0])
                            except TypeError:
                                code = 1002
                                reason = 'Invalid Closing Frame Code Type'
                            except struct.error as 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)
                        
                    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

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

        if frame:
            frame._cleanup()
            frame = None

        utf8validator.reset()    
        utf8validator = None

        self._cleanup()
Esempio n. 44
0
 def test_frame_too_large(self):
     f = Frame(opcode=OPCODE_TEXT, body=b'', fin=1)
     # fake huge length
     f.payload_length = 1 << 63
     self.assertRaises(FrameTooLargeException, f.build)
Esempio n. 45
0
 def test_passing_encoded_string(self):
     # once encoded the u'\xe9' character will be of length 2
     f = Frame(opcode=OPCODE_TEXT, body=u'\xe9trange'.encode('utf-8'), fin=1)
     self.assertEqual(len(f.build()), 10)