def web_socket_transfer_data(request):
    payload1 = 'Invalid continuation frame to be ignored.'
    payload2 = 'Valid frame after closing should be disposed.'
    request.connection.write(
        stream.create_header(common.OPCODE_CONTINUATION,
                             len(payload1), 1, 0, 0, 0, 0) + payload1)
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, len(payload2), 1, 0, 0, 0, 0) +
        payload2)
def web_socket_transfer_data(request):
    payload1 = 'Invalid continuation frame to be ignored.'
    payload2 = 'Valid frame after closing should be disposed.'
    request.connection.write(stream.create_header(common.OPCODE_CONTINUATION,
                                                  len(payload1),
                                                  1, 0, 0, 0, 0) + payload1)
    request.connection.write(stream.create_header(common.OPCODE_TEXT,
                                                  len(payload2),
                                                  1, 0, 0, 0, 0) + payload2)
def web_socket_transfer_data(request):
    message = b"This message should be ignored."
    if bit == 1:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message
    elif bit == 2:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message
    elif bit == 3:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
Exemple #4
0
def web_socket_transfer_data(request):
    message = "This message should be ignored."
    if bit == 1:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message
    elif bit == 2:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message
    elif bit == 3:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
def web_socket_transfer_data(request):
    text = 'This message should be ignored.'
    opcode = common.OPCODE_TEXT
    if bit == 1:
        frame = stream.create_header(opcode, len(text), 1, 1, 0, 0, 0) + text
    elif bit == 2:
        frame = stream.create_header(opcode, len(text), 1, 0, 1, 0, 0) + text
    elif bit == 3:
        frame = stream.create_header(opcode, len(text), 1, 0, 0, 1, 0) + text
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
def web_socket_transfer_data(request):
    text = b'This message should be ignored.'
    opcode = common.OPCODE_TEXT
    if bit == 1:
        frame = stream.create_header(opcode, len(text), 1, 1, 0, 0, 0) + text
    elif bit == 2:
        frame = stream.create_header(opcode, len(text), 1, 0, 1, 0, 0) + text
    elif bit == 3:
        frame = stream.create_header(opcode, len(text), 1, 0, 0, 1, 0) + text
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
Exemple #7
0
def web_socket_transfer_data(request):
    line = request.ws_stream.receive_message()
    # Hello
    payload = b'\xf2\x48\xcd\xc9\xc9\x07\x00\x00\x00\xff\xff'
    # Strip \x00\x00\xff\xff
    stripped = payload[:-4]

    header = create_header(common.OPCODE_TEXT, len(payload),
        fin=0, rsv1=1, rsv2=0, rsv3=0, mask=False)
    request.ws_stream._write(header + payload)

    header = create_header(common.OPCODE_CONTINUATION, len(stripped),
        fin=1, rsv1=0, rsv2=0, rsv3=0, mask=False)
    request.ws_stream._write(header + stripped)
Exemple #8
0
def web_socket_transfer_data(request):
    messages_to_send = ['Hello, world!', '', all_distinct_bytes()]
    for message in messages_to_send:
        # FIXME: Should use better API to send binary messages when pywebsocket supports it.
        header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0,
                                      0, 0, 0)
        request.connection.write(header + message)
def web_socket_transfer_data(request):
    match = re.search(r'\?bit=(\d+)$', request.ws_resource)
    if match is None:
        msgutil.send_message(request, 'FAIL: Query value is incorrect or missing')
        return

    bit = int(match.group(1))
    message = "This message should be ignored."
    if bit == 1:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message
    elif bit == 2:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message
    elif bit == 3:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
Exemple #10
0
def web_socket_transfer_data(request):
    payload1 = b'This first text should be received.'
    payload2 = b'This second text '
    payload3 = b'should be received, too.'

    # send ''
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, 0, 1, 0, 0, 0, 0))

    # send payload1
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, len(payload1), 1, 0, 0, 0, 0)
        + payload1)

    # send '' + ''
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, 0, 0, 0, 0, 0, 0))
    request.connection.write(
        stream.create_header(common.OPCODE_CONTINUATION, 0, 1, 0, 0, 0, 0))

    # send payload2 + '' + payload3
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, len(payload2), 0, 0, 0, 0, 0)
        + payload2)
    request.connection.write(
        stream.create_header(common.OPCODE_CONTINUATION, 0, 0, 0, 0, 0, 0))
    request.connection.write(
        stream.create_header(common.OPCODE_CONTINUATION, len(payload3), 1, 0,
                             0, 0, 0) + payload3)
def web_socket_transfer_data(request):
    match = re.search(r"\?opcode=(\d+)$", request.ws_resource)
    if match is None:
        msgutil.send_message(request, "FAIL: Query value is incorrect or missing")
        return

    opcode = int(match.group(1))
    payload = "This text should be ignored. (opcode = %d)" % opcode
    request.connection.write(stream.create_header(opcode, len(payload), 1, 0, 0, 0, 0) + payload)
Exemple #12
0
def web_socket_transfer_data(request):
    compress = zlib.compressobj(
        zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
    compressed_message = compress.compress('close message')
    compressed_message += compress.flush(zlib.Z_SYNC_FLUSH)
    compressed_message = compressed_message[:-4]
    header = stream.create_header(
        opcode=common.OPCODE_CLOSE, payload_length=len(compressed_message),
        fin=1, rsv1=1, rsv2=0, rsv3=0, mask=False)
    request.connection.write(header + compressed_message)
def web_socket_transfer_data(request):
    match = re.search(r'\?opcode=(\d+)$', request.ws_resource)
    if match is None:
        msgutil.send_message(request,
                             'FAIL: Query value is incorrect or missing')
        return

    opcode = int(match.group(1))
    payload = f'This text should be ignored. (opcode = {opcode})'.encode()
    request.connection.write(
        stream.create_header(opcode, len(payload), 1, 0, 0, 0, 0) + payload)
Exemple #14
0
def web_socket_transfer_data(request):
    # Make sure to receive a message from client before sending messages.
    client_message = msgutil.receive_message(request)
    messages_to_send = [
        b'Hello, world!', 'Привет, Мир!'.encode(), b'',
        all_distinct_bytes()
    ]
    for message in messages_to_send:
        # FIXME: Should use better API to send binary messages when pywebsocket supports it.
        header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0,
                                      0, 0, 0)
        request.connection.write(header + message)
def web_socket_transfer_data(request):
    match = re.search(r'\?bit=(\d+)$', request.ws_resource)
    if match is None:
        msgutil.send_message(request,
                             'FAIL: Query value is incorrect or missing')
        return

    bit = int(match.group(1))
    message = "This message should be ignored."
    if bit == 1:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0,
                                     0, 0) + message
    elif bit == 2:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1,
                                     0, 0) + message
    elif bit == 3:
        frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0,
                                     1, 0) + message
    else:
        frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit)
    request.connection.write(frame)
Exemple #16
0
    def test_create_header(self):
        # more, rsv1, ..., rsv4 are all true
        header = stream.create_header(common.OPCODE_TEXT, 1, 1, 1, 1, 1, 1)
        self.assertEqual(b'\xf1\x81', header)

        # Maximum payload size
        header = stream.create_header(common.OPCODE_TEXT, (1 << 63) - 1, 0, 0,
                                      0, 0, 0)
        self.assertEqual(b'\x01\x7f\x7f\xff\xff\xff\xff\xff\xff\xff', header)

        # Invalid opcode 0x10
        self.assertRaises(ValueError, stream.create_header, 0x10, 0, 0, 0, 0,
                          0, 0)

        # Invalid value 0xf passed to more parameter
        self.assertRaises(ValueError, stream.create_header, common.OPCODE_TEXT,
                          0, 0xf, 0, 0, 0, 0)

        # Too long payload_length
        self.assertRaises(ValueError, stream.create_header, common.OPCODE_TEXT,
                          1 << 63, 0, 0, 0, 0, 0)
Exemple #17
0
    def test_create_header(self):
        # more, rsv1, ..., rsv4 are all true
        header = stream.create_header(common.OPCODE_TEXT, 1, 1, 1, 1, 1, 1)
        self.assertEqual('\xf1\x81', header)

        # Maximum payload size
        header = stream.create_header(
            common.OPCODE_TEXT, (1 << 63) - 1, 0, 0, 0, 0, 0)
        self.assertEqual('\x01\x7f\x7f\xff\xff\xff\xff\xff\xff\xff', header)

        # Invalid opcode 0x10
        self.assertRaises(ValueError,
                          stream.create_header,
                          0x10, 0, 0, 0, 0, 0, 0)

        # Invalid value 0xf passed to more parameter
        self.assertRaises(ValueError,
                          stream.create_header,
                          common.OPCODE_TEXT, 0, 0xf, 0, 0, 0, 0)

        # Too long payload_length
        self.assertRaises(ValueError,
                          stream.create_header,
                          common.OPCODE_TEXT, 1 << 63, 0, 0, 0, 0, 0)
def web_socket_transfer_data(request):
    messages_to_send = [['Hello, ', 'world!'],
                        ['', 'Hello, ', '', 'world!', ''],
                        ['', '', ''],
                        [chr(i) for i in xrange(256)]]
    for message_list in messages_to_send:
        for index, message in enumerate(message_list):
            # FIXME: Should use better API to send binary messages when pywebsocket supports it.
            if index == 0:
                opcode = common.OPCODE_BINARY
            else:
                opcode = common.OPCODE_CONTINUATION
            if index < len(message_list) - 1:
                final = 0
            else:
                final = 1
            header = stream.create_header(opcode, len(message), final, 0, 0, 0, 0)
            request.connection.write(header + message)
Exemple #19
0
def web_socket_transfer_data(request):
    messages_to_send = [['Hello, ', 'world!'],
                        ['', 'Hello, ', '', 'world!', ''], ['', '', ''],
                        [chr(i) for i in xrange(256)]]
    for message_list in messages_to_send:
        for index, message in enumerate(message_list):
            # FIXME: Should use better API to send binary messages when pywebsocket supports it.
            if index == 0:
                opcode = common.OPCODE_BINARY
            else:
                opcode = common.OPCODE_CONTINUATION
            if index < len(message_list) - 1:
                final = 0
            else:
                final = 1
            header = stream.create_header(opcode, len(message), final, 0, 0, 0,
                                          0)
            request.connection.write(header + message)
def web_socket_transfer_data(request):
    payload1 = "This first text should be received."
    payload2 = "This second text "
    payload3 = "should be received, too."

    # send ''
    request.connection.write(stream.create_header(common.OPCODE_TEXT, 0, 1, 0, 0, 0, 0))

    # send payload1
    request.connection.write(stream.create_header(common.OPCODE_TEXT, len(payload1), 1, 0, 0, 0, 0) + payload1)

    # send '' + ''
    request.connection.write(stream.create_header(common.OPCODE_TEXT, 0, 0, 0, 0, 0, 0))
    request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, 0, 1, 0, 0, 0, 0))

    # send payload2 + '' + payload3
    request.connection.write(stream.create_header(common.OPCODE_TEXT, len(payload2), 0, 0, 0, 0, 0) + payload2)
    request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, 0, 0, 0, 0, 0, 0))
    request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, len(payload3), 1, 0, 0, 0, 0) + payload3)
Exemple #21
0
def web_socket_transfer_data(request):
    payload = 'This text should be ignored. \xff'  # '\xff' will never appear in UTF-8 encoded data.
    request.connection.write(
        stream.create_header(common.OPCODE_TEXT, len(payload), 1, 0, 0, 0, 0) +
        payload)
Exemple #22
0
def web_socket_transfer_data(request):
    payload = 'This text should be ignored. \xff'  # '\xff' will never appear in UTF-8 encoded data.
    request.connection.write(stream.create_header(common.OPCODE_TEXT, len(payload), 1, 0, 0, 0, 0) + payload)
def web_socket_transfer_data(request):
    messages_to_send = ['Hello, world!', 'Привет, Мир!', '', all_distinct_bytes()]
    for message in messages_to_send:
        # FIXME: Should use better API to send binary messages when pywebsocket supports it.
        header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0, 0, 0, 0)
        request.connection.write(header + message)