server.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. from __future__ import annotations
  2. import functools
  3. import http
  4. import logging
  5. import re
  6. import ssl as ssl_module
  7. from collections.abc import Awaitable, Mapping, Sequence
  8. from types import TracebackType
  9. from typing import Any, Callable, Coroutine, Self
  10. import trio
  11. import trio.abc
  12. from ..asyncio.server import basic_auth
  13. from ..extensions.base import ServerExtensionFactory
  14. from ..extensions.permessage_deflate import enable_server_permessage_deflate
  15. from ..frames import CloseCode
  16. from ..headers import validate_subprotocols
  17. from ..http11 import SERVER, Request, Response
  18. from ..protocol import CONNECTING, OPEN, Event
  19. from ..server import ServerProtocol
  20. from ..typing import LoggerLike, Origin, StatusLike, Subprotocol
  21. from ..utils import get_socket_name
  22. from .connection import Connection, broadcast
  23. from .utils import race_events
  24. __all__ = [
  25. "broadcast",
  26. "serve",
  27. "ServerConnection",
  28. "Server",
  29. "basic_auth",
  30. ]
  31. class ServerConnection(Connection):
  32. """
  33. :mod:`trio` implementation of a WebSocket server connection.
  34. :class:`ServerConnection` provides :meth:`recv` and :meth:`send` methods for
  35. receiving and sending messages.
  36. It supports asynchronous iteration to receive messages::
  37. async for message in websocket:
  38. await process(message)
  39. The iterator exits normally when the connection is closed with close code
  40. 1000 (OK) or 1001 (going away) or without a close code. It raises a
  41. :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
  42. closed with any other code.
  43. The ``ping_interval``, ``ping_timeout``, ``close_timeout``, and
  44. ``max_queue`` arguments have the same meaning as in :func:`serve`.
  45. Args:
  46. nursery: Trio nursery.
  47. stream: Trio stream connected to a WebSocket client.
  48. protocol: Sans-I/O connection.
  49. server: Server that manages this connection.
  50. """
  51. def __init__(
  52. self,
  53. nursery: trio.Nursery,
  54. stream: trio.abc.Stream,
  55. protocol: ServerProtocol,
  56. server: Server,
  57. *,
  58. ping_interval: float | None = 20,
  59. ping_timeout: float | None = 20,
  60. close_timeout: float | None = 10,
  61. max_queue: int | None | tuple[int | None, int | None] = 16,
  62. ) -> None:
  63. self.protocol: ServerProtocol
  64. super().__init__(
  65. nursery,
  66. stream,
  67. protocol,
  68. ping_interval=ping_interval,
  69. ping_timeout=ping_timeout,
  70. close_timeout=close_timeout,
  71. max_queue=max_queue,
  72. )
  73. self.server = server
  74. self.request_rcvd: trio.Event = trio.Event()
  75. self.username: str # see basic_auth()
  76. self.handler: Callable[[ServerConnection], Awaitable[None]] # see route()
  77. self.handler_kwargs: Mapping[str, Any] # see route()
  78. def respond(self, status: StatusLike, text: str) -> Response:
  79. """
  80. Create a plain text HTTP response.
  81. ``process_request`` and ``process_response`` may call this method to
  82. return an HTTP response instead of performing the WebSocket opening
  83. handshake.
  84. You can modify the response before returning it, for example by changing
  85. HTTP headers.
  86. Args:
  87. status: HTTP status code.
  88. text: HTTP response body; it will be encoded to UTF-8.
  89. Returns:
  90. HTTP response to send to the client.
  91. """
  92. return self.protocol.reject(status, text)
  93. async def handshake(
  94. self,
  95. process_request: (
  96. Callable[
  97. [ServerConnection, Request],
  98. Awaitable[Response | None] | Response | None,
  99. ]
  100. | None
  101. ) = None,
  102. process_response: (
  103. Callable[
  104. [ServerConnection, Request, Response],
  105. Awaitable[Response | None] | Response | None,
  106. ]
  107. | None
  108. ) = None,
  109. server_header: str | None = SERVER,
  110. ) -> None:
  111. """
  112. Perform the opening handshake.
  113. """
  114. await race_events(self.request_rcvd, self.stream_closed)
  115. if self.request is not None:
  116. response = None
  117. if process_request is not None:
  118. try:
  119. response = process_request(self, self.request)
  120. if isinstance(response, Awaitable):
  121. response = await response
  122. except Exception as exc:
  123. self.protocol.handshake_exc = exc
  124. self.logger.error("process_request failed", exc_info=True)
  125. response = self.protocol.reject(
  126. http.HTTPStatus.INTERNAL_SERVER_ERROR,
  127. (
  128. "Failed to open a WebSocket connection.\n"
  129. "See server log for more information.\n"
  130. ),
  131. )
  132. if response is None:
  133. self.response = self.protocol.accept(self.request)
  134. else:
  135. assert isinstance(response, Response) # help mypy
  136. self.response = response
  137. if server_header is not None:
  138. self.response.headers["Server"] = server_header
  139. response = None
  140. if process_response is not None:
  141. try:
  142. response = process_response(self, self.request, self.response)
  143. if isinstance(response, Awaitable):
  144. response = await response
  145. except Exception as exc:
  146. self.protocol.handshake_exc = exc
  147. self.logger.error("process_response failed", exc_info=True)
  148. response = self.protocol.reject(
  149. http.HTTPStatus.INTERNAL_SERVER_ERROR,
  150. (
  151. "Failed to open a WebSocket connection.\n"
  152. "See server log for more information.\n"
  153. ),
  154. )
  155. if response is not None:
  156. assert isinstance(response, Response) # help mypy
  157. self.response = response
  158. # Reject the connection if the server started closing during the
  159. # opening handshake. Don't yield before send_response() to avoid
  160. # a race condition after checking if the server is closing.
  161. if (
  162. self.response.status_code == http.HTTPStatus.SWITCHING_PROTOCOLS
  163. and self.server.closing
  164. ):
  165. self.response = self.protocol.reject(
  166. http.HTTPStatus.SERVICE_UNAVAILABLE,
  167. "Server is shutting down.\n",
  168. )
  169. # Don't respond if the connection was closed during the handshake.
  170. if self.state is CONNECTING:
  171. async with self.send_context(expected_state=CONNECTING):
  172. self.protocol.send_response(self.response)
  173. def process_event(self, event: Event) -> None:
  174. """
  175. Process one incoming event.
  176. """
  177. # First event - handshake request.
  178. if self.request is None:
  179. assert isinstance(event, Request)
  180. self.request = event
  181. self.request_rcvd.set()
  182. # Later events - frames.
  183. else:
  184. super().process_event(event)
  185. class Server(trio.abc.AsyncResource):
  186. """
  187. WebSocket server returned by :func:`serve`.
  188. Args:
  189. listeners: List of Trio listeners accepting new connections.
  190. handler: Handler for one connection. It receives a Trio stream.
  191. logger: Logger for this server.
  192. It defaults to ``logging.getLogger("websockets.server")``.
  193. See the :doc:`logging guide <../../topics/logging>` for details.
  194. """
  195. def __init__(
  196. self,
  197. listeners: list[trio.SocketListener],
  198. handler: Callable[[trio.abc.Stream], Coroutine[Any, Any, None]],
  199. logger: LoggerLike | None = None,
  200. ) -> None:
  201. self.listeners = listeners
  202. self.handler = handler
  203. if logger is None:
  204. logger = logging.getLogger("websockets.server")
  205. self.logger = logger
  206. # Keep track of active connections.
  207. # Trio keeps track of connection handler tasks.
  208. self.all_connections: set[ServerConnection] = set()
  209. # Completed when all handlers are done.
  210. self.handlers_waiter = trio.Event()
  211. self.closing = False
  212. @property
  213. def connections(self) -> set[ServerConnection]:
  214. """
  215. Set of active connections.
  216. This property contains all connections that completed the opening
  217. handshake successfully and didn't start the closing handshake yet.
  218. It can be useful in combination with :func:`~broadcast`.
  219. """
  220. return {
  221. connection
  222. for connection in self.all_connections
  223. if connection.protocol.state is OPEN
  224. }
  225. async def serve_forever(
  226. self,
  227. task_status: trio.TaskStatus[Server] = trio.TASK_STATUS_IGNORED,
  228. ) -> None:
  229. # Running handlers in a dedicated nursery makes it possible to close
  230. # listeners while handlers finish running. The nursery for listeners
  231. # is created in trio.serve_listeners().
  232. async with trio.open_nursery() as self.handler_nursery:
  233. # Wrap trio.serve_listeners() in another nursery to return the
  234. # Server object in task_status instead of a list of listeners.
  235. async with trio.open_nursery() as self.serve_nursery:
  236. await self.serve_nursery.start(
  237. functools.partial(
  238. trio.serve_listeners,
  239. self.handler,
  240. self.listeners,
  241. handler_nursery=self.handler_nursery,
  242. )
  243. )
  244. for listener in self.listeners:
  245. self.logger.info(
  246. "server listening on %s",
  247. # listener.socket is a Trio socket, not a socket.socket,
  248. # but it offers the same APIs used by get_socket_name().
  249. get_socket_name(listener.socket), # type: ignore
  250. )
  251. task_status.started(self)
  252. # When the nursery for handlers has exited, all handlers have returned.
  253. self.handlers_waiter.set()
  254. # Shutting down the server cleanly when serve_forever() is canceled would be
  255. # the most idiomatic in Trio. However, that would require shielding too many
  256. # asynchronous operations, including the TLS & WebSocket opening handshakes.
  257. async def aclose(
  258. self,
  259. close_connections: bool = True,
  260. code: CloseCode | int = CloseCode.GOING_AWAY,
  261. reason: str = "",
  262. ) -> None:
  263. """
  264. Close the server.
  265. * Close the TCP listeners.
  266. * When ``close_connections`` is :obj:`True`, which is the default,
  267. close existing connections. Specifically:
  268. * Reject opening WebSocket connections with an HTTP 503 (service
  269. unavailable) error. This happens when the server accepted the TCP
  270. connection but didn't complete the opening handshake before closing.
  271. * Close open WebSocket connections with close code 1001 (going away).
  272. ``code`` and ``reason`` can be customized, for example to use code
  273. 1012 (service restart).
  274. * Wait until all connection handlers have returned.
  275. :meth:`aclose` is idempotent.
  276. """
  277. self.logger.info("server closing")
  278. # Stop accepting new connections.
  279. self.serve_nursery.cancel_scope.cancel()
  280. # Reject OPENING connections with HTTP 503 — see handshake().
  281. self.closing = True
  282. # Close OPEN connections.
  283. if close_connections:
  284. for connection in self.all_connections:
  285. if connection.protocol.state is OPEN: # pragma: no branch
  286. self.handler_nursery.start_soon(connection.aclose, code, reason)
  287. # Wait until all connection handlers have returned.
  288. await self.handlers_waiter.wait()
  289. self.logger.info("server closed")
  290. async def __aenter__(self) -> Self:
  291. return self
  292. async def __aexit__(
  293. self,
  294. exc_type: type[BaseException] | None,
  295. exc_value: BaseException | None,
  296. traceback: TracebackType | None,
  297. ) -> None:
  298. await self.aclose()
  299. async def serve(
  300. handler: Callable[[ServerConnection], Awaitable[None]],
  301. port: int | None = None,
  302. *,
  303. # TCP/TLS
  304. host: str | bytes | None = None,
  305. backlog: int | None = None,
  306. listeners: list[trio.SocketListener] | None = None,
  307. ssl: ssl_module.SSLContext | None = None,
  308. # WebSocket
  309. origins: Sequence[Origin | re.Pattern[str] | None] | None = None,
  310. extensions: Sequence[ServerExtensionFactory] | None = None,
  311. subprotocols: Sequence[Subprotocol] | None = None,
  312. select_subprotocol: (
  313. Callable[
  314. [ServerConnection, Sequence[Subprotocol]],
  315. Subprotocol | None,
  316. ]
  317. | None
  318. ) = None,
  319. compression: str | None = "deflate",
  320. # HTTP
  321. process_request: (
  322. Callable[
  323. [ServerConnection, Request],
  324. Awaitable[Response | None] | Response | None,
  325. ]
  326. | None
  327. ) = None,
  328. process_response: (
  329. Callable[
  330. [ServerConnection, Request, Response],
  331. Awaitable[Response | None] | Response | None,
  332. ]
  333. | None
  334. ) = None,
  335. server_header: str | None = SERVER,
  336. # Timeouts
  337. open_timeout: float | None = 10,
  338. ping_interval: float | None = 20,
  339. ping_timeout: float | None = 20,
  340. close_timeout: float | None = 10,
  341. # Limits
  342. max_size: int | None | tuple[int | None, int | None] = 2**20,
  343. max_queue: int | None | tuple[int | None, int | None] = 16,
  344. # Logging
  345. logger: LoggerLike | None = None,
  346. # Escape hatch for advanced customization
  347. create_connection: type[ServerConnection] | None = None,
  348. # Compatibility with trio.Nursery.start()
  349. task_status: trio.TaskStatus[Server] = trio.TASK_STATUS_IGNORED,
  350. ) -> None:
  351. """
  352. Create a WebSocket server listening on ``port``.
  353. Whenever a client connects, the server creates a :class:`ServerConnection`,
  354. performs the opening handshake, and delegates to the ``handler`` coroutine.
  355. The handler receives the :class:`ServerConnection` instance, which you can
  356. use to send and receive messages.
  357. Once the handler completes, either normally or with an exception, the server
  358. performs the closing handshake and closes the connection.
  359. When using :func:`serve` with :meth:`nursery.start <trio.Nursery.start>`,
  360. you get back a :class:`Server` object. Treat it as an asynchronous context
  361. manager to ensure that the server will be closed gracefully::
  362. from websockets.trio.server import serve
  363. async def handler(websocket):
  364. ...
  365. # set this event to exit the server
  366. stop = trio.Event()
  367. with trio.open_nursery() as nursery:
  368. server = await nursery.start(serve, handler, port)
  369. async with server:
  370. await stop.wait()
  371. Alternatively, to stop the server gracefully, call its
  372. :meth:`~Server.aclose` method::
  373. with trio.open_nursery() as nursery:
  374. server = await nursery.start(serve, handler, port)
  375. try:
  376. await stop.wait()
  377. finally:
  378. await server.aclose()
  379. Args:
  380. handler: Connection handler. It receives the WebSocket connection,
  381. which is a :class:`ServerConnection`, in argument.
  382. port: TCP port the server listens on.
  383. See :func:`~trio.open_tcp_listeners` for details.
  384. host: Network interfaces the server binds to.
  385. See :func:`~trio.open_tcp_listeners` for details.
  386. backlog: Listen backlog. See :func:`~trio.open_tcp_listeners` for
  387. details.
  388. listeners: Preexisting TCP listeners. ``listeners`` replaces ``port``,
  389. ``host``, and ``backlog``. See :func:`trio.serve_listeners` for
  390. details.
  391. ssl: Configuration for enabling TLS on the connection.
  392. origins: Acceptable values of the ``Origin`` header, for defending
  393. against Cross-Site WebSocket Hijacking attacks. Values can be
  394. :class:`str` to test for an exact match or regular expressions
  395. compiled by :func:`re.compile` to test against a pattern. Include
  396. :obj:`None` in the list if the lack of an origin is acceptable.
  397. extensions: List of supported extensions, in order in which they
  398. should be negotiated and run.
  399. subprotocols: List of supported subprotocols, in order of decreasing
  400. preference.
  401. select_subprotocol: Callback for selecting a subprotocol among
  402. those supported by the client and the server. It receives a
  403. :class:`ServerConnection` (not a
  404. :class:`~websockets.server.ServerProtocol`!) instance and a list of
  405. subprotocols offered by the client. Other than the first argument,
  406. it has the same behavior as the
  407. :meth:`ServerProtocol.select_subprotocol
  408. <websockets.server.ServerProtocol.select_subprotocol>` method.
  409. compression: The "permessage-deflate" extension is enabled by default.
  410. Set ``compression`` to :obj:`None` to disable it. See the
  411. :doc:`compression guide <../../topics/compression>` for details.
  412. process_request: Intercept the request during the opening handshake.
  413. Return an HTTP response to force the response or :obj:`None` to
  414. continue normally. When you force an HTTP 101 Continue response, the
  415. handshake is successful. Else, the connection is aborted.
  416. ``process_request`` may be a function or a coroutine.
  417. process_response: Intercept the response during the opening handshake.
  418. Return an HTTP response to force the response or :obj:`None` to
  419. continue normally. When you force an HTTP 101 Continue response, the
  420. handshake is successful. Else, the connection is aborted.
  421. ``process_response`` may be a function or a coroutine.
  422. server_header: Value of the ``Server`` response header.
  423. It defaults to ``"Python/x.y.z websockets/X.Y"``. Setting it to
  424. :obj:`None` removes the header.
  425. open_timeout: Timeout for opening connections in seconds.
  426. :obj:`None` disables the timeout.
  427. ping_interval: Interval between keepalive pings in seconds.
  428. :obj:`None` disables keepalive.
  429. ping_timeout: Timeout for keepalive pings in seconds.
  430. :obj:`None` disables timeouts.
  431. close_timeout: Timeout for closing connections in seconds.
  432. :obj:`None` disables the timeout.
  433. max_size: Maximum size of incoming messages in bytes.
  434. :obj:`None` disables the limit. You may pass a ``(max_message_size,
  435. max_fragment_size)`` tuple to set different limits for messages and
  436. fragments when you expect long messages sent in short fragments.
  437. max_queue: High-water mark of the buffer where frames are received.
  438. It defaults to 16 frames. The low-water mark defaults to ``max_queue
  439. // 4``. You may pass a ``(high, low)`` tuple to set the high-water
  440. and low-water marks. If you want to disable flow control entirely,
  441. you may set it to ``None``, although that's a bad idea.
  442. logger: Logger for this server.
  443. It defaults to ``logging.getLogger("websockets.server")``.
  444. See the :doc:`logging guide <../../topics/logging>` for details.
  445. create_connection: Factory for the :class:`ServerConnection` managing
  446. the connection. Set it to a wrapper or a subclass to customize
  447. connection handling.
  448. task_status: For compatibility with :meth:`nursery.start
  449. <trio.Nursery.start>`.
  450. """
  451. # Process parameters
  452. if subprotocols is not None:
  453. validate_subprotocols(subprotocols)
  454. if compression == "deflate":
  455. extensions = enable_server_permessage_deflate(extensions)
  456. elif compression is not None:
  457. raise ValueError(f"unsupported compression: {compression}")
  458. if create_connection is None:
  459. create_connection = ServerConnection
  460. # Create listeners
  461. if listeners is None:
  462. if port is None:
  463. raise ValueError("port is required when listeners is not provided")
  464. listeners = await trio.open_tcp_listeners(port, host=host, backlog=backlog)
  465. else:
  466. if port is not None:
  467. raise ValueError("port is incompatible with listeners")
  468. if host is not None:
  469. raise ValueError("host is incompatible with listeners")
  470. if backlog is not None:
  471. raise ValueError("backlog is incompatible with listeners")
  472. async def stream_handler(stream: trio.abc.Stream) -> None:
  473. """
  474. Handle the lifecycle of a WebSocket connection.
  475. Since this coroutine doesn't have a caller that can handle
  476. exceptions, it attempts to log relevant ones.
  477. It guarantees that the TCP connection is closed before exiting.
  478. """
  479. async with trio.open_nursery() as nursery:
  480. try:
  481. # Apply open_timeout to the TLS and WebSocket handshake.
  482. with (
  483. trio.CancelScope()
  484. if open_timeout is None
  485. else trio.fail_after(open_timeout)
  486. ):
  487. # Enable TLS.
  488. if ssl is not None:
  489. # Wrap with SSLStream here rather than with TLSListener
  490. # in order to include the TLS handshake within open_timeout.
  491. stream = trio.SSLStream(
  492. stream,
  493. ssl,
  494. server_side=True,
  495. https_compatible=True,
  496. )
  497. assert isinstance(stream, trio.SSLStream) # help mypy
  498. try:
  499. await stream.do_handshake()
  500. except trio.BrokenResourceError:
  501. return
  502. # Create a closure to give select_subprotocol access to connection.
  503. protocol_select_subprotocol: (
  504. Callable[
  505. [ServerProtocol, Sequence[Subprotocol]],
  506. Subprotocol | None,
  507. ]
  508. | None
  509. ) = None
  510. if select_subprotocol is not None:
  511. def protocol_select_subprotocol(
  512. protocol: ServerProtocol,
  513. subprotocols: Sequence[Subprotocol],
  514. ) -> Subprotocol | None:
  515. # mypy doesn't know that select_subprotocol is immutable.
  516. assert select_subprotocol is not None
  517. # Ensure this function is only used in the intended context.
  518. assert protocol is connection.protocol
  519. return select_subprotocol(connection, subprotocols)
  520. # Initialize WebSocket protocol.
  521. protocol = ServerProtocol(
  522. origins=origins,
  523. extensions=extensions,
  524. subprotocols=subprotocols,
  525. select_subprotocol=protocol_select_subprotocol,
  526. max_size=max_size,
  527. logger=logger,
  528. )
  529. # Initialize WebSocket connection.
  530. connection = create_connection(
  531. nursery,
  532. stream,
  533. protocol,
  534. server,
  535. ping_interval=ping_interval,
  536. ping_timeout=ping_timeout,
  537. close_timeout=close_timeout,
  538. max_queue=max_queue,
  539. )
  540. await connection.handshake(
  541. process_request,
  542. process_response,
  543. server_header,
  544. )
  545. if connection.protocol.state is not OPEN:
  546. await connection.close_stream()
  547. return
  548. server.all_connections.add(connection)
  549. connection.start_keepalive()
  550. try:
  551. await handler(connection)
  552. except Exception:
  553. connection.logger.error("connection handler failed", exc_info=True)
  554. await connection.aclose(CloseCode.INTERNAL_ERROR)
  555. else:
  556. await connection.aclose()
  557. finally:
  558. server.all_connections.discard(connection)
  559. except Exception:
  560. # Don't leak connections when the opening handshake times out or
  561. # an unexpected error occurs.
  562. await trio.aclose_forcefully(stream)
  563. # The server variable is captured by the closure of conn_handler().
  564. server = Server(listeners, stream_handler, logger)
  565. await server.serve_forever(task_status=task_status)