Example #1
0
    def test_inflate_deflate_default(self):
        input = b'hello' + b'-' * 30000 + b'hello'
        inflater15 = util._Inflater(15)
        deflater15 = util._Deflater(15)
        inflater8 = util._Inflater(8)
        deflater8 = util._Deflater(8)

        compressed15 = deflater15.compress_and_finish(input)
        compressed8 = deflater8.compress_and_finish(input)

        inflater15.append(compressed15)
        inflater8.append(compressed8)

        self.assertNotEqual(compressed15, compressed8)
        self.assertEqual(input, inflater15.decompress(-1))
        self.assertEqual(input, inflater8.decompress(-1))
Example #2
0
    def test_random_section(self):
        random.seed(a=0)
        source = ''.join(
            [chr(random.randint(0, 255)) for i in xrange(100 * 1024)])

        chunked_input = get_random_section(source, 10)
        print "Input chunk sizes: %r" % [len(c) for c in chunked_input]

        deflater = util._Deflater(15)
        compressed = []
        for chunk in chunked_input:
            compressed.append(deflater.compress(chunk))
        compressed.append(deflater.compress_and_finish(''))

        chunked_expectation = get_random_section(source, 10)
        print("Expectation chunk sizes: %r" %
              [len(c) for c in chunked_expectation])

        inflater = util._Inflater(15)
        inflater.append(''.join(compressed))
        for chunk in chunked_expectation:
            decompressed = inflater.decompress(len(chunk))
            self.assertEqual(chunk, decompressed)

        self.assertEqual('', inflater.decompress(-1))
Example #3
0
    def test_random_section(self):
        random.seed(a=0)
        source = ''.join(
            [chr(random.randint(0, 255)) for i in xrange(100 * 1024)])

        chunked_input = get_random_section(source, 10)
        print "Input chunk sizes: %r" % [len(c) for c in chunked_input]

        deflater = util._Deflater(15)
        compressed = []
        for chunk in chunked_input:
            compressed.append(deflater.compress(chunk))
        compressed.append(deflater.compress_and_finish(''))

        chunked_expectation = get_random_section(source, 10)
        print ("Expectation chunk sizes: %r" %
               [len(c) for c in chunked_expectation])

        inflater = util._Inflater(15)
        inflater.append(''.join(compressed))
        for chunk in chunked_expectation:
            decompressed = inflater.decompress(len(chunk))
            self.assertEqual(chunk, decompressed)

        self.assertEqual('', inflater.decompress(-1))
Example #4
0
    def test_inflate_deflate_default(self):
        input = b'hello' + '-' * 30000 + b'hello'
        inflater15 = util._Inflater(15)
        deflater15 = util._Deflater(15)
        inflater8 = util._Inflater(8)
        deflater8 = util._Deflater(8)

        compressed15 = deflater15.compress_and_finish(input)
        compressed8 = deflater8.compress_and_finish(input)

        inflater15.append(compressed15)
        inflater8.append(compressed8)

        self.assertNotEqual(compressed15, compressed8)
        self.assertEqual(input, inflater15.decompress(-1))
        self.assertEqual(input, inflater8.decompress(-1))
Example #5
0
    def test_random_section(self):
        random.seed(a=0)
        source = b''.join(
            [int2byte(random.randint(0, 255)) for i in range(100 * 1024)])

        chunked_input = get_random_section(source, 10)

        deflater = util._Deflater(15)
        compressed = []
        for chunk in chunked_input:
            compressed.append(deflater.compress(chunk))
        compressed.append(deflater.compress_and_finish(b''))

        chunked_expectation = get_random_section(source, 10)

        inflater = util._Inflater(15)
        inflater.append(b''.join(compressed))
        for chunk in chunked_expectation:
            decompressed = inflater.decompress(len(chunk))
            self.assertEqual(chunk, decompressed)

        self.assertEqual(b'', inflater.decompress(-1))
def web_socket_transfer_data(request):
    processor = _get_permessage_deflate_extension_processor(request)
    processor.set_bfinal(_bfinal)
    inflater = util._Inflater(_client_max_window_bits)
    while True:
        compress, possibly_compressed_body = receive(request)
        body = None
        if possibly_compressed_body is None:
            return
        if compress:
            inflater.append(possibly_compressed_body)
            body = inflater.decompress(-1)
        else:
            body = possibly_compressed_body

        text = body.decode('utf-8')
        if processor:
            if text == _ENABLE_MESSAGE:
                processor.enable_outgoing_compression()
            elif text == _DISABLE_MESSAGE:
                processor.disable_outgoing_compression()
        request.ws_stream.send_message(text, binary=False)
        if text == _GOODBYE_MESSAGE:
            return
Example #7
0
def web_socket_transfer_data(request):
    processor = _get_permessage_deflate_extension_processor(request)
    processor.set_bfinal(_bfinal)
    inflater = util._Inflater(_client_max_window_bits)
    while True:
        compress, possibly_compressed_body = receive(request)
        body = None
        if possibly_compressed_body is None:
            return
        if compress:
            inflater.append(possibly_compressed_body)
            body = inflater.decompress(-1)
        else:
            body = possibly_compressed_body

        text = body.decode('utf-8')
        if processor:
            if text == _ENABLE_MESSAGE:
                processor.enable_outgoing_compression()
            elif text == _DISABLE_MESSAGE:
                processor.disable_outgoing_compression()
        request.ws_stream.send_message(text, binary=False)
        if text == _GOODBYE_MESSAGE:
            return