Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def socket_handler(listener):
sock = listener.accept()[0]
# We should get one packet. Rather than respond to it, send a
# GOAWAY frame with error code 0 indicating clean shutdown.
sock.recv(65535)
# Now, send the shut down.
f = GoAwayFrame(0)
f.error_code = 0
sock.send(f.serialize())
# Wait for the message from the main thread.
recv_event.wait(5)
sock.close()
def test_go_away_has_no_flags(self):
f = GoAwayFrame()
flags = f.parse_flags(0xFF)
assert not flags
assert isinstance(flags, Flags)
def build_goaway_frame(self,
last_stream_id,
error_code=0,
additional_data=b''):
"""
Builds a single GOAWAY frame.
"""
f = GoAwayFrame(0)
f.error_code = error_code
f.last_stream_id = last_stream_id
f.additional_data = additional_data
return f
a reason for closing the connection. Must be a bytestring.
:param last_stream_id: (optional) The last stream which was processed
by the sender. Defaults to ``highest_inbound_stream_id``.
:returns: Nothing
"""
self.config.logger.debug("Close connection")
self.state_machine.process_input(ConnectionInputs.SEND_GOAWAY)
# Additional_data must be bytes
if additional_data is not None:
assert isinstance(additional_data, bytes)
if last_stream_id is None:
last_stream_id = self.highest_inbound_stream_id
f = GoAwayFrame(
stream_id=0,
last_stream_id=last_stream_id,
error_code=error_code,
additional_data=(additional_data or b'')
)
self._prepare_for_sending([f])
def test_race_condition_on_socket_close(self):
# Prepare a socket so we can open a stream.
sock = DummySocket()
c = HTTP20Connection('www.google.com')
c._sock = sock
# Open a few requests (which creates a stream)
s1 = c.request('GET', '/')
c.request('GET', '/')
# simulate state of blocking on read while sock
f = GoAwayFrame(0)
# Set error code to PROTOCOL_ERROR
f.error_code = 1
c._sock.buffer = BytesIO(f.serialize())
# 'Receive' the GOAWAY frame.
# Validate that the spec error name and description are used to throw
# the connection exception.
with pytest.raises(ConnectionError):
c.get_response(s1)
# try to read again after close
with pytest.raises(ConnectionError):
c._single_read()
def build_goaway_frame(self,
last_stream_id,
error_code=0,
additional_data=b''):
"""
Builds a single GOAWAY frame.
"""
f = GoAwayFrame(0)
f.error_code = error_code
f.last_stream_id = last_stream_id
f.additional_data = additional_data
return f
def _terminate_connection(self, error_code):
"""
Terminate the connection early. Used in error handling blocks to send
GOAWAY frames.
"""
f = GoAwayFrame(0)
f.last_stream_id = self.highest_inbound_stream_id
f.error_code = error_code
self.state_machine.process_input(ConnectionInputs.SEND_GOAWAY)
self._prepare_for_sending([f])
# The flow control window manager for the connection.
self._inbound_flow_control_window_manager = WindowManager(
max_window_size=self.local_settings.initial_window_size
)
# When in doubt use dict-dispatch.
self._frame_dispatch_table = {
HeadersFrame: self._receive_headers_frame,
PushPromiseFrame: self._receive_push_promise_frame,
SettingsFrame: self._receive_settings_frame,
DataFrame: self._receive_data_frame,
WindowUpdateFrame: self._receive_window_update_frame,
PingFrame: self._receive_ping_frame,
RstStreamFrame: self._receive_rst_stream_frame,
PriorityFrame: self._receive_priority_frame,
GoAwayFrame: self._receive_goaway_frame,
ContinuationFrame: self._receive_naked_continuation,
AltSvcFrame: self._receive_alt_svc_frame,
ExtensionFrame: self._receive_unknown_frame
}
def _terminate_connection(self, error_code):
"""
Terminate the connection early. Used in error handling blocks to send
GOAWAY frames.
"""
f = GoAwayFrame(0)
f.last_stream_id = self.highest_inbound_stream_id
f.error_code = error_code
self.state_machine.process_input(ConnectionInputs.SEND_GOAWAY)
self._prepare_for_sending([f])