(sock: socket.socket, addr: Any)
| 521 | # Define request handler |
| 522 | |
| 523 | def conn_handler(sock: socket.socket, addr: Any) -> None: |
| 524 | # Calculate timeouts on the TLS and WebSocket handshakes. |
| 525 | # The TLS timeout must be set on the socket, then removed |
| 526 | # to avoid conflicting with the WebSocket timeout in handshake(). |
| 527 | deadline = Deadline(open_timeout) |
| 528 | |
| 529 | try: |
| 530 | # Disable Nagle algorithm |
| 531 | |
| 532 | if not unix: |
| 533 | sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) |
| 534 | |
| 535 | # Perform TLS handshake |
| 536 | |
| 537 | if ssl is not None: |
| 538 | sock.settimeout(deadline.timeout()) |
| 539 | # mypy cannot figure this out |
| 540 | assert isinstance(sock, ssl_module.SSLSocket) |
| 541 | sock.do_handshake() |
| 542 | sock.settimeout(None) |
| 543 | |
| 544 | # Create a closure to give select_subprotocol access to connection. |
| 545 | protocol_select_subprotocol: ( |
| 546 | Callable[ |
| 547 | [ServerProtocol, Sequence[Subprotocol]], |
| 548 | Subprotocol | None, |
| 549 | ] |
| 550 | | None |
| 551 | ) = None |
| 552 | if select_subprotocol is not None: |
| 553 | |
| 554 | def protocol_select_subprotocol( |
| 555 | protocol: ServerProtocol, |
| 556 | subprotocols: Sequence[Subprotocol], |
| 557 | ) -> Subprotocol | None: |
| 558 | # mypy doesn't know that select_subprotocol is immutable. |
| 559 | assert select_subprotocol is not None |
| 560 | # Ensure this function is only used in the intended context. |
| 561 | assert protocol is connection.protocol |
| 562 | return select_subprotocol(connection, subprotocols) |
| 563 | |
| 564 | # Initialize WebSocket protocol |
| 565 | |
| 566 | protocol = ServerProtocol( |
| 567 | origins=origins, |
| 568 | extensions=extensions, |
| 569 | subprotocols=subprotocols, |
| 570 | select_subprotocol=protocol_select_subprotocol, |
| 571 | max_size=max_size, |
| 572 | logger=logger, |
| 573 | ) |
| 574 | |
| 575 | # Initialize WebSocket connection |
| 576 | |
| 577 | assert create_connection is not None # help mypy |
| 578 | connection = create_connection( |
| 579 | sock, |
| 580 | protocol, |
nothing calls this directly
no test coverage detected
searching dependent graphs…