Sans-I/O implementation of a WebSocket server connection. Args: origins: Acceptable values of the ``Origin`` header. Values can be :class:`str` to test for an exact match or regular expressions compiled by :func:`re.compile` to test against a pattern. Includ
| 46 | |
| 47 | |
| 48 | class ServerProtocol(Protocol): |
| 49 | """ |
| 50 | Sans-I/O implementation of a WebSocket server connection. |
| 51 | |
| 52 | Args: |
| 53 | origins: Acceptable values of the ``Origin`` header. Values can be |
| 54 | :class:`str` to test for an exact match or regular expressions |
| 55 | compiled by :func:`re.compile` to test against a pattern. Include |
| 56 | :obj:`None` in the list if the lack of an origin is acceptable. |
| 57 | This is useful for defending against Cross-Site WebSocket |
| 58 | Hijacking attacks. |
| 59 | extensions: List of supported extensions, in order in which they |
| 60 | should be tried. |
| 61 | subprotocols: List of supported subprotocols, in order of decreasing |
| 62 | preference. |
| 63 | select_subprotocol: Callback for selecting a subprotocol among |
| 64 | those supported by the client and the server. It has the same |
| 65 | signature as the :meth:`select_subprotocol` method, including a |
| 66 | :class:`ServerProtocol` instance as first argument. |
| 67 | state: Initial state of the WebSocket connection. |
| 68 | max_size: Maximum size of incoming messages in bytes. |
| 69 | :obj:`None` disables the limit. You may pass a ``(max_message_size, |
| 70 | max_fragment_size)`` tuple to set different limits for messages and |
| 71 | fragments when you expect long messages sent in short fragments. |
| 72 | logger: Logger for this connection; |
| 73 | defaults to ``logging.getLogger("websockets.server")``; |
| 74 | see the :doc:`logging guide <../../topics/logging>` for details. |
| 75 | |
| 76 | """ |
| 77 | |
| 78 | def __init__( |
| 79 | self, |
| 80 | *, |
| 81 | origins: Sequence[Origin | re.Pattern[str] | None] | None = None, |
| 82 | extensions: Sequence[ServerExtensionFactory] | None = None, |
| 83 | subprotocols: Sequence[Subprotocol] | None = None, |
| 84 | select_subprotocol: ( |
| 85 | Callable[ |
| 86 | [ServerProtocol, Sequence[Subprotocol]], |
| 87 | Subprotocol | None, |
| 88 | ] |
| 89 | | None |
| 90 | ) = None, |
| 91 | state: State = CONNECTING, |
| 92 | max_size: int | None | tuple[int | None, int | None] = 2**20, |
| 93 | logger: LoggerLike | None = None, |
| 94 | ) -> None: |
| 95 | super().__init__( |
| 96 | side=SERVER, |
| 97 | state=state, |
| 98 | max_size=max_size, |
| 99 | logger=logger, |
| 100 | ) |
| 101 | self.origins = origins |
| 102 | self.available_extensions = extensions |
| 103 | self.available_subprotocols = subprotocols |
| 104 | if select_subprotocol is not None: |
| 105 | # Bind select_subprotocol then shadow self.select_subprotocol. |
no outgoing calls
searching dependent graphs…