(
# TODO: The return type is a specific combination of subclasses of
# asyncio.protocols.Protocol that we can't express. For now, having the
# return type be dependent on signature of the factory is an improvement
protocol_factory: Callable[[], _ProctolFactoryR],
host: str,
port: int,
*,
loop: asyncio.AbstractEventLoop,
ssl_context: ssl_module.SSLContext,
ssl_is_advisory: bool = False,
)
| 954 | |
| 955 | |
| 956 | async def _create_ssl_connection( |
| 957 | # TODO: The return type is a specific combination of subclasses of |
| 958 | # asyncio.protocols.Protocol that we can't express. For now, having the |
| 959 | # return type be dependent on signature of the factory is an improvement |
| 960 | protocol_factory: Callable[[], _ProctolFactoryR], |
| 961 | host: str, |
| 962 | port: int, |
| 963 | *, |
| 964 | loop: asyncio.AbstractEventLoop, |
| 965 | ssl_context: ssl_module.SSLContext, |
| 966 | ssl_is_advisory: bool = False, |
| 967 | ) -> typing.Tuple[asyncio.Transport, _ProctolFactoryR]: |
| 968 | |
| 969 | tr, pr = await loop.create_connection( |
| 970 | lambda: TLSUpgradeProto(loop, host, port, |
| 971 | ssl_context, ssl_is_advisory), |
| 972 | host, port) |
| 973 | |
| 974 | tr.write(struct.pack('!ll', 8, 80877103)) # SSLRequest message. |
| 975 | |
| 976 | try: |
| 977 | do_ssl_upgrade = await pr.on_data |
| 978 | except (Exception, asyncio.CancelledError): |
| 979 | tr.close() |
| 980 | raise |
| 981 | |
| 982 | if hasattr(loop, 'start_tls'): |
| 983 | if do_ssl_upgrade: |
| 984 | try: |
| 985 | new_tr = await loop.start_tls( |
| 986 | tr, pr, ssl_context, server_hostname=host) |
| 987 | assert new_tr is not None |
| 988 | except (Exception, asyncio.CancelledError): |
| 989 | tr.close() |
| 990 | raise |
| 991 | else: |
| 992 | new_tr = tr |
| 993 | |
| 994 | pg_proto = protocol_factory() |
| 995 | pg_proto.is_ssl = do_ssl_upgrade |
| 996 | pg_proto.connection_made(new_tr) |
| 997 | new_tr.set_protocol(pg_proto) |
| 998 | |
| 999 | return new_tr, pg_proto |
| 1000 | else: |
| 1001 | conn_factory = functools.partial( |
| 1002 | loop.create_connection, protocol_factory) |
| 1003 | |
| 1004 | if do_ssl_upgrade: |
| 1005 | conn_factory = functools.partial( |
| 1006 | conn_factory, ssl=ssl_context, server_hostname=host) |
| 1007 | |
| 1008 | sock = _get_socket(tr) |
| 1009 | sock = sock.dup() |
| 1010 | _set_nodelay(sock) |
| 1011 | tr.close() |
| 1012 | |
| 1013 | try: |
no test coverage detected
searching dependent graphs…