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_cannot_set_invalid_vals_for_initial_window_size(self, val):
"""
SETTINGS_INITIAL_WINDOW_SIZE only allows values between 0 and 2**32 - 1
inclusive.
"""
s = h2.settings.Settings()
if 0 <= val <= 2**31 - 1:
s.initial_window_size = val
s.acknowledge()
assert s.initial_window_size == val
else:
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.initial_window_size = val
s.acknowledge()
assert (
e.value.error_code == h2.errors.ErrorCodes.FLOW_CONTROL_ERROR
)
assert s.initial_window_size == 65535
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] = val
s.acknowledge()
assert (
e.value.error_code == h2.errors.ErrorCodes.FLOW_CONTROL_ERROR
)
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535
"""
s = h2.settings.Settings()
if val >= 0:
s.max_header_list_size = val
s.acknowledge()
assert s.max_header_list_size == val
else:
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.max_header_list_size = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s.max_header_list_size is None
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE] = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
with pytest.raises(KeyError):
s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE]
def test_restricting_outbound_frame_size_by_settings(self, frame_factory):
"""
The remote peer can shrink the maximum outbound frame size using
settings.
"""
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)
c.receive_data(f.serialize())
c.clear_outbound_data_buffer()
with pytest.raises(h2.exceptions.FrameTooLargeError):
c.send_data(1, b'\x01' * 17000)
received_frame = frame_factory.build_settings_frame(
{h2.settings.SettingCodes.MAX_FRAME_SIZE: 17001}
)
c.receive_data(received_frame.serialize())
c.send_data(1, b'\x01' * 17000)
assert c.data_to_send()
def test_cannot_reset_nonexistent_stream(self, frame_factory):
"""
Resetting nonexistent streams raises NoSuchStreamError.
"""
c = h2.connection.H2Connection(config=self.server_config)
c.receive_data(frame_factory.preamble())
f = frame_factory.build_headers_frame(
self.example_request_headers,
stream_id=3
)
c.receive_data(f.serialize())
with pytest.raises(h2.exceptions.NoSuchStreamError) as e:
c.reset_stream(stream_id=1)
assert e.value.stream_id == 1
with pytest.raises(h2.exceptions.NoSuchStreamError) as e:
c.reset_stream(stream_id=5)
assert e.value.stream_id == 5
def test_cannot_send_on_closed_streams(self, input_):
"""
Sending anything but a PRIORITY frame is forbidden on closed streams.
"""
c = h2.stream.H2StreamStateMachine(stream_id=1)
c.state = h2.stream.StreamState.CLOSED
expected_error = (
h2.exceptions.ProtocolError
if input_ == h2.stream.StreamInputs.SEND_PUSH_PROMISE
else h2.exceptions.StreamClosedError
)
with pytest.raises(expected_error):
c.process_input(input_)
async def handle(self, event: Event) -> None:
if isinstance(event, RawData):
try:
events = self.connection.receive_data(event.data)
except h2.exceptions.ProtocolError:
await self._flush()
await self.send(Closed())
else:
await self._handle_events(events)
elif isinstance(event, Closed):
self.closed = True
stream_ids = list(self.streams.keys())
for stream_id in stream_ids:
await self._close_stream(stream_id)
def send_error_response(self, code, message, headers=None) -> None:
try:
response = http.make_error_response(code, message, headers)
self.send_response(response)
except (exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException):
self.log("Failed to send error response to client: {}".format(message), "debug")
def data_received(self, data: bytes) -> None:
super().data_received(data)
try:
events = self.connection.receive_data(data)
except h2.exceptions.ProtocolError:
self.write(self.connection.data_to_send()) # type: ignore
self.close()
else:
self._handle_events(events)
self.write(self.connection.data_to_send()) # type: ignore
(b":method", b"GET"),
(b":path", path.encode()),
(b":scheme", stream.scope["scheme"].encode()),
(b":authority", authority),
],
headers,
self.response_headers(),
)
]
try:
self.connection.push_stream(
stream_id=stream_id,
promised_stream_id=push_stream_id,
request_headers=request_headers,
)
except h2.exceptions.ProtocolError:
# Client does not accept push promises or we are trying to
# push on a push promises request.
pass
else:
event = h2.events.RequestReceived()
event.stream_id = push_stream_id
event.headers = request_headers
await self.create_stream(event, complete=True)
def safe_reset_stream(self, stream_id: int, error_code: int):
with self.lock:
try:
self.reset_stream(stream_id, error_code)
except h2.exceptions.StreamClosedError: # pragma: no cover
# stream is already closed - good
pass
self.conn.send(self.data_to_send())