Beispiel #1
0
def parse_handshake(request):
    nullhandshake = ffi.new('struct handshake*')
    C.nullhandshake(nullhandshake)
    request = ffi.new('const uint8_t[]', request)
    request_len = ffi.cast('size_t', len(request))
    ws_type = C.ws_parse_handshake(request, request_len, nullhandshake)
    return ws_type
Beispiel #2
0
    def test_pycws_cffi_parse_input_single_frame_masked(self):
        frame = ffi.new('const uint8_t[]', SINGLE_FRAME_UNMASKED)
        frame_len = ffi.cast('size_t', len(frame))
        ws_type = C.ws_parse_input_frame(frame, frame_len)
        assert C.WS_TEXT_FRAME == ws_type

        uint64 = ffi.new('uint64_t*')
        data = C.extract_payload(frame, uint64)
        data = ffi.buffer(data, uint64[0])[:]
        assert b'Hello' == data
Beispiel #3
0
    def test_pycws_cffi_parse_input_single_masked(self):
        FRAME = bytes([0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58])

        frame = ffi.new('const uint8_t[]', FRAME)
        frame_len = ffi.cast('size_t', len(frame))
        ws_type = C.ws_parse_input_frame(frame, frame_len)
        assert C.WS_TEXT_FRAME == ws_type

        uint64 = ffi.new('uint64_t*')
        data = C.extract_payload(frame, uint64)
        data = ffi.buffer(data, uint64[0])[:]
        assert b'Hello' == data
Beispiel #4
0
    def test_pycws_cffi_parse_input_fragmented_text(self):
        FRAG1 = bytes([0x80, 0x02, 0x6c, 0x67])
        FRAG2 = bytes([0x88, 0x00])

        frame = ffi.new('const uint8_t[]', FRAG1)
        frame_len = ffi.cast('size_t', len(frame))
        ws_type = C.ws_parse_input_frame(frame, frame_len)
        assert C.WS_INCOMPLETE_FRAME == ws_type

        frame = ffi.new('const uint8_t[]', FRAG2)
        frame_len = ffi.cast('size_t', len(frame))
        ws_type = C.ws_parse_input_frame(frame, frame_len)
        assert C.WS_CLOSING_FRAME == ws_type
Beispiel #5
0
    def test_pycws_cffi_parse_handshake(self):
        nullhandshake = ffi.new('struct handshake*')
        C.nullhandshake(nullhandshake)

        requests = (CLIENT_HS1, CLIENT_HS2, CLIENT_HS3)

        for request_data in requests:
            request = ffi.new('char[]', request_data)
            request_len = ffi.cast('size_t', len(request))
            ws_type = C.ws_parse_handshake(request,
                                           request_len,
                                           nullhandshake)

            assert C.WS_OPENING_FRAME == ws_type
Beispiel #6
0
    def test_pycws_cffi_parse_bad_handshake(self):
        nullhandshake = ffi.new('struct handshake*')
        C.nullhandshake(nullhandshake)

        requests = (b'GET', b'GET /', b'GET /index.html HTTP/1.1')

        for request_data in requests:
            request = ffi.new('const uint8_t[]', request_data)
            request_len = ffi.cast('size_t', len(request))
            ws_type = C.ws_parse_handshake(request,
                                        request_len,
                                        nullhandshake)

            assert C.WS_ERROR_FRAME == ws_type
Beispiel #7
0
    def test_pycws_cffi_extract_payload(self):
        LEN_256 = bytes([0x82, 0x7e, 0x01, 0x00, 0x1, 0x1, 0x1, 0x1])


        MASKED_LEN_256 = bytes([0x82, 0xFe, 0x01, 0x00, 0x37, 0xfa, 0x21, 0x3d] + ([0x01] * 256))

        for request in (LEN_256, MASKED_LEN_256, ):
            frame = ffi.new('const uint8_t[]', request)
            frame_len = ffi.cast('size_t', len(frame))
            uint64 = ffi.new('uint64_t*')

            ws_type = C.ws_parse_input_frame(frame, frame_len)
            data = C.extract_payload(frame, uint64)
            assert C.WS_BINARY_FRAME == ws_type
            assert 256 == uint64[0]
Beispiel #8
0
    def test_pycws_cffi_nullhandshake(self):
        nullhandshake = ffi.new('struct handshake*')
        C.nullhandshake(nullhandshake)

        assert C.nullhandshake
        assert nullhandshake.resource == ffi.NULL
        assert nullhandshake.host == ffi.NULL
        assert nullhandshake.origin == ffi.NULL
        assert nullhandshake.protocol == ffi.NULL
        assert nullhandshake.key == ffi.NULL
Beispiel #9
0
def get_handshake_answer(handshake):
    out_data = ffi.new('uint8_t*')
    size_t = ffi.new('size_t*')
    C.ws_get_handshake_answer(handshake, out_data, size_t)
    return ffi.buffer(out_data, size_t)[:]