From 9883509f894dde57c8a71340a69581ac46c44f51 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 12:44:29 +0200 Subject: [PATCH 01/14] simplify default ssl params for test servers --- netlib/test.py | 30 +++++++++++++------ test/test_tcp.py | 76 +++++++----------------------------------------- 2 files changed, 32 insertions(+), 74 deletions(-) diff --git a/netlib/test.py b/netlib/test.py index 14f501577..ee8c66852 100644 --- a/netlib/test.py +++ b/netlib/test.py @@ -4,6 +4,7 @@ import Queue import cStringIO import OpenSSL from . import tcp, certutils +import tutils class ServerThread(threading.Thread): @@ -55,22 +56,33 @@ class TServer(tcp.TCPServer): dhparams, v3_only """ tcp.TCPServer.__init__(self, addr) - self.ssl, self.q = ssl, q + + if ssl is True: + self.ssl = dict() + elif isinstance(ssl, dict): + self.ssl = ssl + else: + self.ssl = None + + self.q = q self.handler_klass = handler_klass self.last_handler = None def handle_client_connection(self, request, client_address): h = self.handler_klass(request, client_address, self) self.last_handler = h - if self.ssl: - cert = certutils.SSLCert.from_pem( - file(self.ssl["cert"], "rb").read() - ) - raw = file(self.ssl["key"], "rb").read() + if self.ssl is not None: + raw_cert = self.ssl.get( + "cert", + tutils.test_data.path("data/server.crt")) + cert = certutils.SSLCert.from_pem(file(raw_cert, "rb").read()) + raw_key = self.ssl.get( + "key", + tutils.test_data.path("data/server.key")) key = OpenSSL.crypto.load_privatekey( OpenSSL.crypto.FILETYPE_PEM, - raw) - if self.ssl["v3_only"]: + file(raw_key, "rb").read()) + if self.ssl.get("v3_only", False): method = tcp.SSLv3_METHOD options = OpenSSL.SSL.OP_NO_SSLv2 | OpenSSL.SSL.OP_NO_TLSv1 else: @@ -81,7 +93,7 @@ class TServer(tcp.TCPServer): method=method, options=options, handle_sni=getattr(h, "handle_sni", None), - request_client_cert=self.ssl["request_client_cert"], + request_client_cert=self.ssl.get("request_client_cert", None), cipher_list=self.ssl.get("cipher_list", None), dhparams=self.ssl.get("dhparams", None), chain_file=self.ssl.get("chain_file", None), diff --git a/test/test_tcp.py b/test/test_tcp.py index 14ba555d5..cbe92f3c8 100644 --- a/test/test_tcp.py +++ b/test/test_tcp.py @@ -135,10 +135,6 @@ class TestFinishFail(test.ServerTestBase): class TestServerSSL(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list="AES256-SHA", chain_file=tutils.test_data.path("data/server.crt") ) @@ -165,8 +161,6 @@ class TestServerSSL(test.ServerTestBase): class TestSSLv3Only(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), request_client_cert=False, v3_only=True ) @@ -188,9 +182,8 @@ class TestSSLClientCert(test.ServerTestBase): def handle(self): self.wfile.write("%s\n" % self.clientcert.serial) self.wfile.flush() + ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), request_client_cert=True, v3_only=False ) @@ -224,12 +217,7 @@ class TestSNI(test.ServerTestBase): self.wfile.write(self.sni) self.wfile.flush() - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -242,10 +230,6 @@ class TestSNI(test.ServerTestBase): class TestServerCipherList(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -264,11 +248,8 @@ class TestServerCurrentCipher(test.ServerTestBase): def handle(self): self.wfile.write("%s" % str(self.get_current_cipher())) self.wfile.flush() + ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -282,10 +263,6 @@ class TestServerCurrentCipher(test.ServerTestBase): class TestServerCipherListError(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='bogus' ) @@ -298,10 +275,6 @@ class TestServerCipherListError(test.ServerTestBase): class TestClientCipherListError(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -321,12 +294,8 @@ class TestSSLDisconnect(test.ServerTestBase): def handle(self): self.finish() - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -341,12 +310,7 @@ class TestSSLDisconnect(test.ServerTestBase): class TestSSLHardDisconnect(test.ServerTestBase): handler = HardDisconnectHandler - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -400,13 +364,9 @@ class TestTimeOut(test.ServerTestBase): class TestALPN(test.ServerTestBase): - handler = HangHandler + handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, - alpn_select="h2" + alpn_select="foobar" ) if OpenSSL._util.lib.Cryptography_HAS_ALPN: @@ -414,19 +374,13 @@ class TestALPN(test.ServerTestBase): def test_alpn(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() - c.convert_to_ssl(alpn_protos=["h2"]) - print "ALPN: %s" % c.get_alpn_proto_negotiated() - assert c.get_alpn_proto_negotiated() == "h2" + c.convert_to_ssl(alpn_protos=["foobar"]) + assert c.get_alpn_proto_negotiated() == "foobar" class TestSSLTimeOut(test.ServerTestBase): handler = HangHandler - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_timeout_client(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -439,10 +393,6 @@ class TestSSLTimeOut(test.ServerTestBase): class TestDHParams(test.ServerTestBase): handler = HangHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, dhparams=certutils.CertStore.load_dhparam( tutils.test_data.path("data/dhparam.pem"), ), @@ -643,10 +593,6 @@ class TestAddress: class TestSSLKeyLogger(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list="AES256-SHA" ) From 436291764c4e557155d7e4e87482a4e378a2ccce Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 15:14:31 +0200 Subject: [PATCH 02/14] http2: fix default settings --- netlib/h2/h2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netlib/h2/h2.py b/netlib/h2/h2.py index 707b1465b..227139a33 100644 --- a/netlib/h2/h2.py +++ b/netlib/h2/h2.py @@ -29,8 +29,8 @@ class H2Client(tcp.TCPClient): SettingsFrame.SETTINGS.SETTINGS_HEADER_TABLE_SIZE: 4096, SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: None, - SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 2 ^ 16 - 1, - SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE: 2 ^ 14, + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 2 ** 16 - 1, + SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE: 2 ** 14, SettingsFrame.SETTINGS.SETTINGS_MAX_HEADER_LIST_SIZE: None, } From b84001e8f082a9198f56037aa6861a360d5d76cf Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 15:16:00 +0200 Subject: [PATCH 03/14] http2: explicitly mention all arguments in tests --- test/h2/test_frames.py | 325 +++++++++++++++++++++++++---------------- 1 file changed, 203 insertions(+), 122 deletions(-) diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 313ef4054..310336b07 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,43 +3,59 @@ import tutils from nose.tools import assert_equal -# TODO test stream association if valid or not - - def test_invalid_flags(): tutils.raises( ValueError, DataFrame, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + payload='foobar') def test_frame_equality(): - a = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') - b = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') + a = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') + b = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') assert_equal(a, b) def test_too_large_frames(): - DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567) + DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567) def test_data_frame_to_bytes(): - f = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') + f = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') assert_equal(f.to_bytes().encode('hex'), '000006000101234567666f6f626172') f = DataFrame( - 11, - Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, - 0x1234567, - 'foobar', + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', pad_length=3) assert_equal( f.to_bytes().encode('hex'), '00000a00090123456703666f6f626172000000') - f = DataFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar') + f = DataFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload='foobar') tutils.raises(ValueError, f.to_bytes) @@ -63,26 +79,26 @@ def test_data_frame_from_bytes(): def test_data_frame_human_readable(): f = DataFrame( - 11, - Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, - 0x1234567, - 'foobar', + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', pad_length=3) assert f.human_readable() def test_headers_frame_to_bytes(): f = HeadersFrame( - 6, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=6, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, headers=[('host', 'foo.bar')]) assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') f = HeadersFrame( - 10, - HeadersFrame.FLAG_PADDED, - 0x1234567, + length=10, + flags=(HeadersFrame.FLAG_PADDED), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3) assert_equal( @@ -90,9 +106,9 @@ def test_headers_frame_to_bytes(): '00000b01080123456703668594e75e31d9000000') f = HeadersFrame( - 10, - HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=10, + flags=(HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], exclusive=True, stream_dependency=0x7654321, @@ -102,9 +118,9 @@ def test_headers_frame_to_bytes(): '00000c012001234567876543212a668594e75e31d9') f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=True, @@ -115,9 +131,9 @@ def test_headers_frame_to_bytes(): '00001001280123456703876543212a668594e75e31d9000000') f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=False, @@ -127,7 +143,11 @@ def test_headers_frame_to_bytes(): f.to_bytes().encode('hex'), '00001001280123456703076543212a668594e75e31d9000000') - f = HeadersFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar') + f = HeadersFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + headers=[('host', 'foo.bar')]) tutils.raises(ValueError, f.to_bytes) @@ -188,9 +208,9 @@ def test_headers_frame_from_bytes(): def test_headers_frame_human_readable(): f = HeadersFrame( - 7, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=7, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[], pad_length=3, exclusive=False, @@ -199,9 +219,9 @@ def test_headers_frame_human_readable(): assert f.human_readable() f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=False, @@ -212,27 +232,35 @@ def test_headers_frame_human_readable(): def test_priority_frame_to_bytes(): f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, exclusive=True, stream_dependency=0x7654321, weight=42) assert_equal(f.to_bytes().encode('hex'), '000005020001234567876543212a') f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21) assert_equal(f.to_bytes().encode('hex'), '0000050200012345670765432115') - f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x0, stream_dependency=0x1234567) + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + stream_dependency=0x1234567) tutils.raises(ValueError, f.to_bytes) - f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x1234567, stream_dependency=0x0) + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + stream_dependency=0x0) tutils.raises(ValueError, f.to_bytes) @@ -260,9 +288,9 @@ def test_priority_frame_from_bytes(): def test_priority_frame_human_readable(): f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21) @@ -270,10 +298,17 @@ def test_priority_frame_human_readable(): def test_rst_stream_frame_to_bytes(): - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, error_code=0x7654321) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) assert_equal(f.to_bytes().encode('hex'), '00000403000123456707654321') - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x0) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) tutils.raises(ValueError, f.to_bytes) @@ -288,28 +323,39 @@ def test_rst_stream_frame_from_bytes(): def test_rst_stream_frame_human_readable(): - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, error_code=0x7654321) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) assert f.human_readable() def test_settings_frame_to_bytes(): - f = SettingsFrame(0, Frame.FLAG_NO_FLAGS, 0x0) + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) assert_equal(f.to_bytes().encode('hex'), '000000040000000000') - f = SettingsFrame(0, SettingsFrame.FLAG_ACK, 0x0) + f = SettingsFrame( + length=0, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0) assert_equal(f.to_bytes().encode('hex'), '000000040100000000') f = SettingsFrame( - 6, - SettingsFrame.FLAG_ACK, 0x0, + length=6, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1}) assert_equal(f.to_bytes().encode('hex'), '000006040100000000000200000001') f = SettingsFrame( - 12, - Frame.FLAG_NO_FLAGS, - 0x0, + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) @@ -317,7 +363,10 @@ def test_settings_frame_to_bytes(): f.to_bytes().encode('hex'), '00000c040000000000000200000001000312345678') - f = SettingsFrame(0, Frame.FLAG_NO_FLAGS, 0x1234567) + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) tutils.raises(ValueError, f.to_bytes) @@ -361,13 +410,17 @@ def test_settings_frame_from_bytes(): def test_settings_frame_human_readable(): - f = SettingsFrame(12, Frame.FLAG_NO_FLAGS, 0x0, settings={}) + f = SettingsFrame( + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + settings={}) assert f.human_readable() f = SettingsFrame( - 12, - Frame.FLAG_NO_FLAGS, - 0x0, + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) @@ -376,30 +429,38 @@ def test_settings_frame_human_readable(): def test_push_promise_frame_to_bytes(): f = PushPromiseFrame( - 10, - Frame.FLAG_NO_FLAGS, - 0x1234567, - 0x7654321, - 'foobar') + length=10, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar') assert_equal( f.to_bytes().encode('hex'), '00000a05000123456707654321666f6f626172') f = PushPromiseFrame( - 14, - HeadersFrame.FLAG_PADDED, - 0x1234567, - 0x7654321, - 'foobar', + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', pad_length=3) assert_equal( f.to_bytes().encode('hex'), '00000e0508012345670307654321666f6f626172000000') - f = PushPromiseFrame(4, Frame.FLAG_NO_FLAGS, 0x0, 0x1234567) + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + promised_stream=0x1234567) tutils.raises(ValueError, f.to_bytes) - f = PushPromiseFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, 0x0) + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x0) tutils.raises(ValueError, f.to_bytes) @@ -424,27 +485,38 @@ def test_push_promise_frame_from_bytes(): def test_push_promise_frame_human_readable(): f = PushPromiseFrame( - 14, - HeadersFrame.FLAG_PADDED, - 0x1234567, - 0x7654321, - 'foobar', + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', pad_length=3) assert f.human_readable() def test_ping_frame_to_bytes(): - f = PingFrame(8, PingFrame.FLAG_ACK, 0x0, payload=b'foobar') + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') assert_equal( f.to_bytes().encode('hex'), '000008060100000000666f6f6261720000') - f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x0, payload=b'foobardeadbeef') + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload=b'foobardeadbeef') assert_equal( f.to_bytes().encode('hex'), '000008060000000000666f6f6261726465') - f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x1234567) + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) tutils.raises(ValueError, f.to_bytes) @@ -467,15 +539,19 @@ def test_ping_frame_from_bytes(): def test_ping_frame_human_readable(): - f = PingFrame(8, PingFrame.FLAG_ACK, 0x0, payload=b'foobar') + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') assert f.human_readable() def test_goaway_frame_to_bytes(): f = GoAwayFrame( - 8, - Frame.FLAG_NO_FLAGS, - 0x0, + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'') @@ -484,9 +560,9 @@ def test_goaway_frame_to_bytes(): '0000080700000000000123456787654321') f = GoAwayFrame( - 14, - Frame.FLAG_NO_FLAGS, - 0x0, + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar') @@ -495,16 +571,17 @@ def test_goaway_frame_to_bytes(): '00000e0700000000000123456787654321666f6f626172') f = GoAwayFrame( - 8, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, last_stream=0x1234567, error_code=0x87654321) tutils.raises(ValueError, f.to_bytes) def test_goaway_frame_from_bytes(): - f = Frame.from_bytes('0000080700000000000123456787654321'.decode('hex')) + f = Frame.from_bytes( + '0000080700000000000123456787654321'.decode('hex')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -528,9 +605,9 @@ def test_goaway_frame_from_bytes(): def test_go_away_frame_human_readable(): f = GoAwayFrame( - 14, - Frame.FLAG_NO_FLAGS, - 0x0, + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar') @@ -539,23 +616,23 @@ def test_go_away_frame_human_readable(): def test_window_update_frame_to_bytes(): f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x0, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, window_size_increment=0x1234567) assert_equal(f.to_bytes().encode('hex'), '00000408000000000001234567') f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, window_size_increment=0x7654321) assert_equal(f.to_bytes().encode('hex'), '00000408000123456707654321') f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x0, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, window_size_increment=0xdeadbeef) tutils.raises(ValueError, f.to_bytes) @@ -575,22 +652,26 @@ def test_window_update_frame_from_bytes(): def test_window_update_frame_human_readable(): f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, window_size_increment=0x7654321) assert f.human_readable() def test_continuation_frame_to_bytes(): f = ContinuationFrame( - 6, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') assert_equal(f.to_bytes().encode('hex'), '000006090401234567666f6f626172') - f = ContinuationFrame(6, ContinuationFrame.FLAG_END_HEADERS, 0x0, 'foobar') + f = ContinuationFrame( + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x0, + header_block_fragment='foobar') tutils.raises(ValueError, f.to_bytes) @@ -606,8 +687,8 @@ def test_continuation_frame_from_bytes(): def test_continuation_frame_human_readable(): f = ContinuationFrame( - 6, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') assert f.human_readable() From e4c129026fbf4228c13ae64da19a9a85fc7ff2a5 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 15:17:50 +0200 Subject: [PATCH 04/14] http2: introduce state for connection objects --- netlib/h2/frame.py | 102 ++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/netlib/h2/frame.py b/netlib/h2/frame.py index 36456c46f..174ceebd0 100644 --- a/netlib/h2/frame.py +++ b/netlib/h2/frame.py @@ -20,18 +20,28 @@ class Frame(object): FLAG_PADDED = 0x8 FLAG_PRIORITY = 0x20 - def __init__(self, length, flags, stream_id): + def __init__(self, state=None, length=0, flags=FLAG_NO_FLAGS, stream_id=0x0): valid_flags = reduce(lambda x, y: x | y, self.VALID_FLAGS, 0x0) if flags | valid_flags != valid_flags: raise ValueError('invalid flags detected.') + if state is None: + class State(object): + pass + + state = State() + state.encoder = Encoder() + state.decoder = Decoder() + + self.state = state + self.length = length self.type = self.TYPE self.flags = flags self.stream_id = stream_id @classmethod - def from_file(self, fp): + def from_file(self, fp, state=None): """ read a HTTP/2 frame sent by a server or client fp is a "file like" object that could be backed by a network @@ -45,16 +55,16 @@ class Frame(object): stream_id = fields[4] payload = fp.safe_read(length) - return FRAMES[fields[2]].from_bytes(length, flags, stream_id, payload) + return FRAMES[fields[2]].from_bytes(state, length, flags, stream_id, payload) @classmethod - def from_bytes(self, data): + def from_bytes(self, data, state=None): fields = struct.unpack("!HBBBL", data[:9]) length = (fields[0] << 8) + fields[1] # type is already deducted from class flags = fields[3] stream_id = fields[4] - return FRAMES[fields[2]].from_bytes(length, flags, stream_id, data[9:]) + return FRAMES[fields[2]].from_bytes(state, length, flags, stream_id, data[9:]) def to_bytes(self): payload = self.payload_bytes() @@ -96,18 +106,19 @@ class DataFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, payload=b'', pad_length=0): - super(DataFrame, self).__init__(length, flags, stream_id) + super(DataFrame, self).__init__(state, length, flags, stream_id) self.payload = payload self.pad_length = pad_length @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) if f.flags & self.FLAG_PADDED: f.pad_length = struct.unpack('!B', payload[0])[0] @@ -146,6 +157,7 @@ class HeadersFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, @@ -154,7 +166,7 @@ class HeadersFrame(Frame): exclusive=False, stream_dependency=0x0, weight=0): - super(HeadersFrame, self).__init__(length, flags, stream_id) + super(HeadersFrame, self).__init__(state, length, flags, stream_id) if headers is None: headers = [] @@ -166,8 +178,8 @@ class HeadersFrame(Frame): self.weight = weight @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) if f.flags & self.FLAG_PADDED: f.pad_length = struct.unpack('!B', payload[0])[0] @@ -177,18 +189,22 @@ class HeadersFrame(Frame): if f.flags & self.FLAG_PRIORITY: f.stream_dependency, f.weight = struct.unpack( - '!LB', header_block_fragment[ - :5]) + '!LB', header_block_fragment[:5]) f.exclusive = bool(f.stream_dependency >> 31) f.stream_dependency &= 0x7FFFFFFF header_block_fragment = header_block_fragment[5:] - for header, value in Decoder().decode(header_block_fragment): + for header, value in f.state.decoder.decode(header_block_fragment): f.headers.append((header, value)) return f def payload_bytes(self): + """ + This encodes all headers with HPACK + Do NOT call this method twice - it will change the encoder state! + """ + if self.stream_id == 0x0: raise ValueError('HEADERS frames MUST be associated with a stream.') @@ -201,7 +217,7 @@ class HeadersFrame(Frame): (int(self.exclusive) << 31) | self.stream_dependency, self.weight) - b += Encoder().encode(self.headers) + b += self.state.encoder.encode(self.headers) if self.flags & self.FLAG_PADDED: b += b'\0' * self.pad_length @@ -234,20 +250,21 @@ class PriorityFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, exclusive=False, stream_dependency=0x0, weight=0): - super(PriorityFrame, self).__init__(length, flags, stream_id) + super(PriorityFrame, self).__init__(state, length, flags, stream_id) self.exclusive = exclusive self.stream_dependency = stream_dependency self.weight = weight @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.stream_dependency, f.weight = struct.unpack('!LB', payload) f.exclusive = bool(f.stream_dependency >> 31) @@ -283,16 +300,17 @@ class RstStreamFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, error_code=0x0): - super(RstStreamFrame, self).__init__(length, flags, stream_id) + super(RstStreamFrame, self).__init__(state, length, flags, stream_id) self.error_code = error_code @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.error_code = struct.unpack('!L', payload)[0] return f @@ -322,11 +340,12 @@ class SettingsFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, settings=None): - super(SettingsFrame, self).__init__(length, flags, stream_id) + super(SettingsFrame, self).__init__(state, length, flags, stream_id) if settings is None: settings = {} @@ -334,8 +353,8 @@ class SettingsFrame(Frame): self.settings = settings @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) for i in xrange(0, len(payload), 6): identifier, value = struct.unpack("!HL", payload[i:i + 6]) @@ -372,20 +391,21 @@ class PushPromiseFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, promised_stream=0x0, header_block_fragment=b'', pad_length=0): - super(PushPromiseFrame, self).__init__(length, flags, stream_id) + super(PushPromiseFrame, self).__init__(state, length, flags, stream_id) self.pad_length = pad_length self.promised_stream = promised_stream self.header_block_fragment = header_block_fragment @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) if f.flags & self.FLAG_PADDED: f.pad_length, f.promised_stream = struct.unpack('!BL', payload[:5]) @@ -435,16 +455,17 @@ class PingFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, payload=b''): - super(PingFrame, self).__init__(length, flags, stream_id) + super(PingFrame, self).__init__(state, length, flags, stream_id) self.payload = payload @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.payload = payload return f @@ -467,20 +488,21 @@ class GoAwayFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, last_stream=0x0, error_code=0x0, data=b''): - super(GoAwayFrame, self).__init__(length, flags, stream_id) + super(GoAwayFrame, self).__init__(state, length, flags, stream_id) self.last_stream = last_stream self.error_code = error_code self.data = data @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.last_stream, f.error_code = struct.unpack("!LL", payload[:8]) f.last_stream &= 0x7FFFFFFF @@ -511,16 +533,17 @@ class WindowUpdateFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, window_size_increment=0x0): - super(WindowUpdateFrame, self).__init__(length, flags, stream_id) + super(WindowUpdateFrame, self).__init__(state, length, flags, stream_id) self.window_size_increment = window_size_increment @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.window_size_increment = struct.unpack("!L", payload)[0] f.window_size_increment &= 0x7FFFFFFF @@ -544,16 +567,17 @@ class ContinuationFrame(Frame): def __init__( self, + state=None, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, header_block_fragment=b''): - super(ContinuationFrame, self).__init__(length, flags, stream_id) + super(ContinuationFrame, self).__init__(state, length, flags, stream_id) self.header_block_fragment = header_block_fragment @classmethod - def from_bytes(self, length, flags, stream_id, payload): - f = self(length=length, flags=flags, stream_id=stream_id) + def from_bytes(self, state, length, flags, stream_id, payload): + f = self(state=state, length=length, flags=flags, stream_id=stream_id) f.header_block_fragment = payload return f From 5cecbdc1687346bb2bf139c904ffda2b37dc8276 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 12:34:50 +0200 Subject: [PATCH 05/14] http2: add basic protocol handling --- netlib/h2/__init__.py | 169 +++++++++++++++++++++++++++++++++++++++++ netlib/h2/frame.py | 50 ++++++++++-- netlib/h2/h2.py | 89 ---------------------- test/h2/example.py | 18 ----- test/h2/test_frames.py | 1 + 5 files changed, 212 insertions(+), 115 deletions(-) delete mode 100644 netlib/h2/h2.py delete mode 100644 test/h2/example.py diff --git a/netlib/h2/__init__.py b/netlib/h2/__init__.py index 9b4faa337..054ba91c4 100644 --- a/netlib/h2/__init__.py +++ b/netlib/h2/__init__.py @@ -1 +1,170 @@ from __future__ import (absolute_import, print_function, division) +import itertools + +from .. import utils +from .frame import * + + +class HTTP2Protocol(object): + + ERROR_CODES = utils.BiDi( + NO_ERROR=0x0, + PROTOCOL_ERROR=0x1, + INTERNAL_ERROR=0x2, + FLOW_CONTROL_ERROR=0x3, + SETTINGS_TIMEOUT=0x4, + STREAM_CLOSED=0x5, + FRAME_SIZE_ERROR=0x6, + REFUSED_STREAM=0x7, + CANCEL=0x8, + COMPRESSION_ERROR=0x9, + CONNECT_ERROR=0xa, + ENHANCE_YOUR_CALM=0xb, + INADEQUATE_SECURITY=0xc, + HTTP_1_1_REQUIRED=0xd + ) + + # "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" + CLIENT_CONNECTION_PREFACE = '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a' + + ALPN_PROTO_H2 = b'h2' + + HTTP2_DEFAULT_SETTINGS = { + SettingsFrame.SETTINGS.SETTINGS_HEADER_TABLE_SIZE: 4096, + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: None, + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 2 ** 16 - 1, + SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE: 2 ** 14, + SettingsFrame.SETTINGS.SETTINGS_MAX_HEADER_LIST_SIZE: None, + } + + def __init__(self): + self.http2_settings = self.HTTP2_DEFAULT_SETTINGS.copy() + self.current_stream_id = None + self.encoder = Encoder() + self.decoder = Decoder() + + def check_alpn(self): + alp = self.get_alpn_proto_negotiated() + if alp != self.ALPN_PROTO_H2: + raise NotImplementedError( + "H2Client can not handle unknown ALP: %s" % alp) + print("-> Successfully negotiated 'h2' application layer protocol.") + + def send_connection_preface(self): + self.wfile.write(bytes(self.CLIENT_CONNECTION_PREFACE.decode('hex'))) + self.send_frame(SettingsFrame(state=self)) + + frame = Frame.from_file(self.rfile, self) + assert isinstance(frame, SettingsFrame) + self._apply_settings(frame.settings) + self.read_frame() # read setting ACK frame + + print("-> Connection Preface completed.") + + def next_stream_id(self): + if self.current_stream_id is None: + self.current_stream_id = 1 + else: + self.current_stream_id += 2 + return self.current_stream_id + + def send_frame(self, frame): + raw_bytes = frame.to_bytes() + self.wfile.write(raw_bytes) + self.wfile.flush() + + def read_frame(self): + frame = Frame.from_file(self.rfile, self) + if isinstance(frame, SettingsFrame): + self._apply_settings(frame.settings) + + return frame + + def _apply_settings(self, settings): + for setting, value in settings.items(): + old_value = self.http2_settings[setting] + if not old_value: + old_value = '-' + + self.http2_settings[setting] = value + print("-> Setting changed: %s to %d (was %s)" % ( + SettingsFrame.SETTINGS.get_name(setting), + value, + str(old_value))) + + self.send_frame(SettingsFrame(state=self, flags=Frame.FLAG_ACK)) + print("-> New settings acknowledged.") + + def _create_headers(self, headers, stream_id, end_stream=True): + # TODO: implement max frame size checks and sending in chunks + + flags = Frame.FLAG_END_HEADERS + if end_stream: + flags |= Frame.FLAG_END_STREAM + + bytes = HeadersFrame( + state=self, + flags=flags, + stream_id=stream_id, + headers=headers).to_bytes() + return [bytes] + + def _create_body(self, body, stream_id): + if body is None or len(body) == 0: + return b'' + + # TODO: implement max frame size checks and sending in chunks + # TODO: implement flow-control window + + bytes = DataFrame( + state=self, + flags=Frame.FLAG_END_STREAM, + stream_id=stream_id, + payload=body).to_bytes() + return [bytes] + + def create_request(self, method, path, headers=None, body=None): + if headers is None: + headers = [] + + headers = [ + (b':method', bytes(method)), + (b':path', bytes(path)), + (b':scheme', b'https')] + headers + + stream_id = self.next_stream_id() + + return list(itertools.chain( + self._create_headers(headers, stream_id, end_stream=(body is None)), + self._create_body(body, stream_id))) + + def read_response(self): + header_block_fragment = b'' + body = b'' + + while True: + frame = self.read_frame() + if isinstance(frame, HeadersFrame): + header_block_fragment += frame.header_block_fragment + if frame.flags | Frame.FLAG_END_HEADERS: + break + else: + print("Unexpected frame received:") + print(frame.human_readable()) + + while True: + frame = self.read_frame() + if isinstance(frame, DataFrame): + body += frame.payload + if frame.flags | Frame.FLAG_END_STREAM: + break + else: + print("Unexpected frame received:") + print(frame.human_readable()) + + headers = {} + for header, value in self.decoder.decode(header_block_fragment): + headers[header] = value + + return headers[':status'], headers, body diff --git a/netlib/h2/frame.py b/netlib/h2/frame.py index 174ceebd0..137cbb3dd 100644 --- a/netlib/h2/frame.py +++ b/netlib/h2/frame.py @@ -20,16 +20,24 @@ class Frame(object): FLAG_PADDED = 0x8 FLAG_PRIORITY = 0x20 - def __init__(self, state=None, length=0, flags=FLAG_NO_FLAGS, stream_id=0x0): + def __init__( + self, + state=None, + length=0, + flags=FLAG_NO_FLAGS, + stream_id=0x0): valid_flags = reduce(lambda x, y: x | y, self.VALID_FLAGS, 0x0) if flags | valid_flags != valid_flags: raise ValueError('invalid flags detected.') if state is None: + from . import HTTP2Protocol + class State(object): pass state = State() + state.http2_settings = HTTP2Protocol.HTTP2_DEFAULT_SETTINGS.copy() state.encoder = Encoder() state.decoder = Decoder() @@ -40,6 +48,14 @@ class Frame(object): self.flags = flags self.stream_id = stream_id + def _check_frame_size(self, length): + max_length = self.state.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] + if length > max_length: + raise NotImplementedError( + "Frame size exceeded: %d, but only %d allowed." % ( + length, max_length)) + @classmethod def from_file(self, fp, state=None): """ @@ -54,8 +70,15 @@ class Frame(object): flags = fields[3] stream_id = fields[4] + # TODO: check frame size if <= current SETTINGS_MAX_FRAME_SIZE + payload = fp.safe_read(length) - return FRAMES[fields[2]].from_bytes(state, length, flags, stream_id, payload) + return FRAMES[fields[2]].from_bytes( + state, + length, + flags, + stream_id, + payload) @classmethod def from_bytes(self, data, state=None): @@ -64,12 +87,20 @@ class Frame(object): # type is already deducted from class flags = fields[3] stream_id = fields[4] - return FRAMES[fields[2]].from_bytes(state, length, flags, stream_id, data[9:]) + + return FRAMES[fields[2]].from_bytes( + state, + length, + flags, + stream_id, + data[9:]) def to_bytes(self): payload = self.payload_bytes() self.length = len(payload) + self._check_frame_size(self.length) + b = struct.pack('!HB', self.length & 0xFFFF00, self.length & 0x0000FF) b += struct.pack('!B', self.TYPE) b += struct.pack('!B', self.flags) @@ -183,19 +214,20 @@ class HeadersFrame(Frame): if f.flags & self.FLAG_PADDED: f.pad_length = struct.unpack('!B', payload[0])[0] - header_block_fragment = payload[1:-f.pad_length] + f.header_block_fragment = payload[1:-f.pad_length] else: - header_block_fragment = payload[0:] + f.header_block_fragment = payload[0:] if f.flags & self.FLAG_PRIORITY: f.stream_dependency, f.weight = struct.unpack( '!LB', header_block_fragment[:5]) f.exclusive = bool(f.stream_dependency >> 31) f.stream_dependency &= 0x7FFFFFFF - header_block_fragment = header_block_fragment[5:] + f.header_block_fragment = f.header_block_fragment[5:] - for header, value in f.state.decoder.decode(header_block_fragment): - f.headers.append((header, value)) + # TODO only do this if END_HEADERS or something... + # for header, value in f.state.decoder.decode(f.header_block_fragment): + # f.headers.append((header, value)) return f @@ -217,6 +249,8 @@ class HeadersFrame(Frame): (int(self.exclusive) << 31) | self.stream_dependency, self.weight) + # TODO: maybe remove that and only deal with header_block_fragments + # inside frames b += self.state.encoder.encode(self.headers) if self.flags & self.FLAG_PADDED: diff --git a/netlib/h2/h2.py b/netlib/h2/h2.py deleted file mode 100644 index 227139a33..000000000 --- a/netlib/h2/h2.py +++ /dev/null @@ -1,89 +0,0 @@ -from .. import utils, odict, tcp -from frame import * - -# "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" -CLIENT_CONNECTION_PREFACE = '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a' - -ERROR_CODES = utils.BiDi( - NO_ERROR=0x0, - PROTOCOL_ERROR=0x1, - INTERNAL_ERROR=0x2, - FLOW_CONTROL_ERROR=0x3, - SETTINGS_TIMEOUT=0x4, - STREAM_CLOSED=0x5, - FRAME_SIZE_ERROR=0x6, - REFUSED_STREAM=0x7, - CANCEL=0x8, - COMPRESSION_ERROR=0x9, - CONNECT_ERROR=0xa, - ENHANCE_YOUR_CALM=0xb, - INADEQUATE_SECURITY=0xc, - HTTP_1_1_REQUIRED=0xd -) - - -class H2Client(tcp.TCPClient): - ALPN_PROTO_H2 = b'h2' - - DEFAULT_SETTINGS = { - SettingsFrame.SETTINGS.SETTINGS_HEADER_TABLE_SIZE: 4096, - SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, - SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: None, - SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 2 ** 16 - 1, - SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE: 2 ** 14, - SettingsFrame.SETTINGS.SETTINGS_MAX_HEADER_LIST_SIZE: None, - } - - def __init__(self, address, source_address=None): - super(H2Client, self).__init__(address, source_address) - self.settings = self.DEFAULT_SETTINGS.copy() - - def connect(self, send_preface=True): - super(H2Client, self).connect() - self.convert_to_ssl(alpn_protos=[self.ALPN_PROTO_H2]) - - alp = self.get_alpn_proto_negotiated() - if alp != b'h2': - raise NotImplementedError( - "H2Client can not handle unknown protocol: %s" % - alp) - print "-> Successfully negotiated 'h2' application layer protocol." - - if send_preface: - self.wfile.write(bytes(CLIENT_CONNECTION_PREFACE.decode('hex'))) - self.send_frame(SettingsFrame()) - - frame = Frame.from_file(self.rfile) - print frame.human_readable() - assert isinstance(frame, SettingsFrame) - self.apply_settings(frame.settings) - - print "-> Connection Preface completed." - - print "-> H2Client is ready..." - - def send_frame(self, frame): - self.wfile.write(frame.to_bytes()) - self.wfile.flush() - - def read_frame(self): - frame = Frame.from_file(self.rfile) - if isinstance(frame, SettingsFrame): - self.apply_settings(frame.settings) - - return frame - - def apply_settings(self, settings): - for setting, value in settings.items(): - old_value = self.settings[setting] - if not old_value: - old_value = '-' - - self.settings[setting] = value - print "-> Setting changed: %s to %d (was %s)" % - (SettingsFrame.SETTINGS.get_name(setting), - value, - str(old_value)) - - self.send_frame(SettingsFrame(flags=Frame.FLAG_ACK)) - print "-> New settings acknowledged." diff --git a/test/h2/example.py b/test/h2/example.py deleted file mode 100644 index fc4f2f10f..000000000 --- a/test/h2/example.py +++ /dev/null @@ -1,18 +0,0 @@ -from netlib.h2.frame import * -from netlib.h2.h2 import * - -c = H2Client(("127.0.0.1", 443)) -c.connect() - -c.send_frame(HeadersFrame( - flags=(Frame.FLAG_END_HEADERS | Frame.FLAG_END_STREAM), - stream_id=0x1, - headers=[ - (b':method', 'GET'), - (b':path', b'/index.html'), - (b':scheme', b'https'), - (b':authority', b'localhost'), - ])) - -while True: - print c.read_frame().human_readable() diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 310336b07..babf80690 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,6 +3,7 @@ import tutils from nose.tools import assert_equal + def test_invalid_flags(): tutils.raises( ValueError, From 40fa113116a2d3a549bc57c1b1381bbb55c7014b Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 14:11:19 +0200 Subject: [PATCH 06/14] http2: change header_block_fragment handling --- netlib/h2/frame.py | 65 ++++++++---------------- test/h2/test_frames.py | 111 ++++++++++++++++++++++------------------- 2 files changed, 80 insertions(+), 96 deletions(-) diff --git a/netlib/h2/frame.py b/netlib/h2/frame.py index 137cbb3dd..0755c96c4 100644 --- a/netlib/h2/frame.py +++ b/netlib/h2/frame.py @@ -48,13 +48,21 @@ class Frame(object): self.flags = flags self.stream_id = stream_id - def _check_frame_size(self, length): - max_length = self.state.http2_settings[ - SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] - if length > max_length: + @classmethod + def _check_frame_size(self, length, state): + from . import HTTP2Protocol + + if state: + settings = state.http2_settings + else: + settings = HTTP2Protocol.HTTP2_DEFAULT_SETTINGS + + max_frame_size = settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] + + if length > max_frame_size: raise NotImplementedError( "Frame size exceeded: %d, but only %d allowed." % ( - length, max_length)) + length, max_frame_size)) @classmethod def from_file(self, fp, state=None): @@ -70,7 +78,7 @@ class Frame(object): flags = fields[3] stream_id = fields[4] - # TODO: check frame size if <= current SETTINGS_MAX_FRAME_SIZE + self._check_frame_size(length, state) payload = fp.safe_read(length) return FRAMES[fields[2]].from_bytes( @@ -80,26 +88,11 @@ class Frame(object): stream_id, payload) - @classmethod - def from_bytes(self, data, state=None): - fields = struct.unpack("!HBBBL", data[:9]) - length = (fields[0] << 8) + fields[1] - # type is already deducted from class - flags = fields[3] - stream_id = fields[4] - - return FRAMES[fields[2]].from_bytes( - state, - length, - flags, - stream_id, - data[9:]) - def to_bytes(self): payload = self.payload_bytes() self.length = len(payload) - self._check_frame_size(self.length) + self._check_frame_size(self.length, self.state) b = struct.pack('!HB', self.length & 0xFFFF00, self.length & 0x0000FF) b += struct.pack('!B', self.TYPE) @@ -192,17 +185,14 @@ class HeadersFrame(Frame): length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, - headers=None, + header_block_fragment=b'', pad_length=0, exclusive=False, stream_dependency=0x0, weight=0): super(HeadersFrame, self).__init__(state, length, flags, stream_id) - if headers is None: - headers = [] - - self.headers = headers + self.header_block_fragment = header_block_fragment self.pad_length = pad_length self.exclusive = exclusive self.stream_dependency = stream_dependency @@ -220,23 +210,14 @@ class HeadersFrame(Frame): if f.flags & self.FLAG_PRIORITY: f.stream_dependency, f.weight = struct.unpack( - '!LB', header_block_fragment[:5]) + '!LB', f.header_block_fragment[:5]) f.exclusive = bool(f.stream_dependency >> 31) f.stream_dependency &= 0x7FFFFFFF f.header_block_fragment = f.header_block_fragment[5:] - # TODO only do this if END_HEADERS or something... - # for header, value in f.state.decoder.decode(f.header_block_fragment): - # f.headers.append((header, value)) - return f def payload_bytes(self): - """ - This encodes all headers with HPACK - Do NOT call this method twice - it will change the encoder state! - """ - if self.stream_id == 0x0: raise ValueError('HEADERS frames MUST be associated with a stream.') @@ -249,9 +230,7 @@ class HeadersFrame(Frame): (int(self.exclusive) << 31) | self.stream_dependency, self.weight) - # TODO: maybe remove that and only deal with header_block_fragments - # inside frames - b += self.state.encoder.encode(self.headers) + b += self.header_block_fragment if self.flags & self.FLAG_PADDED: b += b'\0' * self.pad_length @@ -269,11 +248,7 @@ class HeadersFrame(Frame): if self.flags & self.FLAG_PADDED: s.append("padding: %d" % self.pad_length) - if not self.headers: - s.append("headers: None") - else: - for header, value in self.headers: - s.append("%s: %s" % (header, value)) + s.append("header_block_fragment: %s" % self.header_block_fragment.encode('hex')) return "\n".join(s) diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index babf80690..30dc71e85 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,6 +3,22 @@ import tutils from nose.tools import assert_equal +class FileAdapter(object): + def __init__(self, data, is_hex=True): + self.position = 0 + if is_hex: + self.data = data.decode('hex') + else: + self.data = data + + def safe_read(self, length): + if self.position + length > len(self.data): + raise ValueError("not enough bytes to read") + + value = self.data[self.position:self.position + length] + self.position += length + return value + def test_invalid_flags(): tutils.raises( @@ -26,14 +42,6 @@ def test_frame_equality(): payload='foobar') assert_equal(a, b) - -def test_too_large_frames(): - DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567) - - def test_data_frame_to_bytes(): f = DataFrame( length=6, @@ -61,7 +69,7 @@ def test_data_frame_to_bytes(): def test_data_frame_from_bytes(): - f = Frame.from_bytes('000006000101234567666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) assert isinstance(f, DataFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, DataFrame.TYPE) @@ -69,7 +77,7 @@ def test_data_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.payload, 'foobar') - f = Frame.from_bytes('00000a00090123456703666f6f626172000000'.decode('hex')) + f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) assert isinstance(f, DataFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, DataFrame.TYPE) @@ -93,14 +101,14 @@ def test_headers_frame_to_bytes(): length=6, flags=(Frame.FLAG_NO_FLAGS), stream_id=0x1234567, - headers=[('host', 'foo.bar')]) + header_block_fragment='668594e75e31d9'.decode('hex')) assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') f = HeadersFrame( length=10, flags=(HeadersFrame.FLAG_PADDED), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3) assert_equal( f.to_bytes().encode('hex'), @@ -110,7 +118,7 @@ def test_headers_frame_to_bytes(): length=10, flags=(HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), exclusive=True, stream_dependency=0x7654321, weight=42) @@ -122,7 +130,7 @@ def test_headers_frame_to_bytes(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=True, stream_dependency=0x7654321, @@ -135,7 +143,7 @@ def test_headers_frame_to_bytes(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -148,60 +156,61 @@ def test_headers_frame_to_bytes(): length=6, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, - headers=[('host', 'foo.bar')]) + header_block_fragment='668594e75e31d9'.decode('hex')) tutils.raises(ValueError, f.to_bytes) def test_headers_frame_from_bytes(): - f = Frame.from_bytes('000007010001234567668594e75e31d9'.decode('hex')) + f = Frame.from_file(FileAdapter( + '000007010001234567668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 7) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_bytes( - '00000b01080123456703668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000b01080123456703668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 11) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_bytes( - '00000c012001234567876543212a668594e75e31d9'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000c012001234567876543212a668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 12) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, True) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes( - '00001001280123456703876543212a668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00001001280123456703876543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, True) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes( - '00001001280123456703076543212a668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00001001280123456703076543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, False) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) @@ -212,7 +221,7 @@ def test_headers_frame_human_readable(): length=7, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[], + header_block_fragment=b'', pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -223,7 +232,7 @@ def test_headers_frame_human_readable(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -266,7 +275,7 @@ def test_priority_frame_to_bytes(): def test_priority_frame_from_bytes(): - f = Frame.from_bytes('000005020001234567876543212a'.decode('hex')) + f = Frame.from_file(FileAdapter('000005020001234567876543212a')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -276,7 +285,7 @@ def test_priority_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes('0000050200012345670765432115'.decode('hex')) + f = Frame.from_file(FileAdapter('0000050200012345670765432115')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -314,7 +323,7 @@ def test_rst_stream_frame_to_bytes(): def test_rst_stream_frame_from_bytes(): - f = Frame.from_bytes('00000403000123456707654321'.decode('hex')) + f = Frame.from_file(FileAdapter('00000403000123456707654321')) assert isinstance(f, RstStreamFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, RstStreamFrame.TYPE) @@ -372,21 +381,21 @@ def test_settings_frame_to_bytes(): def test_settings_frame_from_bytes(): - f = Frame.from_bytes('000000040000000000'.decode('hex')) + f = Frame.from_file(FileAdapter('000000040000000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x0) - f = Frame.from_bytes('000000040100000000'.decode('hex')) + f = Frame.from_file(FileAdapter('000000040100000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, SettingsFrame.FLAG_ACK) assert_equal(f.stream_id, 0x0) - f = Frame.from_bytes('000006040100000000000200000001'.decode('hex')) + f = Frame.from_file(FileAdapter('000006040100000000000200000001')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -395,8 +404,8 @@ def test_settings_frame_from_bytes(): assert_equal(len(f.settings), 1) assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - f = Frame.from_bytes( - '00000c040000000000000200000001000312345678'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000c040000000000000200000001000312345678')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 12) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -466,7 +475,7 @@ def test_push_promise_frame_to_bytes(): def test_push_promise_frame_from_bytes(): - f = Frame.from_bytes('00000a05000123456707654321666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -474,8 +483,8 @@ def test_push_promise_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, 'foobar') - f = Frame.from_bytes( - '00000e0508012345670307654321666f6f626172000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000e0508012345670307654321666f6f626172000000')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 14) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -522,7 +531,7 @@ def test_ping_frame_to_bytes(): def test_ping_frame_from_bytes(): - f = Frame.from_bytes('000008060100000000666f6f6261720000'.decode('hex')) + f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -530,7 +539,7 @@ def test_ping_frame_from_bytes(): assert_equal(f.stream_id, 0x0) assert_equal(f.payload, b'foobar\0\0') - f = Frame.from_bytes('000008060000000000666f6f6261726465'.decode('hex')) + f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -581,8 +590,8 @@ def test_goaway_frame_to_bytes(): def test_goaway_frame_from_bytes(): - f = Frame.from_bytes( - '0000080700000000000123456787654321'.decode('hex')) + f = Frame.from_file(FileAdapter( + '0000080700000000000123456787654321')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -592,8 +601,8 @@ def test_goaway_frame_from_bytes(): assert_equal(f.error_code, 0x87654321) assert_equal(f.data, b'') - f = Frame.from_bytes( - '00000e0700000000000123456787654321666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000e0700000000000123456787654321666f6f626172')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 14) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -642,7 +651,7 @@ def test_window_update_frame_to_bytes(): def test_window_update_frame_from_bytes(): - f = Frame.from_bytes('00000408000000000001234567'.decode('hex')) + f = Frame.from_file(FileAdapter('00000408000000000001234567')) assert isinstance(f, WindowUpdateFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, WindowUpdateFrame.TYPE) @@ -677,7 +686,7 @@ def test_continuation_frame_to_bytes(): def test_continuation_frame_from_bytes(): - f = Frame.from_bytes('000006090401234567666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) assert isinstance(f, ContinuationFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, ContinuationFrame.TYPE) From 623dd850e0ce15630e0950b4de843c0af8046618 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 14:28:09 +0200 Subject: [PATCH 07/14] http2: add logging and error handling --- netlib/h2/__init__.py | 28 ++++++++++++++++++---------- netlib/h2/frame.py | 16 ++++++++++++---- test/h2/test_frames.py | 14 ++++++++++++-- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/netlib/h2/__init__.py b/netlib/h2/__init__.py index 054ba91c4..c06f7a110 100644 --- a/netlib/h2/__init__.py +++ b/netlib/h2/__init__.py @@ -1,8 +1,11 @@ from __future__ import (absolute_import, print_function, division) import itertools +import logging -from .. import utils from .frame import * +from .. import utils + +log = logging.getLogger(__name__) class HTTP2Protocol(object): @@ -49,7 +52,7 @@ class HTTP2Protocol(object): if alp != self.ALPN_PROTO_H2: raise NotImplementedError( "H2Client can not handle unknown ALP: %s" % alp) - print("-> Successfully negotiated 'h2' application layer protocol.") + log.debug("ALP 'h2' successfully negotiated.") def send_connection_preface(self): self.wfile.write(bytes(self.CLIENT_CONNECTION_PREFACE.decode('hex'))) @@ -60,7 +63,7 @@ class HTTP2Protocol(object): self._apply_settings(frame.settings) self.read_frame() # read setting ACK frame - print("-> Connection Preface completed.") + log.debug("Connection Preface completed.") def next_stream_id(self): if self.current_stream_id is None: @@ -88,13 +91,13 @@ class HTTP2Protocol(object): old_value = '-' self.http2_settings[setting] = value - print("-> Setting changed: %s to %d (was %s)" % ( + log.debug("Setting changed: %s to %d (was %s)" % ( SettingsFrame.SETTINGS.get_name(setting), value, str(old_value))) self.send_frame(SettingsFrame(state=self, flags=Frame.FLAG_ACK)) - print("-> New settings acknowledged.") + log.debug("New settings acknowledged.") def _create_headers(self, headers, stream_id, end_stream=True): # TODO: implement max frame size checks and sending in chunks @@ -103,11 +106,13 @@ class HTTP2Protocol(object): if end_stream: flags |= Frame.FLAG_END_STREAM + header_block_fragment = self.encoder.encode(headers) + bytes = HeadersFrame( state=self, flags=flags, stream_id=stream_id, - headers=headers).to_bytes() + header_block_fragment=header_block_fragment).to_bytes() return [bytes] def _create_body(self, body, stream_id): @@ -150,8 +155,8 @@ class HTTP2Protocol(object): if frame.flags | Frame.FLAG_END_HEADERS: break else: - print("Unexpected frame received:") - print(frame.human_readable()) + log.debug("Unexpected frame received:") + log.debug(frame.human_readable()) while True: frame = self.read_frame() @@ -160,11 +165,14 @@ class HTTP2Protocol(object): if frame.flags | Frame.FLAG_END_STREAM: break else: - print("Unexpected frame received:") - print(frame.human_readable()) + log.debug("Unexpected frame received:") + log.debug(frame.human_readable()) headers = {} for header, value in self.decoder.decode(header_block_fragment): headers[header] = value + for header, value in headers.items(): + log.debug("%s: %s" % (header, value)) + return headers[':status'], headers, body diff --git a/netlib/h2/frame.py b/netlib/h2/frame.py index 0755c96c4..018e822f0 100644 --- a/netlib/h2/frame.py +++ b/netlib/h2/frame.py @@ -1,9 +1,14 @@ import struct +import logging +from functools import reduce from hpack.hpack import Encoder, Decoder from .. import utils -from functools import reduce +log = logging.getLogger(__name__) + +class FrameSizeError(Exception): + pass class Frame(object): @@ -57,10 +62,11 @@ class Frame(object): else: settings = HTTP2Protocol.HTTP2_DEFAULT_SETTINGS - max_frame_size = settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] + max_frame_size = settings[ + SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] if length > max_frame_size: - raise NotImplementedError( + raise FrameSizeError( "Frame size exceeded: %d, but only %d allowed." % ( length, max_frame_size)) @@ -248,7 +254,9 @@ class HeadersFrame(Frame): if self.flags & self.FLAG_PADDED: s.append("padding: %d" % self.pad_length) - s.append("header_block_fragment: %s" % self.header_block_fragment.encode('hex')) + s.append( + "header_block_fragment: %s" % + self.header_block_fragment.encode('hex')) return "\n".join(s) diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 30dc71e85..42a0c1cfe 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -1,7 +1,7 @@ -from netlib.h2.frame import * import tutils - from nose.tools import assert_equal +from netlib.h2.frame import * + class FileAdapter(object): def __init__(self, data, is_hex=True): @@ -42,6 +42,16 @@ def test_frame_equality(): payload='foobar') assert_equal(a, b) + +def test_too_large_frames(): + f = DataFrame( + length=9000, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar' * 3000) + tutils.raises(FrameSizeError, f.to_bytes) + + def test_data_frame_to_bytes(): f = DataFrame( length=6, From f003f87197a6dffe1b51a82f7dd218121c75e206 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 19:44:48 +0200 Subject: [PATCH 08/14] http2: rename module and refactor as strategy --- netlib/{h2 => http2}/__init__.py | 17 ++++++++++------- netlib/{h2 => http2}/frame.py | 2 ++ test/h2/test_frames.py | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) rename netlib/{h2 => http2}/__init__.py (92%) rename netlib/{h2 => http2}/frame.py (99%) diff --git a/netlib/h2/__init__.py b/netlib/http2/__init__.py similarity index 92% rename from netlib/h2/__init__.py rename to netlib/http2/__init__.py index c06f7a110..d6f2c51c0 100644 --- a/netlib/h2/__init__.py +++ b/netlib/http2/__init__.py @@ -41,24 +41,27 @@ class HTTP2Protocol(object): SettingsFrame.SETTINGS.SETTINGS_MAX_HEADER_LIST_SIZE: None, } - def __init__(self): + def __init__(self, tcp_client): + self.tcp_client = tcp_client + self.http2_settings = self.HTTP2_DEFAULT_SETTINGS.copy() self.current_stream_id = None self.encoder = Encoder() self.decoder = Decoder() def check_alpn(self): - alp = self.get_alpn_proto_negotiated() + alp = self.tcp_client.get_alpn_proto_negotiated() if alp != self.ALPN_PROTO_H2: raise NotImplementedError( "H2Client can not handle unknown ALP: %s" % alp) log.debug("ALP 'h2' successfully negotiated.") def send_connection_preface(self): - self.wfile.write(bytes(self.CLIENT_CONNECTION_PREFACE.decode('hex'))) + self.tcp_client.wfile.write( + bytes(self.CLIENT_CONNECTION_PREFACE.decode('hex'))) self.send_frame(SettingsFrame(state=self)) - frame = Frame.from_file(self.rfile, self) + frame = Frame.from_file(self.tcp_client.rfile, self) assert isinstance(frame, SettingsFrame) self._apply_settings(frame.settings) self.read_frame() # read setting ACK frame @@ -74,11 +77,11 @@ class HTTP2Protocol(object): def send_frame(self, frame): raw_bytes = frame.to_bytes() - self.wfile.write(raw_bytes) - self.wfile.flush() + self.tcp_client.wfile.write(raw_bytes) + self.tcp_client.wfile.flush() def read_frame(self): - frame = Frame.from_file(self.rfile, self) + frame = Frame.from_file(self.tcp_client.rfile, self) if isinstance(frame, SettingsFrame): self._apply_settings(frame.settings) diff --git a/netlib/h2/frame.py b/netlib/http2/frame.py similarity index 99% rename from netlib/h2/frame.py rename to netlib/http2/frame.py index 018e822f0..1497380a7 100644 --- a/netlib/h2/frame.py +++ b/netlib/http2/frame.py @@ -7,9 +7,11 @@ from .. import utils log = logging.getLogger(__name__) + class FrameSizeError(Exception): pass + class Frame(object): """ diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 42a0c1cfe..d8a4febc7 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -1,6 +1,6 @@ import tutils from nose.tools import assert_equal -from netlib.h2.frame import * +from netlib.http2.frame import * class FileAdapter(object): From fdc908cb9811628435ef02e3168c4d5931c6a3c5 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 13:28:09 +0200 Subject: [PATCH 09/14] http2: add protocol tests --- netlib/http2/__init__.py | 25 ++-- netlib/test.py | 2 +- test/{h2 => }/__init__.py | 0 test/{h2 => http2}/test_frames.py | 2 +- test/http2/test_http2_protocol.py | 216 ++++++++++++++++++++++++++++++ 5 files changed, 231 insertions(+), 14 deletions(-) rename test/{h2 => }/__init__.py (100%) rename test/{h2 => http2}/test_frames.py (99%) create mode 100644 test/http2/test_http2_protocol.py diff --git a/netlib/http2/__init__.py b/netlib/http2/__init__.py index d6f2c51c0..2803cccb0 100644 --- a/netlib/http2/__init__.py +++ b/netlib/http2/__init__.py @@ -30,7 +30,7 @@ class HTTP2Protocol(object): # "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" CLIENT_CONNECTION_PREFACE = '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a' - ALPN_PROTO_H2 = b'h2' + ALPN_PROTO_H2 = 'h2' HTTP2_DEFAULT_SETTINGS = { SettingsFrame.SETTINGS.SETTINGS_HEADER_TABLE_SIZE: 4096, @@ -53,18 +53,25 @@ class HTTP2Protocol(object): alp = self.tcp_client.get_alpn_proto_negotiated() if alp != self.ALPN_PROTO_H2: raise NotImplementedError( - "H2Client can not handle unknown ALP: %s" % alp) + "HTTP2Protocol can not handle unknown ALP: %s" % alp) log.debug("ALP 'h2' successfully negotiated.") + return True - def send_connection_preface(self): + def perform_connection_preface(self): self.tcp_client.wfile.write( bytes(self.CLIENT_CONNECTION_PREFACE.decode('hex'))) self.send_frame(SettingsFrame(state=self)) + # read server settings frame frame = Frame.from_file(self.tcp_client.rfile, self) assert isinstance(frame, SettingsFrame) self._apply_settings(frame.settings) - self.read_frame() # read setting ACK frame + + # read setting ACK frame + settings_ack_frame = self.read_frame() + assert isinstance(settings_ack_frame, SettingsFrame) + assert settings_ack_frame.flags & Frame.FLAG_ACK + assert len(settings_ack_frame.settings) == 0 log.debug("Connection Preface completed.") @@ -94,9 +101,9 @@ class HTTP2Protocol(object): old_value = '-' self.http2_settings[setting] = value - log.debug("Setting changed: %s to %d (was %s)" % ( + log.debug("Setting changed: %s to %s (was %s)" % ( SettingsFrame.SETTINGS.get_name(setting), - value, + str(value), str(old_value))) self.send_frame(SettingsFrame(state=self, flags=Frame.FLAG_ACK)) @@ -157,9 +164,6 @@ class HTTP2Protocol(object): header_block_fragment += frame.header_block_fragment if frame.flags | Frame.FLAG_END_HEADERS: break - else: - log.debug("Unexpected frame received:") - log.debug(frame.human_readable()) while True: frame = self.read_frame() @@ -167,9 +171,6 @@ class HTTP2Protocol(object): body += frame.payload if frame.flags | Frame.FLAG_END_STREAM: break - else: - log.debug("Unexpected frame received:") - log.debug(frame.human_readable()) headers = {} for header, value in self.decoder.decode(header_block_fragment): diff --git a/netlib/test.py b/netlib/test.py index ee8c66852..4b0b6bd29 100644 --- a/netlib/test.py +++ b/netlib/test.py @@ -4,7 +4,7 @@ import Queue import cStringIO import OpenSSL from . import tcp, certutils -import tutils +from test import tutils class ServerThread(threading.Thread): diff --git a/test/h2/__init__.py b/test/__init__.py similarity index 100% rename from test/h2/__init__.py rename to test/__init__.py diff --git a/test/h2/test_frames.py b/test/http2/test_frames.py similarity index 99% rename from test/h2/test_frames.py rename to test/http2/test_frames.py index d8a4febc7..d8f00dec9 100644 --- a/test/h2/test_frames.py +++ b/test/http2/test_frames.py @@ -1,4 +1,4 @@ -import tutils +from test import tutils from nose.tools import assert_equal from netlib.http2.frame import * diff --git a/test/http2/test_http2_protocol.py b/test/http2/test_http2_protocol.py new file mode 100644 index 000000000..6a2754304 --- /dev/null +++ b/test/http2/test_http2_protocol.py @@ -0,0 +1,216 @@ + +import OpenSSL + +from netlib import http2 +from netlib import tcp +from netlib import test +from netlib.http2.frame import * +from test import tutils + + +class EchoHandler(tcp.BaseHandler): + sni = None + + def handle(self): + v = self.rfile.readline() + self.wfile.write(v) + self.wfile.flush() + + +class TestCheckALPNMatch(test.ServerTestBase): + handler = EchoHandler + ssl = dict( + alpn_select=http2.HTTP2Protocol.ALPN_PROTO_H2, + ) + + if OpenSSL._util.lib.Cryptography_HAS_ALPN: + + def test_check_alpn(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2]) + protocol = http2.HTTP2Protocol(c) + assert protocol.check_alpn() + + +class TestCheckALPNMismatch(test.ServerTestBase): + handler = EchoHandler + ssl = dict( + alpn_select=None, + ) + + if OpenSSL._util.lib.Cryptography_HAS_ALPN: + + def test_check_alpn(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2]) + protocol = http2.HTTP2Protocol(c) + tutils.raises(NotImplementedError, protocol.check_alpn) + + +class TestPerformConnectionPreface(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + # check magic + assert self.rfile.read(24) ==\ + '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex') + + # check empty settings frame + assert self.rfile.read(9) ==\ + '000000040000000000'.decode('hex') + + # send empty settings frame + self.wfile.write('000000040000000000'.decode('hex')) + self.wfile.flush() + + # check settings acknowledgement + assert self.rfile.read(9) == \ + '000000040100000000'.decode('hex') + + # send settings acknowledgement + self.wfile.write('000000040100000000'.decode('hex')) + self.wfile.flush() + + ssl = True + + def test_perform_connection_preface(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + protocol.perform_connection_preface() + + +class TestStreamIds(): + c = tcp.TCPClient(("127.0.0.1", 0)) + protocol = http2.HTTP2Protocol(c) + + def test_stream_ids(self): + assert self.protocol.current_stream_id is None + assert self.protocol.next_stream_id() == 1 + assert self.protocol.current_stream_id == 1 + assert self.protocol.next_stream_id() == 3 + assert self.protocol.current_stream_id == 3 + assert self.protocol.next_stream_id() == 5 + assert self.protocol.current_stream_id == 5 + + +class TestApplySettings(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + # check settings acknowledgement + assert self.rfile.read(9) == '000000040100000000'.decode('hex') + self.wfile.write("OK") + self.wfile.flush() + + ssl = True + + def test_apply_settings(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + + protocol._apply_settings({ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 'foo', + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 'bar', + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 'deadbeef', + }) + + assert c.rfile.safe_read(2) == "OK" + + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 'foo' + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS] == 'bar' + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE] == 'deadbeef' + + +class TestCreateHeaders(): + c = tcp.TCPClient(("127.0.0.1", 0)) + + def test_create_headers(self): + headers = [ + (b':method', b'GET'), + (b':path', b'index.html'), + (b':scheme', b'https'), + (b'foo', b'bar')] + + bytes = http2.HTTP2Protocol(self.c)._create_headers( + headers, 1, end_stream=True) + assert b''.join(bytes) ==\ + '000014010500000001824488355217caf3a69a3f87408294e7838c767f'\ + .decode('hex') + + bytes = http2.HTTP2Protocol(self.c)._create_headers( + headers, 1, end_stream=False) + assert b''.join(bytes) ==\ + '000014010400000001824488355217caf3a69a3f87408294e7838c767f'\ + .decode('hex') + + # TODO: add test for too large header_block_fragments + + +class TestCreateBody(): + c = tcp.TCPClient(("127.0.0.1", 0)) + protocol = http2.HTTP2Protocol(c) + + def test_create_body_empty(self): + bytes = self.protocol._create_body(b'', 1) + assert b''.join(bytes) == ''.decode('hex') + + def test_create_body_single_frame(self): + bytes = self.protocol._create_body('foobar', 1) + assert b''.join(bytes) == '000006000100000001666f6f626172'.decode('hex') + + def test_create_body_multiple_frames(self): + pass + # bytes = self.protocol._create_body('foobar' * 3000, 1) + # TODO: add test for too large frames + + +class TestCreateRequest(): + c = tcp.TCPClient(("127.0.0.1", 0)) + + def test_create_request_simple(self): + bytes = http2.HTTP2Protocol(self.c).create_request('GET', '/') + assert len(bytes) == 1 + assert bytes[0] == '000003010500000001828487'.decode('hex') + + def test_create_request_with_body(self): + bytes = http2.HTTP2Protocol(self.c).create_request( + 'GET', '/', [(b'foo', b'bar')], 'foobar') + assert len(bytes) == 2 + assert bytes[0] ==\ + '00000b010400000001828487408294e7838c767f'.decode('hex') + assert bytes[1] ==\ + '000006000100000001666f6f626172'.decode('hex') + + +class TestReadResponse(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + self.wfile.write( + b'00000801040000000188628594e78c767f'.decode('hex')) + self.wfile.write( + b'000006000100000001666f6f626172'.decode('hex')) + self.wfile.flush() + + ssl = True + + def test_read_response(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + + status, headers, body = protocol.read_response() + + assert headers == {':status': '200', 'etag': 'foobar'} + assert status == '200' + assert body == b'foobar' From 49043131cc49a602f54c1671ef5637b606c401b7 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 19:39:15 +0200 Subject: [PATCH 10/14] increase test coverage --- test/test_tcp.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_tcp.py b/test/test_tcp.py index cbe92f3c8..f8fc6a282 100644 --- a/test/test_tcp.py +++ b/test/test_tcp.py @@ -575,6 +575,11 @@ class TestFileLike: s = tcp.Reader(o) tutils.raises(tcp.NetLibDisconnect, s.readline, 10) + def test_reader_incomplete_error(self): + s = cStringIO.StringIO("foobar") + s = tcp.Reader(s) + tutils.raises(tcp.NetLibIncomplete, s.safe_read, 10) + class TestAddress: From e7c84a1ce14ca339184de1cd615727144d50d381 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:05:05 +0200 Subject: [PATCH 11/14] make travis run all tests --- test/http2/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/http2/__init__.py diff --git a/test/http2/__init__.py b/test/http2/__init__.py new file mode 100644 index 000000000..e69de29bb From 6c1c6f5f0ad375d5a8f37007e6cf2d6862282de9 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:49:03 +0200 Subject: [PATCH 12/14] http2: fix EchoHandler test helper --- test/http2/test_http2_protocol.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/http2/test_http2_protocol.py b/test/http2/test_http2_protocol.py index 6a2754304..cb46bc68a 100644 --- a/test/http2/test_http2_protocol.py +++ b/test/http2/test_http2_protocol.py @@ -12,9 +12,10 @@ class EchoHandler(tcp.BaseHandler): sni = None def handle(self): - v = self.rfile.readline() - self.wfile.write(v) - self.wfile.flush() + while True: + v = self.rfile.safe_read(1) + self.wfile.write(v) + self.wfile.flush() class TestCheckALPNMatch(test.ServerTestBase): From f2db8abbe859266bb28117e1ffa4b0b99d62e321 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:52:11 +0200 Subject: [PATCH 13/14] use open instead of file --- netlib/test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netlib/test.py b/netlib/test.py index 4b0b6bd29..1e1b5e9d7 100644 --- a/netlib/test.py +++ b/netlib/test.py @@ -75,13 +75,13 @@ class TServer(tcp.TCPServer): raw_cert = self.ssl.get( "cert", tutils.test_data.path("data/server.crt")) - cert = certutils.SSLCert.from_pem(file(raw_cert, "rb").read()) + cert = certutils.SSLCert.from_pem(open(raw_cert, "rb").read()) raw_key = self.ssl.get( "key", tutils.test_data.path("data/server.key")) key = OpenSSL.crypto.load_privatekey( OpenSSL.crypto.FILETYPE_PEM, - file(raw_key, "rb").read()) + open(raw_key, "rb").read()) if self.ssl.get("v3_only", False): method = tcp.SSLv3_METHOD options = OpenSSL.SSL.OP_NO_SSLv2 | OpenSSL.SSL.OP_NO_TLSv1 From e39d8aed6d77b6cf5d57c795c69e735a7c1430fa Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:55:32 +0200 Subject: [PATCH 14/14] http2: refactor hex to file adapter --- test/http2/test_frames.py | 64 +++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/test/http2/test_frames.py b/test/http2/test_frames.py index d8f00dec9..76a4b712e 100644 --- a/test/http2/test_frames.py +++ b/test/http2/test_frames.py @@ -1,23 +1,13 @@ +import cStringIO from test import tutils from nose.tools import assert_equal +from netlib import tcp from netlib.http2.frame import * -class FileAdapter(object): - def __init__(self, data, is_hex=True): - self.position = 0 - if is_hex: - self.data = data.decode('hex') - else: - self.data = data - - def safe_read(self, length): - if self.position + length > len(self.data): - raise ValueError("not enough bytes to read") - - value = self.data[self.position:self.position + length] - self.position += length - return value +def hex_to_file(data): + data = data.decode('hex') + return tcp.Reader(cStringIO.StringIO(data)) def test_invalid_flags(): @@ -79,7 +69,7 @@ def test_data_frame_to_bytes(): def test_data_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) + f = Frame.from_file(hex_to_file('000006000101234567666f6f626172')) assert isinstance(f, DataFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, DataFrame.TYPE) @@ -87,7 +77,7 @@ def test_data_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.payload, 'foobar') - f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) + f = Frame.from_file(hex_to_file('00000a00090123456703666f6f626172000000')) assert isinstance(f, DataFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, DataFrame.TYPE) @@ -171,7 +161,7 @@ def test_headers_frame_to_bytes(): def test_headers_frame_from_bytes(): - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '000007010001234567668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 7) @@ -180,7 +170,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000b01080123456703668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 11) @@ -189,7 +179,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000c012001234567876543212a668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 12) @@ -201,7 +191,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00001001280123456703876543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) @@ -213,7 +203,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00001001280123456703076543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) @@ -285,7 +275,7 @@ def test_priority_frame_to_bytes(): def test_priority_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000005020001234567876543212a')) + f = Frame.from_file(hex_to_file('000005020001234567876543212a')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -295,7 +285,7 @@ def test_priority_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter('0000050200012345670765432115')) + f = Frame.from_file(hex_to_file('0000050200012345670765432115')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -333,7 +323,7 @@ def test_rst_stream_frame_to_bytes(): def test_rst_stream_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000403000123456707654321')) + f = Frame.from_file(hex_to_file('00000403000123456707654321')) assert isinstance(f, RstStreamFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, RstStreamFrame.TYPE) @@ -391,21 +381,21 @@ def test_settings_frame_to_bytes(): def test_settings_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000000040000000000')) + f = Frame.from_file(hex_to_file('000000040000000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x0) - f = Frame.from_file(FileAdapter('000000040100000000')) + f = Frame.from_file(hex_to_file('000000040100000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, SettingsFrame.FLAG_ACK) assert_equal(f.stream_id, 0x0) - f = Frame.from_file(FileAdapter('000006040100000000000200000001')) + f = Frame.from_file(hex_to_file('000006040100000000000200000001')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -414,7 +404,7 @@ def test_settings_frame_from_bytes(): assert_equal(len(f.settings), 1) assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000c040000000000000200000001000312345678')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 12) @@ -485,7 +475,7 @@ def test_push_promise_frame_to_bytes(): def test_push_promise_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) + f = Frame.from_file(hex_to_file('00000a05000123456707654321666f6f626172')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -493,7 +483,7 @@ def test_push_promise_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, 'foobar') - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000e0508012345670307654321666f6f626172000000')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 14) @@ -541,7 +531,7 @@ def test_ping_frame_to_bytes(): def test_ping_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) + f = Frame.from_file(hex_to_file('000008060100000000666f6f6261720000')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -549,7 +539,7 @@ def test_ping_frame_from_bytes(): assert_equal(f.stream_id, 0x0) assert_equal(f.payload, b'foobar\0\0') - f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) + f = Frame.from_file(hex_to_file('000008060000000000666f6f6261726465')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -600,7 +590,7 @@ def test_goaway_frame_to_bytes(): def test_goaway_frame_from_bytes(): - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '0000080700000000000123456787654321')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) @@ -611,7 +601,7 @@ def test_goaway_frame_from_bytes(): assert_equal(f.error_code, 0x87654321) assert_equal(f.data, b'') - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000e0700000000000123456787654321666f6f626172')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 14) @@ -661,7 +651,7 @@ def test_window_update_frame_to_bytes(): def test_window_update_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000408000000000001234567')) + f = Frame.from_file(hex_to_file('00000408000000000001234567')) assert isinstance(f, WindowUpdateFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, WindowUpdateFrame.TYPE) @@ -696,7 +686,7 @@ def test_continuation_frame_to_bytes(): def test_continuation_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) + f = Frame.from_file(hex_to_file('000006090401234567666f6f626172')) assert isinstance(f, ContinuationFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, ContinuationFrame.TYPE)