Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rst_stream_frame_has_no_flags(self):
f = RstStreamFrame(1)
flags = f.parse_flags(0xFF)
assert not flags
assert isinstance(flags, Flags)
def reset_stream(self, error_code=0):
"""
Close the stream locally. Reset the stream with an error code.
"""
self.config.logger.debug(
"Local reset %r with error code: %d", self, error_code
)
self.state_machine.process_input(StreamInputs.SEND_RST_STREAM)
rsf = RstStreamFrame(self.stream_id)
rsf.error_code = error_code
return [rsf]
f.error_code = e.error_code
self._prepare_for_sending([f])
events = e._events
else:
raise
except StreamIDTooLowError as e:
# The stream ID seems invalid. This may happen when the closed
# stream has been cleaned up, or when the remote peer has opened a
# new stream with a higher stream ID than this one, forcing it
# closed implicitly.
#
# Check how the stream was closed: depending on the mechanism, it
# is either a stream error or a connection error.
if self._stream_is_closed_by_reset(e.stream_id):
# Closed by RST_STREAM is a stream error.
f = RstStreamFrame(e.stream_id)
f.error_code = ErrorCodes.STREAM_CLOSED
self._prepare_for_sending([f])
events = []
elif self._stream_is_closed_by_end(e.stream_id):
# Closed by END_STREAM is a connection error.
raise StreamClosedError(e.stream_id)
else:
# Closed implicitly, also a connection error, but of type
# PROTOCOL_ERROR.
raise
else:
self._prepare_for_sending(frames)
return events
try:
stream = self._get_stream_by_id(frame.stream_id)
except NoSuchStreamError:
# We need to check if the parent stream was reset by us. If it was
# then we presume that the PUSH_PROMISE was in flight when we reset
# the parent stream. Rather than accept the new stream, just reset
# it.
#
# If this was closed naturally, however, we should call this a
# PROTOCOL_ERROR: pushing a stream on a naturally closed stream is
# a real problem because it creates a brand new stream that the
# remote peer now believes exists.
if (self._stream_closed_by(frame.stream_id) ==
StreamClosedBy.SEND_RST_STREAM):
f = RstStreamFrame(frame.promised_stream_id)
f.error_code = ErrorCodes.REFUSED_STREAM
return [f], events
raise ProtocolError("Attempted to push on closed stream.")
# We need to prevent peers pushing streams in response to streams that
# they themselves have already pushed: see #163 and RFC 7540 § 6.6. The
# easiest way to do that is to assert that the stream_id is not even:
# this shortcut works because only servers can push and the state
# machine will enforce this.
if (frame.stream_id % 2) == 0:
raise ProtocolError("Cannot recursively push streams.")
try:
frames, stream_events = stream.receive_push_promise_in_band(
frame.promised_stream_id,
def test_rst_stream_frame_must_have_body_length_four(self):
f = RstStreamFrame(1)
with pytest.raises(ValueError):
f.parse_body(b'\x01')
self._data_to_send = b''
# Keeps track of streams that have been forcefully reset by this peer.
# Used to ensure that we don't blow up in the face of frames that were
# in flight when a stream was reset.
self._reset_streams = set()
# When in doubt use dict-dispatch.
self._frame_dispatch_table = {
HeadersFrame: self._receive_headers_frame,
PushPromiseFrame: self._receive_push_promise_frame,
SettingsFrame: self._receive_settings_frame,
DataFrame: self._receive_data_frame,
WindowUpdateFrame: self._receive_window_update_frame,
PingFrame: self._receive_ping_frame,
RstStreamFrame: self._receive_rst_stream_frame,
PriorityFrame: self._receive_priority_frame,
GoAwayFrame: self._receive_goaway_frame,
ContinuationFrame: self._receive_naked_continuation,
AltSvcFrame: self._receive_alt_svc_frame,
}
try:
stream = self._get_stream_by_id(frame.stream_id)
except NoSuchStreamError:
# We need to check if the parent stream was reset by us. If it was
# then we presume that the PUSH_PROMISE was in flight when we reset
# the parent stream. Rather than accept the new stream, just reset
# it.
#
# If this was closed naturally, however, we should call this a
# PROTOCOL_ERROR: pushing a stream on a naturally closed stream is
# a real problem because it creates a brand new stream that the
# remote peer now believes exists.
if (self._stream_closed_by(frame.stream_id) ==
StreamClosedBy.SEND_RST_STREAM):
f = RstStreamFrame(frame.promised_stream_id)
f.error_code = ErrorCodes.REFUSED_STREAM
return [f], events
raise ProtocolError("Attempted to push on closed stream.")
# We need to prevent peers pushing streams in response to streams that
# they themselves have already pushed: see #163 and RFC 7540 § 6.6. The
# easiest way to do that is to assert that the stream_id is not even:
# this shortcut works because only servers can push and the state
# machine will enforce this.
if (frame.stream_id % 2) == 0:
raise ProtocolError("Cannot recursively push streams.")
try:
frames, stream_events = stream.receive_push_promise_in_band(
frame.promised_stream_id,
def reset_stream(self, error_code=0):
"""
Close the stream locally. Reset the stream with an error code.
"""
self.config.logger.debug(
"Local reset %r with error code: %d", self, error_code
)
self.state_machine.process_input(StreamInputs.SEND_RST_STREAM)
rsf = RstStreamFrame(self.stream_id)
rsf.error_code = error_code
return [rsf]
.. versionchanged:: 2.0.0
Removed from the public API.
"""
try:
# I don't love using __class__ here, maybe reconsider it.
frames, events = self._frame_dispatch_table[frame.__class__](frame)
except StreamClosedError as e:
# We need to send a RST_STREAM frame on behalf of the stream.
# The frame the stream wants to emit is already present in the
# exception.
# This does not require re-raising: it's an expected behaviour. The
# only time we don't do that is if this is a stream the user
# manually reset.
if frame.stream_id not in self._reset_streams:
f = RstStreamFrame(e.stream_id)
f.error_code = e.error_code
self._prepare_for_sending([f])
events = e._events
else:
events = []
except StreamIDTooLowError as e:
# The stream ID seems invalid. This is unlikely, so it's probably
# the case that this frame is actually for a stream that we've
# already reset and removed the state for. If it is, just swallow
# the error. If we didn't do that, re-raise.
if frame.stream_id not in self._reset_streams:
raise
events = []
except KeyError as e: # pragma: no cover
# We don't have a function for handling this frame. Let's call this
# a PROTOCOL_ERROR and exit.
)
try:
stream = self._get_stream_by_id(frame.stream_id)
except NoSuchStreamError:
# We need to check if the parent stream was reset by us. If it was
# then we presume that the PUSH_PROMISE was in flight when we reset
# the parent stream. Rather than accept the new stream, just reset
# it.
#
# If this was closed naturally, however, we should call this a
# PROTOCOL_ERROR: pushing a stream on a naturally closed stream is
# a real problem because it creates a brand new stream that the
# remote peer now believes exists.
if frame.stream_id in self._reset_streams:
f = RstStreamFrame(frame.promised_stream_id)
f.error_code = ErrorCodes.REFUSED_STREAM
return [f], events
raise ProtocolError("Attempted to push on closed stream.")
# We need to prevent peers pushing streams in response to streams that
# they themselves have already pushed: see #163 and RFC 7540 § 6.6. The
# easiest way to do that is to assert that the stream_id is not even:
# this shortcut works because only servers can push and the state
# machine will enforce this.
if (frame.stream_id % 2) == 0:
raise ProtocolError("Cannot recursively push streams.")
frames, stream_events = stream.receive_push_promise_in_band(
frame.promised_stream_id,
pushed_headers,