Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _send_headers(self, end_stream=False):
headers = [
(':method', 'GET'),
(':authority', 'example.com'),
(':scheme', 'h2c'),
(':path', '/index.html'),
]
stream_id = self.conn.get_next_available_stream_id()
self.conn.send_headers(stream_id, headers, end_stream=end_stream)
yield from self._expect_events(0)
event = yield from self.server.events.get()
self.assertIsInstance(event, RequestReceived)
self.assertEqual(event.stream_id, stream_id)
self.assertEqual(event.headers, headers)
return stream_id
RequestReceived events.
"""
config = h2.config.H2Configuration(
client_side=False, header_encoding=encoding
)
c = h2.connection.H2Connection(config=config)
c.receive_data(frame_factory.preamble())
f = frame_factory.build_headers_frame(headers)
data = f.serialize()
events = c.receive_data(data)
assert len(events) == 1
event = events[0]
assert isinstance(event, h2.events.RequestReceived)
assert_header_blocks_actually_equal(headers, event.headers)
def dataReceived(self, data):
try:
events = self._conn.receive_data(data)
except h2.exceptions.ProtocolError:
# this try/except block catches exceptions due to race between sending
# GOAWAY and processing a response in flight.
return
if self._conn.data_to_send:
self.transport.write(self._conn.data_to_send())
for event in events:
if isinstance(event, h2.events.RequestReceived) and self._handlers.has_key('RequestReceived'):
logging.info('RequestReceived Event for stream: %d' % event.stream_id)
self._handlers['RequestReceived'](event)
elif isinstance(event, h2.events.DataReceived) and self._handlers.has_key('DataReceived'):
logging.info('DataReceived Event for stream: %d' % event.stream_id)
self._handlers['DataReceived'](event)
elif isinstance(event, h2.events.WindowUpdated) and self._handlers.has_key('WindowUpdated'):
logging.info('WindowUpdated Event for stream: %d' % event.stream_id)
self._handlers['WindowUpdated'](event)
elif isinstance(event, h2.events.PingAcknowledged) and self._handlers.has_key('PingAcknowledged'):
logging.info('PingAcknowledged Event')
self._handlers['PingAcknowledged'](event)
self.transport.write(self._conn.data_to_send())
def run_forever(self):
self.conn.initiate_connection()
self.sock.sendall(self.conn.data_to_send())
while True:
data = self.sock.recv(65535)
if not data:
break
events = self.conn.receive_data(data)
for event in events:
if isinstance(event, RequestReceived):
self.request_received(event.headers, event.stream_id)
elif isinstance(event, DataReceived):
self.conn.reset_stream(event.stream_id)
self.sock.sendall(self.conn.data_to_send())
ready = select.select([self.sock], [], [], SELECT_TIMEOUT)
if not ready[0]:
continue
data = self.sock.recv(65535)
if not data:
break
try:
events = self.conn.receive_data(data)
except StreamClosedError:
# TODO what if data was also recieved for a non-closed stream?
continue
for event in events:
if isinstance(event, RequestReceived):
self.request_received(event)
elif isinstance(event, ResponseReceived):
self.response_received(event)
elif isinstance(event, DataReceived):
self.data_received(event)
elif isinstance(event, StreamEnded):
self.stream_ended(event)
elif isinstance(event, StreamReset):
self.stream_reset(event)
elif isinstance(event, WindowUpdated):
self.window_updated(event)
elif isinstance(event, RemoteSettingsChanged):
self.settings_changed(event)
elif isinstance(event, SettingsAcknowledged):
self.settings_acknowledged(event)
elif isinstance(event, TrailersReceived):
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):
def dataReceived(self, data):
if not self.known_proto:
self.known_proto = True
try:
events = self.conn.receive_data(data)
except ProtocolError:
if self.conn.data_to_send:
self.transport.write(self.conn.data_to_send())
self.transport.loseConnection()
else:
for event in events:
if isinstance(event, RequestReceived):
self.requestReceived(event.headers, event.stream_id)
elif isinstance(event, DataReceived):
self.dataFrameReceived(event.stream_id)
elif isinstance(event, WindowUpdated):
self.windowUpdated(event)
if self.conn.data_to_send:
self.transport.write(self.conn.data_to_send())
def data_received(self, data: bytes):
try:
events = self.conn.receive_data(data)
except ProtocolError as e:
self.transport.write(self.conn.data_to_send())
self.transport.close()
else:
self.transport.write(self.conn.data_to_send())
for event in events:
if isinstance(event, RequestReceived):
self.request_received(event.headers, event.stream_id)
elif isinstance(event, DataReceived):
self.receive_data(event.data, event.stream_id)
elif isinstance(event, StreamEnded):
self.stream_complete(event.stream_id)
elif isinstance(event, ConnectionTerminated):
self.transport.close()
elif isinstance(event, StreamReset):
self.stream_reset(event.stream_id)
elif isinstance(event, WindowUpdated):
self.window_updated(event.stream_id, event.delta)
elif isinstance(event, RemoteSettingsChanged):
if SettingCodes.INITIAL_WINDOW_SIZE in event.changed_settings:
self.window_updated(None, 0)
self.transport.write(self.conn.data_to_send())
self._is_resumed = False
self._resumed = CallableEvent(lambda: self._is_resumed, loop=loop)
self._stream_creatable = CallableEvent(self._is_stream_creatable,
loop=loop)
self._last_active = 0
self._ping_index = -1
self._ping_time = 0
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 _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()