How to use the h11.ProtocolError 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 fnproject / fdk-python / fdk / http / routine.py View on Github external
async def inner(*args, **kwargs):
        try:
            return await action(*args, **kwargs)
        except h11.ProtocolError as ex:
            log.log(str(ex))
            traceback.print_exc(file=sys.stderr)
            sys.exit("Fn <-> FDK connectivity issue: ".format(str(ex)))
github poljar / matrix-nio / nio / http.py View on Github external
def _get_response(self):
        # type: () -> Optional[HttpResponse]
        ret = self._connection.next_event()

        if not self._current_response:
            self._current_response = HttpResponse()

        while ret != h11.NEED_DATA:
            if ret == h11.PAUSED or isinstance(ret, h11.EndOfMessage):
                try:
                    self._connection.start_next_cycle()
                except h11.ProtocolError:
                    self._connection = h11.Connection(our_role=h11.CLIENT)
                response = self._current_response
                self._current_response = None
                response.mark_as_received()
                return response
            elif isinstance(ret, h11.InformationalResponse):
                pass
            elif isinstance(ret, h11.Response):
                self._current_response.add_response(ret)
            elif isinstance(ret, h11.Data):
                self._current_response.add_data(ret.data)

            ret = self._connection.next_event()

        return None
github mitmproxy / mitmproxy / mitmproxy / proxy2 / layers / http / http.py View on Github external
def read_body(self, event: events.Event, is_request: bool) -> typing.Iterator[HttpEvent]:
        while True:
            try:
                if isinstance(event, events.DataReceived):
                    h11_event = self.body_reader(self.buf)
                elif isinstance(event, events.ConnectionClosed):
                    h11_event = self.body_reader.read_eof()
                else:
                    raise ValueError(f"Unexpected event: {event}")
            except h11.ProtocolError:
                raise  # FIXME

            if h11_event is None:
                return
            elif isinstance(h11_event, h11.Data):
                h11_event.data: bytearray  # type checking
                if is_request:
                    yield RequestData(bytes(h11_event.data), self.stream_id)
                else:
                    yield ResponseData(bytes(h11_event.data), self.stream_id)
            elif isinstance(h11_event, h11.EndOfMessage):
                if is_request:
                    yield RequestEndOfMessage(self.stream_id)
                else:
                    yield ResponseEndOfMessage(self.stream_id)
                return
github python-hyper / h11 / examples / trio-server.py View on Github external
wrapper.info("Server main loop got event:", event)
                if type(event) is h11.Request:
                    await send_echo_response(wrapper, event)
        except Exception as exc:
            wrapper.info("Error during response handler:", exc)
            await maybe_send_error_response(wrapper, exc)

        if wrapper.conn.our_state is h11.MUST_CLOSE:
            wrapper.info("connection is not reusable, so shutting down")
            await wrapper.shutdown_and_clean_up()
            return
        else:
            try:
                wrapper.info("trying to re-use connection")
                wrapper.conn.start_next_cycle()
            except h11.ProtocolError:
                states = wrapper.conn.states
                wrapper.info("unexpected state", states, "-- bailing out")
                await maybe_send_error_response(
                    wrapper,
                    RuntimeError("unexpected state {}".format(states)))
                await wrapper.shutdown_and_clean_up()
                return
github JeffBelgum / curious / curious / web.py View on Github external
except Exception as exc:
                log.info(f"Error during response handler: {exc}")
                handler = self.router.match_error(exc)
                status, response = await handler(exc)
                content_type, response = response_to_bytes(handler, response)
                await respond(status, content_type, response)

            if transport.conn.our_state is h11.MUST_CLOSE:
                log.info("connection is not reusable, so shutting down")
                await transport.shutdown_and_clean_up()
                return
            else:
                try:
                    log.info("trying to re-use connection")
                    transport.conn.start_next_cycle()
                except h11.ProtocolError:
                    states = transport.conn.states
                    log.info(f"unexpected state {states} -- bailing out")
                    exc = RuntimeError("unexpected state {}".format(states))
                    handler = self.router.match_error(exc)
                    status, response = await handler(exc)
                    content_type, response = response_to_bytes(handler, response)
                    await respond(status, content_type, response)
                    await transport.shutdown_and_clean_up()
                    return
github python-trio / hip / src / ahip / response.py View on Github external
except SocketTimeout:
                # FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
                # there is yet no clean way to get at it from this context.
                raise ReadTimeoutError(self._pool, None, "Read timed out.")

            except BaseSSLError as e:
                # FIXME: Is there a better way to differentiate between SSLErrors?
                if "read operation timed out" not in str(e):  # Defensive:
                    # This shouldn't happen but just in case we're missing an edge
                    # case, let's avoid swallowing SSL errors.
                    raise

                raise ReadTimeoutError(self._pool, None, "Read timed out.")

            except (h11.ProtocolError, SocketError) as e:
                # This includes IncompleteRead.
                raise ProtocolError("Connection broken: %r" % e, e)

            except GeneratorExit:
                # We swallow GeneratorExit when it is emitted: this allows the
                # use of the error checker inside stream()
                pass

            # If no exception is thrown, we should avoid cleaning up
            # unnecessarily.
            clean_exit = True
        finally:
            # If we didn't terminate cleanly, we need to throw away our
            # connection.
            if not clean_exit:
                self.close()
github psf / requests / requests3 / core / _http / _sync / response.py View on Github external
except SocketTimeout:
                # FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
                # there is yet no clean way to get at it from this context.
                raise ReadTimeoutError(self._pool, None, 'Read timed out.')

            except BaseSSLError as e:
                # FIXME: Is there a better way to differentiate between SSLErrors?
                if 'read operation timed out' not in str(e):  # Defensive:
                    # This shouldn't happen but just in case we're missing an edge
                    # case, let's avoid swallowing SSL errors.
                    raise

                raise ReadTimeoutError(self._pool, None, 'Read timed out.')

            except (h11.ProtocolError, SocketError) as e:
                # This includes IncompleteRead.
                raise ProtocolError('Connection broken: %r' % e, e)

            except GeneratorExit:
                # We swallow GeneratorExit when it is emitted: this allows the
                # use of the error checker inside stream()
                pass
            # If no exception is thrown, we should avoid cleaning up
            # unnecessarily.
            clean_exit = True
        finally:
            # If we didn't terminate cleanly, we need to throw away our
            # connection.
            if not clean_exit:
                self.close()
            # If we hold the original response but it's finished now, we should
github sorcio / trio-asgi / trio_web / h11server.py View on Github external
except Exception as exc:
            wrapper.info("Error during response handler:", exc)
            await maybe_send_error_response(wrapper, exc)

        if wrapper.conn.our_state is h11.MUST_CLOSE:
            wrapper.info("connection is not reusable, so shutting down")
            await wrapper.shutdown_and_clean_up()
            return
        else:
            try:
                wrapper.info("trying to re-use connection")
                wrapper.conn.start_next_cycle()
            except h11.LocalProtocolError:
                wrapper.info("not in a reusable state")
                return
            except h11.ProtocolError:
                states = wrapper.conn.states
                wrapper.info("unexpected state", states, "-- bailing out")
                await maybe_send_error_response(
                    wrapper,
                    RuntimeError("unexpected state {}".format(states)))
                await wrapper.shutdown_and_clean_up()
                return
github python-trio / hip / src / ahip / connectionpool.py View on Github external
SocketError,
            ProtocolError,
            h11.ProtocolError,
            BaseSSLError,
            SSLError,
            CertificateError,
        ) as e:
            # Discard the connection for these exceptions. It will be
            # replaced during the next _get_conn() call.
            clean_exit = False

            if isinstance(e, (BaseSSLError, CertificateError)):
                e = SSLError(e)
            elif isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
                e = ProxyError("Cannot connect to proxy.", e)
            elif isinstance(e, (SocketError, h11.ProtocolError)):
                e = ProtocolError("Connection aborted.", e)

            retries = retries.increment(
                method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
            )
            retries.sleep()

            # Keep track of the error for the retry warning.
            err = e

        finally:
            if not clean_exit:
                # We hit some kind of exception, handled or otherwise. We need
                # to throw the connection away unless explicitly told not to.
                # Close the connection, set the variable to None, and make sure
                # we put the None back in the pool to avoid leaking it.
github psf / requests / requests3 / core / _http / _async / response.py View on Github external
except SocketTimeout:
                # FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
                # there is yet no clean way to get at it from this context.
                raise ReadTimeoutError(self._pool, None, 'Read timed out.')

            except BaseSSLError as e:
                # FIXME: Is there a better way to differentiate between SSLErrors?
                if 'read operation timed out' not in str(e):  # Defensive:
                    # This shouldn't happen but just in case we're missing an edge
                    # case, let's avoid swallowing SSL errors.
                    raise

                raise ReadTimeoutError(self._pool, None, 'Read timed out.')

            except (h11.ProtocolError, SocketError) as e:
                # This includes IncompleteRead.
                raise ProtocolError('Connection broken: %r' % e, e)

            except GeneratorExit:
                # We swallow GeneratorExit when it is emitted: this allows the
                # use of the error checker inside stream()
                pass
            # If no exception is thrown, we should avoid cleaning up
            # unnecessarily.
            clean_exit = True
        finally:
            # If we didn't terminate cleanly, we need to throw away our
            # connection.
            if not clean_exit:
                self.close()
            # If we hold the original response but it's finished now, we should