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_priority(self):
self.conn.update_settings({
SettingCodes.MAX_FRAME_SIZE: 16384,
SettingCodes.INITIAL_WINDOW_SIZE: 16384 * 1024 * 32,
})
event = yield from self._expect_events()
self.assertIsInstance(event[0], SettingsAcknowledged)
event = yield from self.server.events.get()
self.assertIsInstance(event, RemoteSettingsChanged)
stream_1 = yield from self._send_headers()
yield from self.server.start_response(stream_1, [(':status', '200')])
events = yield from self._expect_events()
self.assertIsInstance(events[0], ResponseReceived)
stream_2 = yield from self._send_headers()
yield from self.server.start_response(stream_2, [(':status', '200')])
events = yield from self._expect_events()
self.assertIsInstance(events[0], ResponseReceived)
p1 = 32
p2 = 20
self.server.reprioritize(stream_1, weight=p1)
self.server.reprioritize(stream_2, weight=p2)
self.server.pause_writing()
running = [True]
@asyncio.coroutine
def test_responsereceived_repr(self):
"""
ResponseReceived has a useful debug representation.
"""
e = h2.events.ResponseReceived()
e.stream_id = 500
e.headers = self.example_response_headers
assert repr(e) == (
"
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.StreamEnded) and event.stream_id == 1:
ended_streams += 1
elif isinstance(event, h2.events.PushedStreamReceived):
pushed_streams += 1
h2_conn.reset_stream(event.pushed_stream_id, error_code=0x8)
self.client.wfile.write(h2_conn.data_to_send())
self.client.wfile.flush()
elif isinstance(event, h2.events.ResponseReceived):
responses += 1
if isinstance(event, h2.events.ConnectionTerminated):
done = True
if responses >= 1 and ended_streams >= 1 and pushed_streams == 2:
done = True
h2_conn.close_connection()
self.client.wfile.write(h2_conn.data_to_send())
self.client.wfile.flush()
bodies = [flow.response.content for flow in self.master.state.flows if flow.response]
assert len(bodies) >= 1
assert b'regular_stream' in bodies
# the other two bodies might not be transmitted before the reset
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)
def handle_events (self, events):
for event in events:
if isinstance(event, ResponseReceived):
self.handle_response (event.stream_id, event.headers)
elif isinstance(event, TrailersReceived):
self.handle_trailers (event.stream_id, event.headers)
elif isinstance(event, StreamReset):
if event.remote_reset:
h = self.get_handler (event.stream_id)
if h:
h.response = http_response.FailedResponse (721, respcodes.get (721), h.request)
h.handle_callback ()
self.remove_handler (event.stream_id)
elif isinstance(event, ConnectionTerminated):
self.asyncon.handle_close (721, "HTTP2 Connection Terminated")
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):
for event in events:
if isinstance(event, h2.events.DataReceived):
self._adjust_receive_window(event.flow_controlled_length)
self.streams[event.stream_id].receive_data(event)
elif isinstance(event, h2.events.PushedStreamReceived):
if self._enable_push:
self._new_stream(event.pushed_stream_id, local_closed=True)
self.streams[event.parent_stream_id].receive_push(event)
else:
# Servers are forbidden from sending push promises when
# the ENABLE_PUSH setting is 0, but the spec leaves the
# client action undefined when they do it anyway. So we
# just refuse the stream and go about our business.
self._send_rst_frame(event.pushed_stream_id, 7)
elif isinstance(event, h2.events.ResponseReceived):
self.streams[event.stream_id].receive_response(event)
elif isinstance(event, h2.events.TrailersReceived):
self.streams[event.stream_id].receive_trailers(event)
elif isinstance(event, h2.events.StreamEnded):
self.streams[event.stream_id].receive_end_stream(event)
elif isinstance(event, h2.events.StreamReset):
if event.stream_id not in self.reset_streams:
self.reset_streams.add(event.stream_id)
self.streams[event.stream_id].receive_reset(event)
elif isinstance(event, h2.events.ConnectionTerminated):
# If we get GoAway with error code zero, we are doing a
# graceful shutdown and all is well. Otherwise, throw an
# exception.
self.close()
# If an error occured, try to read the error description from
def response_received(self, previous_state):
"""
Fires when a response is received. Also disambiguates between responses
and trailers.
"""
if not self.headers_received:
assert self.client is True
self.headers_received = True
event = ResponseReceived()
else:
assert not self.trailers_received
self.trailers_received = True
event = TrailersReceived()
event.stream_id = self.stream_id
return [event]
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,
}
for event in events:
if isinstance(event, h2.events.DataReceived):
self._adjust_receive_window(event.flow_controlled_length)
self.streams[event.stream_id].receive_data(event)
elif isinstance(event, h2.events.PushedStreamReceived):
if self._enable_push:
self._new_stream(event.pushed_stream_id, local_closed=True)
self.streams[event.parent_stream_id].receive_push(event)
else:
# Servers are forbidden from sending push promises when
# the ENABLE_PUSH setting is 0, but the spec leaves the
# client action undefined when they do it anyway. So we
# just refuse the stream and go about our business.
self._send_rst_frame(event.pushed_stream_id, 7)
elif isinstance(event, h2.events.ResponseReceived):
self.streams[event.stream_id].receive_response(event)
elif isinstance(event, h2.events.TrailersReceived):
self.streams[event.stream_id].receive_trailers(event)
elif isinstance(event, h2.events.StreamEnded):
self.streams[event.stream_id].receive_end_stream(event)
elif isinstance(event, h2.events.StreamReset):
if event.stream_id not in self.reset_streams:
self.reset_streams.add(event.stream_id)
self.streams[event.stream_id].receive_reset(event)
elif isinstance(event, h2.events.ConnectionTerminated):
# If we get GoAway with error code zero, we are doing a
# graceful shutdown and all is well. Otherwise, throw an
# exception.
self.close()
# If an error occured, try to read the error description from