bpo-33654: Support BufferedProtocol in set_protocol() and start_tls() (GH-7130)

In this commit:

* Support BufferedProtocol in set_protocol() and start_tls()
* Fix proactor to cancel readers reliably
* Update tests to be compatible with OpenSSL 1.1.1
* Clarify BufferedProtocol docs
* Bump TLS tests timeouts to 60 seconds; eliminate possible race from start_serving
* Rewrite test_start_tls_server_1
(cherry picked from commit dbf102271f)

Co-authored-by: Yury Selivanov <yury@magic.io>
This commit is contained in:
Miss Islington (bot) 2018-05-28 11:50:45 -07:00 committed by GitHub
parent f8fdb368e3
commit bc3a002e7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 382 additions and 69 deletions

View file

@ -597,8 +597,10 @@ class _SelectorTransport(transports._FlowControlMixin,
self._extra['peername'] = None
self._sock = sock
self._sock_fd = sock.fileno()
self._protocol = protocol
self._protocol_connected = True
self._protocol_connected = False
self.set_protocol(protocol)
self._server = server
self._buffer = self._buffer_factory()
self._conn_lost = 0 # Set when call to connection_lost scheduled.
@ -640,6 +642,7 @@ class _SelectorTransport(transports._FlowControlMixin,
def set_protocol(self, protocol):
self._protocol = protocol
self._protocol_connected = True
def get_protocol(self):
return self._protocol
@ -721,11 +724,7 @@ class _SelectorSocketTransport(_SelectorTransport):
def __init__(self, loop, sock, protocol, waiter=None,
extra=None, server=None):
if protocols._is_buffered_protocol(protocol):
self._read_ready = self._read_ready__get_buffer
else:
self._read_ready = self._read_ready__data_received
self._read_ready_cb = None
super().__init__(loop, sock, protocol, extra, server)
self._eof = False
self._paused = False
@ -745,6 +744,14 @@ class _SelectorSocketTransport(_SelectorTransport):
self._loop.call_soon(futures._set_result_unless_cancelled,
waiter, None)
def set_protocol(self, protocol):
if isinstance(protocol, protocols.BufferedProtocol):
self._read_ready_cb = self._read_ready__get_buffer
else:
self._read_ready_cb = self._read_ready__data_received
super().set_protocol(protocol)
def is_reading(self):
return not self._paused and not self._closing
@ -764,12 +771,17 @@ class _SelectorSocketTransport(_SelectorTransport):
if self._loop.get_debug():
logger.debug("%r resumes reading", self)
def _read_ready(self):
self._read_ready_cb()
def _read_ready__get_buffer(self):
if self._conn_lost:
return
try:
buf = self._protocol.get_buffer()
buf = self._protocol.get_buffer(-1)
if not len(buf):
raise RuntimeError('get_buffer() returned an empty buffer')
except Exception as exc:
self._fatal_error(
exc, 'Fatal error: protocol.get_buffer() call failed.')