Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def client():
c = h2.connection.H2Connection()
# Do the handshake. First send the preamble.
c.initiate_connection()
data = yield c.data_to_send()
# Next, handle the remote preamble.
events = c.receive_data(data)
assert len(events) == 2
assert isinstance(events[0], h2.events.SettingsAcknowledged)
assert isinstance(events[1], h2.events.RemoteSettingsChanged)
changed = events[1].changed_settings
assert (
changed[
h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS
].new_value == 100
)
# Send a request.
events = c.send_headers(1, request_headers, end_stream=True)
assert not events
data = yield c.data_to_send()
# Validate the response.
events = c.receive_data(data)
assert len(events) == 2
def test_only_reports_changed_settings(self,
old_settings_list,
new_settings_list):
"""
Settings that were not changed are not reported.
"""
old_settings_dict = dict(old_settings_list)
new_settings_dict = dict(new_settings_list)
e = h2.events.RemoteSettingsChanged.from_settings(
old_settings=old_settings_dict,
new_settings=new_settings_dict,
)
assert len(e.changed_settings) == len(new_settings_dict)
assert (
sorted(list(e.changed_settings.keys())) ==
sorted(list(new_settings_dict.keys()))
)
input_frame = frame_factory.build_headers_frame(
headers=self.example_request_headers,
flags=['END_STREAM', 'PRIORITY'],
stream_weight=15,
depends_on=0,
exclusive=False,
)
events = c.receive_data(input_frame.serialize())
assert len(events) == 3
base_event = events[0]
other_events = events[1:]
assert base_event.stream_ended in other_events
assert isinstance(base_event.stream_ended, h2.events.StreamEnded)
assert base_event.priority_updated in other_events
assert isinstance(
base_event.priority_updated, h2.events.PriorityUpdated
)
request = mock.MagicMock()
request.start_time = self.io_loop.time()
request.request_timeout = 3
callback_cleanup = mock.MagicMock()
callback_response = mock.MagicMock()
stream = HTTP2ClientStream(
connection, request,
callback_cleanup, callback_response, self.io_loop
)
stream.finish = mock.MagicMock()
event = mock.Mock(spec=h2.events.StreamEnded)
stream.handle_event(event)
stream.finish.assert_called_once()
stream.finish.reset_mock()
event = mock.Mock(spec=h2.events.StreamReset)
stream.handle_event(event)
stream.finish.assert_called_once()
)
done = False
while not done:
try:
raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
self.client.wfile.write(h2_conn.data_to_send())
self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamReset):
done = True
h2_conn.close_connection()
self.client.wfile.write(h2_conn.data_to_send())
self.client.wfile.flush()
assert len(self.master.state.flows) == 0
def test_receiving_settings_frame_event(self, frame_factory):
"""
Settings frames should cause a RemoteSettingsChanged event to fire.
"""
c = h2.connection.H2Connection(config=self.server_config)
c.receive_data(frame_factory.preamble())
f = frame_factory.build_settings_frame(
settings=helpers.SAMPLE_SETTINGS
)
events = c.receive_data(f.serialize())
assert len(events) == 1
event = events[0]
assert isinstance(event, h2.events.RemoteSettingsChanged)
assert len(event.changed_settings) == len(helpers.SAMPLE_SETTINGS)
async def process_data(self, data: bytes) -> None:
try:
events = self.connection.receive_data(data)
except h2.exceptions.ProtocolError:
raise MustCloseError()
else:
for event in events:
if isinstance(event, h2.events.RequestReceived):
await self.create_stream(event)
elif isinstance(event, h2.events.DataReceived):
await self.streams[event.stream_id].data_received(event.data)
self.connection.acknowledge_received_data(
event.flow_controlled_length, event.stream_id
)
elif isinstance(event, h2.events.StreamReset):
await self.streams[event.stream_id].reset()
elif isinstance(event, h2.events.StreamEnded):
await self.streams[event.stream_id].ended()
elif isinstance(event, h2.events.WindowUpdated):
self.window_updated(event.stream_id)
elif isinstance(event, h2.events.ConnectionTerminated):
raise MustCloseError()
await self.send()
if type(event) in self.event_handlers:
for ev_handler in self.event_handlers[type(event)]:
ev_handler(event)
if isinstance(event, h2.events.DataReceived):
recv_streams[stream_id] = (recv_streams.get(stream_id, 0) +
event.flow_controlled_length)
elif isinstance(event, h2.events.WindowUpdated):
if stream_id == 0:
self.flow_control_window.produce(event.delta)
log.debug(
'INCREMENTED CONNECTION WINDOW BY %d, NOW AT %d',
event.delta, self.flow_control_window.value
)
elif isinstance(event, h2.events.RemoteSettingsChanged):
self.process_settings(event)
if stream_id and stream_id in self._ongoing_streams:
stream = self._ongoing_streams[stream_id]
with stack_context.ExceptionStackContext(
stream.handle_exception
):
stream.handle_event(event)
recv_connection = 0
for stream_id, num_bytes in six.iteritems(recv_streams):
if not num_bytes or stream_id not in self._ongoing_streams:
continue
log.debug(
'Incrementing flow control window for stream %d with %d',
stillActive = self._tryToWriteControlData()
if stillActive:
self.transport.loseConnection()
self.connectionLost(Failure(), _cancelTimeouts=False)
return
# Only reset the timeout if we've received an actual H2
# protocol message
self.resetTimeout()
for event in events:
if isinstance(event, h2.events.RequestReceived):
self._requestReceived(event)
elif isinstance(event, h2.events.DataReceived):
self._requestDataReceived(event)
elif isinstance(event, h2.events.StreamEnded):
self._requestEnded(event)
elif isinstance(event, h2.events.StreamReset):
self._requestAborted(event)
elif isinstance(event, h2.events.WindowUpdated):
self._handleWindowUpdate(event)
elif isinstance(event, h2.events.PriorityUpdated):
self._handlePriorityUpdate(event)
elif isinstance(event, h2.events.ConnectionTerminated):
self.transport.loseConnection()
self.connectionLost(
ConnectionLost("Remote peer sent GOAWAY"),
_cancelTimeouts=False,
)
self._tryToWriteControlData()