How to use the h11.NEED_DATA function in h11

To help you get started, we’ve selected a few h11 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 / hypercorn / tests / asyncio / test_wsproto.py View on Github external
def get_events(self) -> list:
        events = []
        self.client.receive_data(self.transport.data)
        while True:
            event = self.client.next_event()
            if event in (h11.NEED_DATA, h11.PAUSED):
                break
            events.append(event)
            if isinstance(event, h11.ConnectionClosed):
                break
        return events
github psf / requests / requests3 / core / _http / _sync / connection.py View on Github external
def consume_bytes(data):
        state_machine.receive_data(data)
        while True:
            event = state_machine.next_event()
            if event is h11.NEED_DATA:
                break

            elif isinstance(event, h11.InformationalResponse):
                # Ignore 1xx responses
                continue

            elif isinstance(event, h11.Response):
                # We have our response! Save it and get out of here.
                context['h11_response'] = event
                raise LoopAbort

            else:
                # Can't happen
                raise RuntimeError("Unexpected h11 event {}".format(event))
github python-hyper / h11 / examples / curio-server.py View on Github external
async def next_event(self):
        while True:
            event = self.conn.next_event()
            if event is h11.NEED_DATA:
                await self._read_from_peer()
                continue
            return event
github encode / httpx / httpcore / http11.py View on Github external
async def _receive_event(self, timeout: TimeoutConfig) -> H11Event:
        assert self._reader is not None

        event = self._h11_state.next_event()

        while event is h11.NEED_DATA:
            try:
                data = await asyncio.wait_for(
                    self._reader.read(2048), timeout.read_timeout
                )
            except asyncio.TimeoutError:
                raise ReadTimeout()
            self._h11_state.receive_data(data)
            event = self._h11_state.next_event()

        return event
github python-hyper / h11 / docs / source / _examples / myclient.py View on Github external
def next_event(self, max_bytes_per_recv=200):
        while True:
            # If we already have a complete event buffered internally, just
            # return that. Otherwise, read some data, add it to the internal
            # buffer, and then try again.
            event = self.conn.next_event()
            if event is h11.NEED_DATA:
                self.conn.receive_data(self.sock.recv(max_bytes_per_recv))
                continue
            return event
github sorcio / trio-asgi / trio_web / h11server.py View on Github external
async def next_event(self):
        while True:
            event = self.conn.next_event()
            if event is h11.NEED_DATA:
                await self._read_from_peer()
                continue
            return event
github python-hyper / wsproto / src / wsproto / handshake.py View on Github external
A list of events that the remote peer triggered by sending
        this data can be retrieved with :meth:`events`.

        :param bytes data: Data received from the WebSocket peer.
        """
        self._h11_connection.receive_data(data)
        while True:
            try:
                event = self._h11_connection.next_event()
            except h11.RemoteProtocolError:
                raise RemoteProtocolError(
                    "Bad HTTP message", event_hint=RejectConnection()
                )
            if (
                isinstance(event, h11.ConnectionClosed)
                or event is h11.NEED_DATA
                or event is h11.PAUSED
            ):
                break

            if self.client:
                if isinstance(event, h11.InformationalResponse):
                    if event.status_code == 101:
                        self._events.append(self._establish_client_connection(event))
                    else:
                        self._events.append(
                            RejectConnection(
                                headers=event.headers,
                                status_code=event.status_code,
                                has_body=False,
                            )
                        )
github psf / requests / requests3 / core / _http / _async / connection.py View on Github external
def consume_bytes(data):
        state_machine.receive_data(data)
        while True:
            event = state_machine.next_event()
            if event is h11.NEED_DATA:
                break

            elif isinstance(event, h11.InformationalResponse):
                # Ignore 1xx responses
                continue

            elif isinstance(event, h11.Response):
                # We have our response! Save it and get out of here.
                context['h11_response'] = event
                raise LoopAbort

            else:
                # Can't happen
                raise RuntimeError("Unexpected h11 event {}".format(event))
github encode / requests-async / example.py View on Github external
writer.write(data)

        message = h11.EndOfMessage()
        data = conn.send(message)
        writer.write(data)

        status_code = 0
        headers = []
        reason = b''
        buffer = io.BytesIO()

        while True:
            event = conn.next_event()
            event_type = type(event)

            if event_type is h11.NEED_DATA:
                data = await reader.read(2048)
                conn.receive_data(data)

            elif event_type is h11.Response:
                status_code = event.status_code
                headers = [(key.decode(), value.decode()) for key, value in event.headers]
                reason = event.reason

            elif event_type is h11.Data:
                buffer.write(event.data)

            elif event_type is h11.EndOfMessage:
                buffer.seek(0)
                break

        writer.close()