Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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()
if hasattr(req_handler, "frame_handler"):
# Convert this to a handler that will utilise H2 specific functionality, such as handling individual frames
req_handler = self.frame_handler(request, response, req_handler)
if hasattr(req_handler, 'handle_headers'):
req_handler.handle_headers(frame, request, response)
elif isinstance(frame, DataReceived):
wfile.write(frame.data)
if hasattr(req_handler, 'handle_data'):
req_handler.handle_data(frame, request, response)
if frame.stream_ended:
wfile.close()
elif frame is None or isinstance(frame, (StreamReset, StreamEnded, ConnectionTerminated)):
self.logger.debug('(%s - %s) Stream Reset, Thread Closing' % (self.uid, stream_id))
break
if request is not None:
request.frames.append(frame)
if hasattr(frame, "stream_ended") and frame.stream_ended:
self.finish_handling(request, response, req_handler)
def handle_events(self, events):
for event in events:
logger.debug("event received from {0}: {1}".format(self.conn_type, event))
if isinstance(event, ResponseReceived):
self.handle_response(event)
elif isinstance(event, RequestReceived):
self.handle_request(event)
elif isinstance(event, DataReceived):
self.handle_data(event)
elif isinstance(event, StreamEnded):
self.handle_end_stream(event.stream_id)
elif isinstance(event, StreamReset):
self.handle_reset(event)
elif isinstance(event, RemoteSettingsChanged):
self.handle_update_settings(event)
elif isinstance(event, WindowUpdated):
self.handle_window_updates(event)
elif isinstance(event, PushedStreamReceived):
self.handle_pushed_stream(event)
elif isinstance(event, PriorityUpdated):
self.handle_priority_updates(event)
elif isinstance(event, ConnectionTerminated):
self.on_terminate(
event.additional_data, event.error_code,
event.last_stream_id)
elif isinstance(event, SettingsAcknowledged):
# Note: nothing need to do with this event
pass
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):
grpc_events.extend(self._connection_terminated(event))
elif isinstance(event, h2.events.AlternativeServiceAvailable):
grpc_events.extend(self._alternative_service_available(event))
elif isinstance(event, h2.events.UnknownFrameReceived):
grpc_events.extend(self._unknown_frame_received(event))
return grpc_events
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()
self._rtt = None
self._functional_timeout = functional_timeout
self._functional = CallableEvent(self._is_functional, loop=loop)
# Dispatch table
self._event_handlers = {
events.RequestReceived: self._request_received,
events.ResponseReceived: self._response_received,
events.TrailersReceived: self._trailers_received,
events.DataReceived: self._data_received,
events.WindowUpdated: self._window_updated,
events.RemoteSettingsChanged: self._remote_settings_changed,
events.PingAcknowledged: self._ping_acknowledged,
events.StreamEnded: self._stream_ended,
events.StreamReset: self._stream_reset,
events.PushedStreamReceived: self._pushed_stream_received,
events.SettingsAcknowledged: self._settings_acknowledged,
events.PriorityUpdated: self._priority_updated,
events.ConnectionTerminated: self._connection_terminated,
}
def data_received(self, data: bytes):
events = self.conn.receive_data(data)
for event in events:
if isinstance(event, ResponseReceived):
self.handle_response(event.headers, event.stream_id)
elif isinstance(event, DataReceived):
self.handle_data(event.data, event.stream_id)
elif isinstance(event, StreamEnded):
self.end_stream(event.stream_id)
elif isinstance(event, StreamReset):
self.reset_stream(event.stream_id)
else:
self.logger.debug("ignored", h2event=event)
data = self.conn.data_to_send()
if data:
self.transport.write(data)
pprint(events)
print("\n\n\n!!!!!!!!!!!")
# 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)
async def _handle_events(self, events: List[h2.events.Event]) -> None:
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()
if not self.known_proto:
self.known_proto = self.transport.negotiatedProtocol
assert self.known_proto == b'h2'
events = self.conn.receive_data(data)
for event in events:
if isinstance(event, ResponseReceived):
self.handleResponse(event.headers)
elif isinstance(event, DataReceived):
self.handleData(event.data)
elif isinstance(event, StreamEnded):
self.endStream()
elif isinstance(event, SettingsAcknowledged):
self.settingsAcked(event)
elif isinstance(event, StreamReset):
reactor.stop()
raise RuntimeError("Stream reset: %d" % event.error_code)
elif isinstance(event, WindowUpdated):
self.windowUpdated(event)
data = self.conn.data_to_send()
if data:
self.transport.write(data)