Ejemplo n.º 1
0
 def __init__(self, ctx, mode):
     super(Http2Layer, self).__init__(ctx)
     self.mode = mode
     self.client_protocol = HTTP2Protocol(self.client_conn, is_server=True,
                                          unhandled_frame_cb=self.handle_unexpected_frame_from_client)
     self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False,
                                          unhandled_frame_cb=self.handle_unexpected_frame_from_server)
Ejemplo n.º 2
0
 def set_server(self, *args, **kwargs):
     self.ctx.set_server(*args, **kwargs)
     self.server_protocol = HTTP2Protocol(
         self.server_conn,
         is_server=False,
         unhandled_frame_cb=self.handle_unexpected_frame_from_server)
     self.server_protocol.perform_connection_preface()
Ejemplo n.º 3
0
 def connect(self):
     self.ctx.connect()
     self.server_protocol = HTTP2Protocol(
         self.server_conn,
         is_server=False,
         unhandled_frame_cb=self.handle_unexpected_frame_from_server)
     self.server_protocol.perform_connection_preface()
Ejemplo n.º 4
0
 def test_create_body_multiple_frames(self):
     protocol = HTTP2Protocol(self.c)
     protocol.http2_settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] = 5
     bytes = protocol._create_body('foobarmehm42', 1)
     assert len(bytes) == 3
     assert bytes[0] == '000005000000000001666f6f6261'.decode('hex')
     assert bytes[1] == '000005000000000001726d65686d'.decode('hex')
     assert bytes[2] == '0000020001000000013432'.decode('hex')
Ejemplo n.º 5
0
 def test_simple(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response(
         (2, 0),
         200,
     ))
     assert len(bytes) == 1
     assert bytes[0] ==\
         '00000101050000000288'.decode('hex')
Ejemplo n.º 6
0
    def test_perform_client_connection_preface(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        protocol = HTTP2Protocol(c)

        assert not protocol.connection_preface_performed
        protocol.perform_client_connection_preface()
        assert protocol.connection_preface_performed
Ejemplo n.º 7
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 = HTTP2Protocol(self.c)._create_headers(
            headers, 1, end_stream=True)
        assert b''.join(bytes) ==\
            '000014010500000001824488355217caf3a69a3f87408294e7838c767f'\
            .decode('hex')

        bytes = HTTP2Protocol(self.c)._create_headers(
            headers, 1, end_stream=False)
        assert b''.join(bytes) ==\
            '000014010400000001824488355217caf3a69a3f87408294e7838c767f'\
            .decode('hex')
Ejemplo n.º 8
0
 def test_with_stream_id(self):
     resp = http.Response(
         (2, 0),
         200,
     )
     resp.stream_id = 0x42
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(resp)
     assert len(bytes) == 1
     assert bytes[0] ==\
         '00000101050000004288'.decode('hex')
Ejemplo n.º 9
0
    def test_perform_server_connection_preface(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        protocol = HTTP2Protocol(c)

        assert not protocol.connection_preface_performed
        protocol.perform_server_connection_preface()
        assert protocol.connection_preface_performed

        tutils.raises(tcp.NetLibDisconnect, protocol.perform_server_connection_preface, force=True)
Ejemplo n.º 10
0
    def test_perform_connection_preface_server(self, mock_client_method, mock_server_method):
        protocol = HTTP2Protocol(is_server=True)
        protocol.connection_preface_performed = True

        protocol.perform_connection_preface()
        assert not mock_client_method.called
        assert not mock_server_method.called

        protocol.perform_connection_preface(force=True)
        assert not mock_client_method.called
        assert mock_server_method.called
Ejemplo n.º 11
0
class TestServerStreamIds():
    c = tcp.TCPClient(("127.0.0.1", 0))
    protocol = HTTP2Protocol(c, is_server=True)

    def test_server_stream_ids(self):
        assert self.protocol.current_stream_id is None
        assert self.protocol._next_stream_id() == 2
        assert self.protocol.current_stream_id == 2
        assert self.protocol._next_stream_id() == 4
        assert self.protocol.current_stream_id == 4
        assert self.protocol._next_stream_id() == 6
        assert self.protocol.current_stream_id == 6
Ejemplo n.º 12
0
class TestClientStreamIds():
    c = tcp.TCPClient(("127.0.0.1", 0))
    protocol = HTTP2Protocol(c)

    def test_client_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
Ejemplo n.º 13
0
    def test_asterisk_form_in(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = HTTP2Protocol(c, is_server=True)
        protocol.connection_preface_performed = True

        req = protocol.read_request()

        assert req.form_in == "relative"
        assert req.method == "OPTIONS"
        assert req.path == "*"
Ejemplo n.º 14
0
    def test_read_request(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = HTTP2Protocol(c, is_server=True)
        protocol.connection_preface_performed = True

        req = protocol.read_request()

        assert req.stream_id
        assert req.headers.lst == [[u':method', u'GET'], [u':path', u'/'], [u':scheme', u'https']]
        assert req.body == b'foobar'
Ejemplo n.º 15
0
 def test_with_body(self):
     bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response(
         (2, 0),
         200,
         '',
         odict.ODictCaseless([('foo', 'bar')]),
         'foobar'
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         '00000901040000000288408294e7838c767f'.decode('hex')
     assert bytes[1] ==\
         '000006000100000002666f6f626172'.decode('hex')
Ejemplo n.º 16
0
    def test_absolute_form_in(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = HTTP2Protocol(c, is_server=True)
        protocol.connection_preface_performed = True

        req = protocol.read_request()

        assert req.form_in == "absolute"
        assert req.scheme == "http"
        assert req.host == "address"
        assert req.port == 22
Ejemplo n.º 17
0
 def test_request_simple(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         None,
         None,
     ))
     assert len(bytes) == 1
     assert bytes[0] == '00000d0105000000018284874188089d5c0b8170dc07'.decode('hex')
Ejemplo n.º 18
0
    def test_create_headers_multiple_frames(self):
        headers = [
            (b':method', b'GET'),
            (b':path', b'/'),
            (b':scheme', b'https'),
            (b'foo', b'bar'),
            (b'server', b'version')]

        protocol = HTTP2Protocol(self.c)
        protocol.http2_settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] = 8
        bytes = protocol._create_headers(headers, 1, end_stream=True)
        assert len(bytes) == 3
        assert bytes[0] == '000008010000000001828487408294e783'.decode('hex')
        assert bytes[1] == '0000080900000000018c767f7685ee5b10'.decode('hex')
        assert bytes[2] == '00000209050000000163d5'.decode('hex')
Ejemplo n.º 19
0
    def test_read_empty_response(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = HTTP2Protocol(c)
        protocol.connection_preface_performed = True

        resp = protocol.read_response(stream_id=42)

        assert resp.stream_id == 42
        assert resp.httpversion == (2, 0)
        assert resp.status_code == 200
        assert resp.msg == ""
        assert resp.headers.lst == [[':status', '200'], ['etag', 'foobar']]
        assert resp.body == b''
Ejemplo n.º 20
0
 def test_request_with_stream_id(self):
     req = http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         None,
         None,
     )
     req.stream_id = 0x42
     bytes = HTTP2Protocol(self.c).assemble_request(req)
     assert len(bytes) == 1
     assert bytes[0] == '00000d0105000000428284874188089d5c0b8170dc07'.decode('hex')
Ejemplo n.º 21
0
 def test_request_with_body(self):
     bytes = HTTP2Protocol(self.c).assemble_request(http.Request(
         '',
         'GET',
         'https',
         '',
         '',
         '/',
         (2, 0),
         odict.ODictCaseless([('foo', 'bar')]),
         'foobar',
     ))
     assert len(bytes) == 2
     assert bytes[0] ==\
         '0000150104000000018284874188089d5c0b8170dc07408294e7838c767f'.decode('hex')
     assert bytes[1] ==\
         '000006000100000001666f6f626172'.decode('hex')
Ejemplo n.º 22
0
    def test_connect(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = HTTP2Protocol(c, is_server=True)
        protocol.connection_preface_performed = True

        req = protocol.read_request()
        assert req.form_in == "authority"
        assert req.method == "CONNECT"
        assert req.host == "address"
        assert req.port == 22

        req = protocol.read_request()
        assert req.form_in == "authority"
        assert req.method == "CONNECT"
        assert req.host == "example.com"
        assert req.port == 443
Ejemplo n.º 23
0
    def test_apply_settings(self):
        c = tcp.TCPClient(("127.0.0.1", self.port))
        c.connect()
        c.convert_to_ssl()
        protocol = 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'
Ejemplo n.º 24
0
 def test_check_alpn(self):
     c = tcp.TCPClient(("127.0.0.1", self.port))
     c.connect()
     c.convert_to_ssl(alpn_protos=[HTTP2Protocol.ALPN_PROTO_H2])
     protocol = HTTP2Protocol(c)
     tutils.raises(NotImplementedError, protocol.check_alpn)
Ejemplo n.º 25
0
 def test_check_alpn(self):
     c = tcp.TCPClient(("127.0.0.1", self.port))
     c.connect()
     c.convert_to_ssl(alpn_protos=[HTTP2Protocol.ALPN_PROTO_H2])
     protocol = HTTP2Protocol(c)
     assert protocol.check_alpn()
Ejemplo n.º 26
0
 def test_direct(self):
     p = HTTP2Protocol(rfile='foo', wfile='bar')
     assert isinstance(p.tcp_handler, http2.TCPHandler)
     assert p.tcp_handler.rfile == 'foo'
     assert p.tcp_handler.wfile == 'bar'
Ejemplo n.º 27
0
 def test_create_body_empty(self):
     protocol = HTTP2Protocol(self.c)
     bytes = protocol._create_body(b'', 1)
     assert b''.join(bytes) == ''.decode('hex')
Ejemplo n.º 28
0
 def test_wrapped(self):
     h = http2.TCPHandler(rfile='foo', wfile='bar')
     p = HTTP2Protocol(h)
     assert p.tcp_handler.rfile == 'foo'
     assert p.tcp_handler.wfile == 'bar'
Ejemplo n.º 29
0
 def test_create_body_single_frame(self):
     protocol = HTTP2Protocol(self.c)
     bytes = protocol._create_body('foobar', 1)
     assert b''.join(bytes) == '000006000100000001666f6f626172'.decode('hex')