How to use the hyperframe.exceptions.InvalidFrameError function in hyperframe

To help you get started, we’ve selected a few hyperframe 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 python-hyper / hyperframe / hyperframe / frame.py View on Github external
def parse_body(self, data):
        try:
            self.last_stream_id, self.error_code = _STRUCT_LL.unpack(
                data[:8]
            )
        except struct.error:
            raise InvalidFrameError("Invalid GOAWAY body.")

        self.body_len = len(data)

        if len(data) > 8:
            self.additional_data = data[8:].tobytes()
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def parse_padding_data(self, data):
        if 'PADDED' in self.flags:
            try:
                self.pad_length = struct.unpack('!B', data[:1])[0]
            except struct.error:
                raise InvalidFrameError("Invalid Padding data")
            return 1
        return 0
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def parse_body(self, data):
        if len(data) > 5:
            raise InvalidFrameError(
                "PRIORITY must have 5 byte body: actual length %s." %
                len(data)
            )

        self.parse_priority_data(data)
        self.body_len = 5
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def serialize_body(self):
        if len(self.opaque_data) > 8:
            raise InvalidFrameError(
                "PING frame may not have more than 8 bytes of data, got %s" %
                self.opaque_data
            )

        data = self.opaque_data
        data += b'\x00' * (8 - len(self.opaque_data))
        return data
github python-hyper / hyper-h2 / h2 / frame_buffer.py View on Github external
raise ProtocolError("Received frame with invalid frame header.")

        # Next, check that we have enough length to parse the frame body. If
        # not, bail, leaving the frame header data in the buffer for next time.
        if len(self.data) < length + 9:
            raise StopIteration()

        # Confirm the frame has an appropriate length.
        self._validate_frame_length(length)

        # Don't try to parse the body if we didn't get a frame we know about:
        # there's nothing we can do with it anyway.
        if f is not None:
            try:
                f.parse_body(memoryview(self.data[9:9+length]))
            except InvalidFrameError:
                raise FrameDataMissingError("Frame data missing or invalid")

        # At this point, as we know we'll use or discard the entire frame, we
        # can update the data.
        self.data = self.data[9+length:]

        # Pass the frame through the header buffer.
        f = self._update_header_buffer(f)

        # If we got a frame we didn't understand or shouldn't yield, rather
        # than return None it'd be better if we just tried to get the next
        # frame in the sequence instead. Recurse back into ourselves to do
        # that. This is safe because the amount of work we have to do here is
        # strictly bounded by the length of the buffer.
        return f if f is not None else self.next()
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def __init__(self, stream_id, origin=b'', field=b'', **kwargs):
        super(AltSvcFrame, self).__init__(stream_id, **kwargs)

        if not isinstance(origin, bytes):
            raise InvalidFrameError("AltSvc origin must be bytestring.")
        if not isinstance(field, bytes):
            raise InvalidFrameError("AltSvc field must be a bytestring.")
        self.origin = origin
        self.field = field
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
This populates the flags field, and determines how long the body is.

        :param strict: Whether to raise an exception when encountering a frame
            not defined by spec and implemented by hyperframe.

        :raises hyperframe.exceptions.UnknownFrameError: If a frame of unknown
            type is received.

        .. versionchanged:: 5.0.0
            Added :param:`strict` to accommodate :class:`ExtensionFrame`
        """
        try:
            fields = _STRUCT_HBBBL.unpack(header)
        except struct.error:
            raise InvalidFrameError("Invalid frame header")

        # First 24 bits are frame length.
        length = (fields[0] << 8) + fields[1]
        type = fields[2]
        flags = fields[3]
        stream_id = fields[4] & 0x7FFFFFFF

        try:
            frame = FRAMES[type](stream_id)
        except KeyError:
            if strict:
                raise UnknownFrameError(type, length)
            frame = ExtensionFrame(type=type, stream_id=stream_id)

        frame.parse_flags(flags)
        return (frame, length)
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
#: The stream identifier for the stream this frame was received on.
        #: Set to 0 for frames sent on the connection (stream-id 0).
        self.stream_id = stream_id

        #: The flags set for this frame.
        self.flags = Flags(self.defined_flags)

        #: The frame length, excluding the nine-byte header.
        self.body_len = 0

        for flag in flags:
            self.flags.add(flag)

        if (not self.stream_id and
           self.stream_association == _STREAM_ASSOC_HAS_STREAM):
            raise InvalidFrameError('Stream ID must be non-zero')
        if (self.stream_id and
           self.stream_association == _STREAM_ASSOC_NO_STREAM):
            raise InvalidFrameError('Stream ID must be zero')
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def __init__(self, stream_id, origin=b'', field=b'', **kwargs):
        super(AltSvcFrame, self).__init__(stream_id, **kwargs)

        if not isinstance(origin, bytes):
            raise InvalidFrameError("AltSvc origin must be bytestring.")
        if not isinstance(field, bytes):
            raise InvalidFrameError("AltSvc field must be a bytestring.")
        self.origin = origin
        self.field = field
github python-hyper / hyperframe / hyperframe / frame.py View on Github external
def parse_body(self, data):
        if len(data) > 4:
            raise InvalidFrameError(
                "WINDOW_UPDATE frame must have 4 byte length: got %s" %
                len(data)
            )

        try:
            self.window_increment = _STRUCT_L.unpack(data)[0]
        except struct.error:
            raise InvalidFrameError("Invalid WINDOW_UPDATE body")

        if not 1 <= self.window_increment <= 2**31-1:
            raise InvalidFrameError(
                "WINDOW_UPDATE increment must be between 1 to 2^31-1"
            )

        self.body_len = 4