1# Portions of this file are derived from CPython's asyncio sources
2# (notably asyncio.base_events and asyncio.selector_events).
3# Copyright (c) Python Software Foundation.
4# Licensed under the Python Software Foundation License Version 2.
5# See LICENSES/PSF-2.0.txt and THIRD_PARTY_NOTICES for details.
6
7import socket
8
9from .api_utils import _check_non_ssl_socket, _create_connection_transport, _logger, _validate_bio_size, _validate_ssl_timeout
10
11
12async def connect_accepted_socket(
13 loop,
14 protocol_factory,
15 sock,
16 *,
17 ssl=None,
18 ssl_handshake_timeout=None,
19 ssl_shutdown_timeout=None,
20 ssl_incoming_bio_size=None,
21 ssl_outgoing_bio_size=None,
22):
23 if sock.type != socket.SOCK_STREAM:
24 raise ValueError(f"A Stream Socket was expected, got {sock!r}")
25
26 ssl_handshake_timeout = _validate_ssl_timeout("ssl_handshake_timeout", ssl_handshake_timeout, ssl)
27 ssl_shutdown_timeout = _validate_ssl_timeout("ssl_shutdown_timeout", ssl_shutdown_timeout, ssl)
28 ssl_incoming_bio_size = _validate_bio_size("ssl_incoming_bio_size", ssl_incoming_bio_size, ssl)
29 ssl_outgoing_bio_size = _validate_bio_size("ssl_outgoing_bio_size", ssl_outgoing_bio_size, ssl)
30
31 _check_non_ssl_socket(sock)
32
33 transport, protocol = await _create_connection_transport(
34 loop, sock, protocol_factory, ssl, "",
35 server_side=True,
36 ssl_handshake_timeout=ssl_handshake_timeout,
37 ssl_shutdown_timeout=ssl_shutdown_timeout,
38 ssl_incoming_bio_size=ssl_incoming_bio_size,
39 ssl_outgoing_bio_size=ssl_outgoing_bio_size,
40 )
41 if loop.get_debug():
42 # Get the socket from the transport because SSL transport closes
43 # the old socket and creates a new SSL socket
44 sock = transport.get_extra_info("socket")
45 _logger.debug("%r handled: (%r, %r)", sock, transport, protocol)
46 return transport, protocol