How to use h2 - 10 common examples

To help you get started, we’ve selected a few h2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pgjones / quart / tests / serving / test_h2.py View on Github external
async def get_events(self) -> AsyncGenerator[h2.events.Event, None]:
        while True:
            await self.transport.updated.wait()
            events = self.connection.receive_data(self.transport.data)
            self.transport.clear()
            for event in events:
                if isinstance(event, h2.events.ConnectionTerminated):
                    self.transport.close()
                elif isinstance(event, h2.events.DataReceived):
                    self.connection.acknowledge_received_data(
                        event.flow_controlled_length, event.stream_id,
                    )
                    self.server.data_received(self.connection.data_to_send())
                yield event
            if self.transport.closed.is_set():
                break
github python-hyper / hyper-h2 / test / test_basic_logic.py View on Github external
def test_acknowledging_settings(self, frame_factory):
        """
        Acknowledging settings causes appropriate Settings frame to be emitted.
        """
        c = h2.connection.H2Connection(config=self.server_config)
        c.receive_data(frame_factory.preamble())

        received_frame = frame_factory.build_settings_frame(
            settings=helpers.SAMPLE_SETTINGS
        )
        expected_frame = frame_factory.build_settings_frame(
            settings={}, ack=True
        )
        expected_data = expected_frame.serialize()

        c.clear_outbound_data_buffer()
        events = c.receive_data(received_frame.serialize())

        assert len(events) == 1
        assert c.data_to_send() == expected_data
github python-hyper / hyper-h2 / test / test_complex_logic.py View on Github external
def test_continuation_cannot_interleave_unknown_frame(self, frame_factory):
        """
        We cannot interleave an unknown frame with a CONTINUATION sequence when
        that sequence began with a PUSH_PROMISE frame.
        """
        c = h2.connection.H2Connection()
        c.initiate_connection()
        c.send_headers(stream_id=1, headers=self.example_request_headers)

        frames = self._build_continuation_sequence(
            headers=self.example_request_headers,
            block_size=5,
            frame_factory=frame_factory,
        )
        assert len(frames) > 2  # This is mostly defensive.

        bogus_frame = frame_factory.build_data_frame(
            data=b'hello',
            stream_id=1,
        )
        bogus_frame.type = 88
        frames.insert(len(frames) - 2, bogus_frame)
github python-hyper / hyper-h2 / test / test_invalid_headers.py View on Github external
def test_headers_event(self, frame_factory, headers):
        """
        Test sending invalid headers raise a ProtocolError.
        """
        c = h2.connection.H2Connection()
        c.initiate_connection()

        # Clear the data, then try to send headers.
        c.clear_outbound_data_buffer()
        with pytest.raises(h2.exceptions.ProtocolError):
            c.send_headers(1, headers)
github python-hyper / hyper-h2 / test / test_basic_logic.py View on Github external
def test_closing_stream_sending_data(self, frame_factory):
        """
        We can close a stream with a data frame.
        """
        c = h2.connection.H2Connection()
        c.initiate_connection()
        c.send_headers(1, self.example_request_headers)

        f = frame_factory.build_data_frame(
            data=b'some data',
            flags=['END_STREAM'],
        )

        # Clear the data, then send some data.
        c.clear_outbound_data_buffer()
        events = c.send_data(1, b'some data', end_stream=True)
        assert not events
        assert c.data_to_send() == f.serialize()
github python-hyper / hyper-h2 / test / test_basic_logic.py View on Github external
"""
        # This is a moderately varied set of cookie headers: some combined,
        # some split.
        cookie_headers = [
            ('cookie',
                'username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC'),
            ('cookie', 'path=1'),
            ('cookie', 'test1=val1; test2=val2')
        ]
        expected = (
            'username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; '
            'path=1; test1=val1; test2=val2'
        )

        config = h2.config.H2Configuration(header_encoding='utf-8')
        c = h2.connection.H2Connection(config=config)
        c.initiate_connection()
        c.send_headers(1, self.example_request_headers, end_stream=True)

        f = frame_factory.build_push_promise_frame(
            stream_id=1,
            promised_stream_id=2,
            headers=self.example_request_headers + cookie_headers
        )
        events = c.receive_data(f.serialize())

        assert len(events) == 1
        e = events[0]

        cookie_fields = [(n, v) for n, v in e.headers if n == 'cookie']
        assert len(cookie_fields) == 1
github python-hyper / hyper-h2 / test / test_complex_logic.py View on Github external
def test_continuation_frame_basic(self, frame_factory):
        """
        Test that we correctly decode a header block split across continuation
        frames.
        """
        c = h2.connection.H2Connection(config=self.server_config)
        c.initiate_connection()
        c.receive_data(frame_factory.preamble())

        frames = self._build_continuation_sequence(
            headers=self.example_request_headers,
            block_size=5,
            frame_factory=frame_factory,
        )
        data = b''.join(f.serialize() for f in frames)
        events = c.receive_data(data)

        assert len(events) == 2
        first_event, second_event = events

        assert isinstance(first_event, h2.events.RequestReceived)
        assert first_event.headers == self.example_request_headers
github python-hyper / hyper-h2 / test / test_invalid_frame_sequences.py View on Github external
def test_get_stream_reset_event_on_auto_reset(self, frame_factory):
        """
        When hyper-h2 resets a stream automatically, a StreamReset event fires.
        """
        c = h2.connection.H2Connection(config=self.server_config)
        c.initiate_connection()
        c.receive_data(frame_factory.preamble())

        f = frame_factory.build_headers_frame(
            self.example_request_headers,
            flags=['END_STREAM']
        )
        c.receive_data(f.serialize())
        c.clear_outbound_data_buffer()

        bad_frame = frame_factory.build_data_frame(
            data=b'hello'
        )
        events = c.receive_data(bad_frame.serialize())

        expected_frame = frame_factory.build_rst_stream_frame(
github vmagamedov / grpclib / tests / test_protocol.py View on Github external
)
        server_conn.increment_flow_control_window(
            connection_window - initial_window_size
        )

    if stream_window is not None:
        server_conn.update_settings({
            SettingCodes.INITIAL_WINDOW_SIZE: stream_window
        })

    if max_frame_size is not None:
        server_conn.update_settings({
            SettingCodes.MAX_FRAME_SIZE: max_frame_size
        })

    client_conn = H2Connection(H2Configuration(client_side=True,
                                               header_encoding='ascii'))
    client_conn.initiate_connection()

    client_conn.receive_data(server_conn.data_to_send())
    server_conn.receive_data(client_conn.data_to_send())
    client_conn.receive_data(server_conn.data_to_send())

    return client_conn, server_conn
github vmagamedov / grpclib / tests / test_protocol.py View on Github external
def create_connections(*, connection_window=None, stream_window=None,
                       max_frame_size=None):
    server_conn = H2Connection(H2Configuration(client_side=False,
                                               header_encoding='ascii'))
    server_conn.initiate_connection()

    if connection_window is not None:
        initial_window_size = server_conn.local_settings.initial_window_size
        assert connection_window > initial_window_size, (
            '{} should be greater than {}'
            .format(connection_window, initial_window_size)
        )
        server_conn.increment_flow_control_window(
            connection_window - initial_window_size
        )

    if stream_window is not None:
        server_conn.update_settings({
            SettingCodes.INITIAL_WINDOW_SIZE: stream_window