Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
)
self.assertEqual(stream.flow_control_window.value, initial_window_size)
event = mock.Mock(spec=h2.events.WindowUpdated)
event.delta = initial_window_size
stream.handle_event(event)
self.assertEqual(
stream.flow_control_window.value, initial_window_size * 2
)
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].handle(
Body(stream_id=event.stream_id, data=event.data)
)
self.connection.acknowledge_received_data(
event.flow_controlled_length, event.stream_id
)
elif isinstance(event, h2.events.StreamEnded):
await self.streams[event.stream_id].handle(EndBody(stream_id=event.stream_id))
elif isinstance(event, h2.events.StreamReset):
await self._close_stream(event.stream_id)
await self._window_updated(event.stream_id)
elif isinstance(event, h2.events.WindowUpdated):
await self._window_updated(event.stream_id)
elif isinstance(event, h2.events.PriorityUpdated):
await self._priority_updated(event)
elif isinstance(event, h2.events.RemoteSettingsChanged):
if h2.settings.SettingCodes.INITIAL_WINDOW_SIZE in event.changed_settings:
await self._window_updated(None)
elif isinstance(event, h2.events.ConnectionTerminated):
await self.send(Closed())
await self._flush()
# import pdb; pdb.set_trace()
for event in events:
if isinstance(event, RemoteSettingsChanged):
self.handle_remote_settings_changed(event)
elif isinstance(event, RequestReceived):
self.handle_request_received(event)
elif isinstance(event, DataReceived):
self.handle_data_received(event)
elif isinstance(event, StreamEnded):
self.handle_stream_ended(event)
elif isinstance(event, SettingsAcknowledged):
# TODO check if we can do anything else
pass
elif isinstance(event, StreamReset):
raise NotImplementedError()
elif isinstance(event, WindowUpdated):
raise NotImplementedError()
elif isinstance(event, PriorityUpdated):
raise NotImplementedError()
elif isinstance(event, ConnectionTerminated):
self.transport.close()
# raise NotImplementedError()
else:
# TODO good for testing, but should not go to the final release!
raise NotImplementedError(f'Event "{event}" is not supported.')
self.transport_write(COMMUNICATION_WITH_CLIENT)
def receive_data(self, data: bytes):
events = self.h2_connection.receive_data(data)
grpc_events = []
for event in events:
if isinstance(event, h2.events.RequestReceived):
grpc_events.extend(self._request_received(event))
elif isinstance(event, h2.events.ResponseReceived):
grpc_events.extend(self._response_received(event))
elif isinstance(event, h2.events.TrailersReceived):
grpc_events.extend(self._trailers_received(event))
elif isinstance(event, h2.events.InformationalResponseReceived):
grpc_events.extend(self._informational_response_received(event))
elif isinstance(event, h2.events.DataReceived):
grpc_events.extend(self._data_received(event))
elif isinstance(event, h2.events.WindowUpdated):
grpc_events.extend(self._window_updated(event))
elif isinstance(event, h2.events.RemoteSettingsChanged):
grpc_events.extend(self._remote_settings_changed(event))
elif isinstance(event, h2.events.PingAcknowledged):
grpc_events.extend(self._ping_acknowledged(event))
elif isinstance(event, h2.events.StreamEnded):
grpc_events.extend(self._stream_ended(event))
elif isinstance(event, h2.events.StreamReset):
grpc_events.extend(self._stream_reset(event))
elif isinstance(event, h2.events.PushedStreamReceived):
grpc_events.extend(self._push_stream_received(event))
elif isinstance(event, h2.events.SettingsAcknowledged):
grpc_events.extend(self._settings_acknowledged(event))
elif isinstance(event, h2.events.PriorityUpdated):
grpc_events.extend(self._priority_updated(event))
elif isinstance(event, h2.events.ConnectionTerminated):
def _handle_events(self, events: List[h2.events.Event]) -> None:
for event in events:
if isinstance(event, h2.events.RequestReceived):
headers = CIMultiDict()
for name, value in event.headers:
headers.add(name.title(), value)
self.handle_request(
event.stream_id, headers[':method'].upper(), headers[':path'], headers,
)
elif isinstance(event, h2.events.DataReceived):
self.streams[event.stream_id].append(event.data)
elif isinstance(event, h2.events.StreamReset):
self.streams.pop(event.stream_id).task.cancel()
elif isinstance(event, h2.events.StreamEnded):
self.streams[event.stream_id].complete()
elif isinstance(event, h2.events.WindowUpdated):
self._window_updated(event.stream_id)
elif isinstance(event, h2.events.ConnectionTerminated):
self.close()
return
self.send(self.connection.data_to_send()) # type: ignore
)
if frame.stream_id:
stream = self._get_stream_by_id(frame.stream_id)
frames, stream_events = stream.receive_window_update(
frame.window_increment
)
else:
# Increment our local flow control window.
self.outbound_flow_control_window = guard_increment_window(
self.outbound_flow_control_window,
frame.window_increment
)
# FIXME: Should we split this into one event per active stream?
window_updated_event = WindowUpdated()
window_updated_event.stream_id = 0
window_updated_event.delta = frame.window_increment
stream_events = [window_updated_event]
frames = []
return frames, events + stream_events
dataToSend = self.conn.data_to_send()
self.transport.write(dataToSend)
self.transport.loseConnection()
self.connectionLost("Protocol error from peer.")
return
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("Shutdown by remote peer")
dataToSend = self.conn.data_to_send()
if dataToSend:
self.transport.write(dataToSend)
dispatches them to their corresponding HTTPStreams.
"""
recv_streams = dict()
for event in events:
log.debug(['PROCESSING EVENT', event])
stream_id = getattr(event, 'stream_id', None)
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)
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()
def window_updated(self, previous_state):
"""
Fires when a window update frame is received.
"""
event = WindowUpdated()
event.stream_id = self.stream_id
return [event]