MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / _create_ssl_connection

Function _create_ssl_connection

asyncpg/connect_utils.py:982–1055  ·  view source on GitHub ↗
(
    # 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,
    retry: bool = False,
)

Source from the content-addressed store, hash-verified

980
981
982async def _create_ssl_connection(
983 # TODO: The return type is a specific combination of subclasses of
984 # asyncio.protocols.Protocol that we can't express. For now, having the
985 # return type be dependent on signature of the factory is an improvement
986 protocol_factory: Callable[[], _ProctolFactoryR],
987 host: str,
988 port: int,
989 *,
990 loop: asyncio.AbstractEventLoop,
991 ssl_context: ssl_module.SSLContext,
992 ssl_is_advisory: bool = False,
993 retry: bool = False,
994) -> typing.Tuple[asyncio.Transport, _ProctolFactoryR]:
995
996 tr, pr = await loop.create_connection(
997 lambda: TLSUpgradeProto(loop, host, port,
998 ssl_context, ssl_is_advisory),
999 host, port)
1000
1001 tr.write(struct.pack('!ll', 8, _SSL_REQUEST_CODE)) # SSLRequest message.
1002
1003 try:
1004 do_ssl_upgrade = await pr.on_data
1005 except (Exception, asyncio.CancelledError):
1006 tr.close()
1007 raise
1008
1009 if hasattr(loop, 'start_tls'):
1010 if do_ssl_upgrade:
1011 try:
1012 new_tr = await loop.start_tls(
1013 tr, pr, ssl_context, server_hostname=host)
1014 if new_tr is None:
1015 raise ConnectionError('connection closed during TLS '
1016 'handshake')
1017 except (Exception, asyncio.CancelledError) as exc:
1018 tr.close()
1019 if retry and isinstance(exc, OSError) and not isinstance(
1020 exc, asyncio.TimeoutError
1021 ):
1022 raise _RetryConnectSignal() from exc
1023 raise
1024 else:
1025 new_tr = tr
1026
1027 pg_proto = protocol_factory()
1028 pg_proto.connection_made(new_tr)
1029 pg_proto.is_ssl = do_ssl_upgrade
1030 new_tr.set_protocol(pg_proto)
1031
1032 return new_tr, pg_proto
1033 else:
1034 conn_factory = functools.partial(
1035 loop.create_connection, protocol_factory)
1036
1037 if do_ssl_upgrade:
1038 conn_factory = functools.partial(
1039 conn_factory, ssl=ssl_context, server_hostname=host)

Callers 2

__connect_addrFunction · 0.85
_cancelFunction · 0.85

Calls 8

TLSUpgradeProtoClass · 0.85
_RetryConnectSignalClass · 0.85
_get_socketFunction · 0.85
_set_nodelayFunction · 0.85
writeMethod · 0.80
start_tlsMethod · 0.80
connection_madeMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected