server.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. from __future__ import annotations
  2. import asyncio
  3. import email.utils
  4. import functools
  5. import http
  6. import inspect
  7. import logging
  8. import socket
  9. import warnings
  10. from collections.abc import Awaitable, Generator, Iterable, Sequence
  11. from types import TracebackType
  12. from typing import Any, Callable, Self, cast
  13. from ..datastructures import Headers, HeadersLike, MultipleValuesError
  14. from ..exceptions import (
  15. InvalidHandshake,
  16. InvalidHeader,
  17. InvalidMessage,
  18. InvalidOrigin,
  19. InvalidUpgrade,
  20. NegotiationError,
  21. )
  22. from ..extensions import Extension, ServerExtensionFactory
  23. from ..extensions.permessage_deflate import enable_server_permessage_deflate
  24. from ..headers import (
  25. build_extension,
  26. parse_extension,
  27. parse_subprotocol,
  28. validate_subprotocols,
  29. )
  30. from ..http11 import SERVER
  31. from ..protocol import State
  32. from ..typing import ExtensionHeader, LoggerLike, Origin, StatusLike, Subprotocol
  33. from .exceptions import AbortHandshake
  34. from .handshake import build_response, check_request
  35. from .http import read_request
  36. from .protocol import WebSocketCommonProtocol, broadcast
  37. __all__ = [
  38. "broadcast",
  39. "serve",
  40. "unix_serve",
  41. "WebSocketServerProtocol",
  42. "WebSocketServer",
  43. ]
  44. HeadersLikeOrCallable = HeadersLike | Callable[[str, Headers], HeadersLike]
  45. HTTPResponse = tuple[StatusLike, HeadersLike, bytes]
  46. class WebSocketServerProtocol(WebSocketCommonProtocol):
  47. """
  48. WebSocket server connection.
  49. :class:`WebSocketServerProtocol` provides :meth:`recv` and :meth:`send`
  50. coroutines for receiving and sending messages.
  51. It supports asynchronous iteration to receive messages::
  52. async for message in websocket:
  53. await process(message)
  54. The iterator exits normally when the connection is closed with close code
  55. 1000 (OK) or 1001 (going away) or without a close code. It raises
  56. a :exc:`~websockets.exceptions.ConnectionClosedError` when the connection
  57. is closed with any other code.
  58. You may customize the opening handshake in a subclass by
  59. overriding :meth:`process_request` or :meth:`select_subprotocol`.
  60. Args:
  61. ws_server: WebSocket server that created this connection.
  62. See :func:`serve` for the documentation of ``ws_handler``, ``logger``, ``origins``,
  63. ``extensions``, ``subprotocols``, ``extra_headers``, and ``server_header``.
  64. See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the
  65. documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``,
  66. ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``.
  67. """
  68. is_client = False
  69. side = "server"
  70. def __init__(
  71. self,
  72. # The version that accepts the path in the second argument is deprecated.
  73. ws_handler: (
  74. Callable[[WebSocketServerProtocol], Awaitable[Any]]
  75. | Callable[[WebSocketServerProtocol, str], Awaitable[Any]]
  76. ),
  77. ws_server: WebSocketServer,
  78. *,
  79. logger: LoggerLike | None = None,
  80. origins: Sequence[Origin | None] | None = None,
  81. extensions: Sequence[ServerExtensionFactory] | None = None,
  82. subprotocols: Sequence[Subprotocol] | None = None,
  83. extra_headers: HeadersLikeOrCallable | None = None,
  84. server_header: str | None = SERVER,
  85. process_request: (
  86. Callable[[str, Headers], Awaitable[HTTPResponse | None]] | None
  87. ) = None,
  88. select_subprotocol: (
  89. Callable[[Sequence[Subprotocol], Sequence[Subprotocol]], Subprotocol] | None
  90. ) = None,
  91. open_timeout: float | None = 10,
  92. **kwargs: Any,
  93. ) -> None:
  94. if logger is None:
  95. logger = logging.getLogger("websockets.server")
  96. super().__init__(logger=logger, **kwargs)
  97. # For backwards compatibility with 6.0 or earlier.
  98. if origins is not None and "" in origins:
  99. warnings.warn("use None instead of '' in origins", DeprecationWarning)
  100. origins = [None if origin == "" else origin for origin in origins]
  101. # For backwards compatibility with 10.0 or earlier. Done here in
  102. # addition to serve to trigger the deprecation warning on direct
  103. # use of WebSocketServerProtocol.
  104. self.ws_handler = remove_path_argument(ws_handler)
  105. self.ws_server = ws_server
  106. self.origins = origins
  107. self.available_extensions = extensions
  108. self.available_subprotocols = subprotocols
  109. self.extra_headers = extra_headers
  110. self.server_header = server_header
  111. self._process_request = process_request
  112. self._select_subprotocol = select_subprotocol
  113. self.open_timeout = open_timeout
  114. def connection_made(self, transport: asyncio.BaseTransport) -> None:
  115. """
  116. Register connection and initialize a task to handle it.
  117. """
  118. super().connection_made(transport)
  119. # Register the connection with the server before creating the handler
  120. # task. Registering at the beginning of the handler coroutine would
  121. # create a race condition between the creation of the task, which
  122. # schedules its execution, and the moment the handler starts running.
  123. self.ws_server.register(self)
  124. self.handler_task = self.loop.create_task(self.handler())
  125. async def handler(self) -> None:
  126. """
  127. Handle the lifecycle of a WebSocket connection.
  128. Since this method doesn't have a caller able to handle exceptions, it
  129. attempts to log relevant ones and guarantees that the TCP connection is
  130. closed before exiting.
  131. """
  132. try:
  133. try:
  134. async with asyncio.timeout(self.open_timeout):
  135. await self.handshake(
  136. origins=self.origins,
  137. available_extensions=self.available_extensions,
  138. available_subprotocols=self.available_subprotocols,
  139. extra_headers=self.extra_headers,
  140. )
  141. except asyncio.TimeoutError: # pragma: no cover
  142. raise
  143. except ConnectionError:
  144. raise
  145. except Exception as exc:
  146. if isinstance(exc, AbortHandshake):
  147. status, headers, body = exc.status, exc.headers, exc.body
  148. elif isinstance(exc, InvalidOrigin):
  149. if self.debug:
  150. self.logger.debug("! invalid origin", exc_info=True)
  151. status, headers, body = (
  152. http.HTTPStatus.FORBIDDEN,
  153. Headers(),
  154. f"Failed to open a WebSocket connection: {exc}.\n".encode(),
  155. )
  156. elif isinstance(exc, InvalidUpgrade):
  157. if self.debug:
  158. self.logger.debug("! invalid upgrade", exc_info=True)
  159. status, headers, body = (
  160. http.HTTPStatus.UPGRADE_REQUIRED,
  161. Headers([("Upgrade", "websocket")]),
  162. (
  163. f"Failed to open a WebSocket connection: {exc}.\n"
  164. f"\n"
  165. f"You cannot access a WebSocket server directly "
  166. f"with a browser. You need a WebSocket client.\n"
  167. ).encode(),
  168. )
  169. elif isinstance(exc, InvalidHandshake):
  170. if self.debug:
  171. self.logger.debug("! invalid handshake", exc_info=True)
  172. exc_chain = cast(BaseException, exc)
  173. exc_str = f"{exc_chain}"
  174. while exc_chain.__cause__ is not None:
  175. exc_chain = exc_chain.__cause__
  176. exc_str += f"; {exc_chain}"
  177. status, headers, body = (
  178. http.HTTPStatus.BAD_REQUEST,
  179. Headers(),
  180. f"Failed to open a WebSocket connection: {exc_str}.\n".encode(),
  181. )
  182. else:
  183. self.logger.error("opening handshake failed", exc_info=True)
  184. status, headers, body = (
  185. http.HTTPStatus.INTERNAL_SERVER_ERROR,
  186. Headers(),
  187. (
  188. b"Failed to open a WebSocket connection.\n"
  189. b"See server log for more information.\n"
  190. ),
  191. )
  192. headers.setdefault("Date", email.utils.formatdate(usegmt=True))
  193. if self.server_header:
  194. headers.setdefault("Server", self.server_header)
  195. headers.setdefault("Content-Length", str(len(body)))
  196. headers.setdefault("Content-Type", "text/plain")
  197. headers.setdefault("Connection", "close")
  198. self.write_http_response(status, headers, body)
  199. self.logger.info(
  200. "connection rejected (%d %s)", status.value, status.phrase
  201. )
  202. await self.close_transport()
  203. return
  204. try:
  205. await self.ws_handler(self)
  206. except Exception:
  207. self.logger.error("connection handler failed", exc_info=True)
  208. if not self.closed:
  209. self.fail_connection(1011)
  210. raise
  211. try:
  212. await self.close()
  213. except ConnectionError:
  214. raise
  215. except Exception:
  216. self.logger.error("closing handshake failed", exc_info=True)
  217. raise
  218. except Exception:
  219. # Last-ditch attempt to avoid leaking connections on errors.
  220. try:
  221. self.transport.close()
  222. except Exception: # pragma: no cover
  223. pass
  224. finally:
  225. # Unregister the connection with the server when the handler task
  226. # terminates. Registration is tied to the lifecycle of the handler
  227. # task because the server waits for tasks attached to registered
  228. # connections before terminating.
  229. self.ws_server.unregister(self)
  230. self.logger.info("connection closed")
  231. async def read_http_request(self) -> tuple[str, Headers]:
  232. """
  233. Read request line and headers from the HTTP request.
  234. If the request contains a body, it may be read from ``self.reader``
  235. after this coroutine returns.
  236. Raises:
  237. InvalidMessage: If the HTTP message is malformed or isn't an
  238. HTTP/1.1 GET request.
  239. """
  240. try:
  241. path, headers = await read_request(self.reader)
  242. except asyncio.CancelledError: # pragma: no cover
  243. raise
  244. except Exception as exc:
  245. raise InvalidMessage("did not receive a valid HTTP request") from exc
  246. if self.debug:
  247. self.logger.debug("< GET %s HTTP/1.1", path)
  248. for key, value in headers.raw_items():
  249. self.logger.debug("< %s: %s", key, value)
  250. self.path = path
  251. self.request_headers = headers
  252. return path, headers
  253. def write_http_response(
  254. self, status: http.HTTPStatus, headers: Headers, body: bytes | None = None
  255. ) -> None:
  256. """
  257. Write status line and headers to the HTTP response.
  258. This coroutine is also able to write a response body.
  259. """
  260. self.response_headers = headers
  261. if self.debug:
  262. self.logger.debug("> HTTP/1.1 %d %s", status.value, status.phrase)
  263. for key, value in headers.raw_items():
  264. self.logger.debug("> %s: %s", key, value)
  265. if body is not None:
  266. self.logger.debug("> [body] (%d bytes)", len(body))
  267. # Since the status line and headers only contain ASCII characters,
  268. # we can keep this simple.
  269. response = f"HTTP/1.1 {status.value} {status.phrase}\r\n"
  270. response += str(headers)
  271. self.transport.write(response.encode())
  272. if body is not None:
  273. self.transport.write(body)
  274. async def process_request(
  275. self, path: str, request_headers: Headers
  276. ) -> HTTPResponse | None:
  277. """
  278. Intercept the HTTP request and return an HTTP response if appropriate.
  279. You may override this method in a :class:`WebSocketServerProtocol`
  280. subclass, for example:
  281. * to return an HTTP 200 OK response on a given path; then a load
  282. balancer can use this path for a health check;
  283. * to authenticate the request and return an HTTP 401 Unauthorized or an
  284. HTTP 403 Forbidden when authentication fails.
  285. You may also override this method with the ``process_request``
  286. argument of :func:`serve` and :class:`WebSocketServerProtocol`. This
  287. is equivalent, except ``process_request`` won't have access to the
  288. protocol instance, so it can't store information for later use.
  289. :meth:`process_request` is expected to complete quickly. If it may run
  290. for a long time, then it should await :meth:`wait_closed` and exit if
  291. :meth:`wait_closed` completes, or else it could prevent the server
  292. from shutting down.
  293. Args:
  294. path: Request path, including optional query string.
  295. request_headers: Request headers.
  296. Returns:
  297. tuple[StatusLike, HeadersLike, bytes] | None: :obj:`None` to
  298. continue the WebSocket handshake normally.
  299. An HTTP response, represented by a 3-uple of the response status,
  300. headers, and body, to abort the WebSocket handshake and return
  301. that HTTP response instead.
  302. """
  303. if self._process_request is not None:
  304. response = self._process_request(path, request_headers)
  305. if isinstance(response, Awaitable):
  306. return await response
  307. else:
  308. # For backwards compatibility with 7.0.
  309. warnings.warn(
  310. "declare process_request as a coroutine", DeprecationWarning
  311. )
  312. return response
  313. return None
  314. @staticmethod
  315. def process_origin(
  316. headers: Headers, origins: Sequence[Origin | None] | None = None
  317. ) -> Origin | None:
  318. """
  319. Handle the Origin HTTP request header.
  320. Args:
  321. headers: Request headers.
  322. origins: Optional list of acceptable origins.
  323. Raises:
  324. InvalidOrigin: If the origin isn't acceptable.
  325. """
  326. # "The user agent MUST NOT include more than one Origin header field"
  327. # per https://datatracker.ietf.org/doc/html/rfc6454#section-7.3.
  328. try:
  329. origin = headers.get("Origin")
  330. except MultipleValuesError as exc:
  331. raise InvalidHeader("Origin", "multiple values") from exc
  332. if origin is not None:
  333. origin = cast(Origin, origin)
  334. if origins is not None:
  335. if origin not in origins:
  336. raise InvalidOrigin(origin)
  337. return origin
  338. @staticmethod
  339. def process_extensions(
  340. headers: Headers,
  341. available_extensions: Sequence[ServerExtensionFactory] | None,
  342. ) -> tuple[str | None, list[Extension]]:
  343. """
  344. Handle the Sec-WebSocket-Extensions HTTP request header.
  345. Accept or reject each extension proposed in the client request.
  346. Negotiate parameters for accepted extensions.
  347. Return the Sec-WebSocket-Extensions HTTP response header and the list
  348. of accepted extensions.
  349. :rfc:`6455` leaves the rules up to the specification of each
  350. :extension.
  351. To provide this level of flexibility, for each extension proposed by
  352. the client, we check for a match with each extension available in the
  353. server configuration. If no match is found, the extension is ignored.
  354. If several variants of the same extension are proposed by the client,
  355. it may be accepted several times, which won't make sense in general.
  356. Extensions must implement their own requirements. For this purpose,
  357. the list of previously accepted extensions is provided.
  358. This process doesn't allow the server to reorder extensions. It can
  359. only select a subset of the extensions proposed by the client.
  360. Other requirements, for example related to mandatory extensions or the
  361. order of extensions, may be implemented by overriding this method.
  362. Args:
  363. headers: Request headers.
  364. extensions: Optional list of supported extensions.
  365. Raises:
  366. InvalidHandshake: To abort the handshake with an HTTP 400 error.
  367. """
  368. response_header_value: str | None = None
  369. extension_headers: list[ExtensionHeader] = []
  370. accepted_extensions: list[Extension] = []
  371. header_values = headers.get_all("Sec-WebSocket-Extensions")
  372. if header_values and available_extensions:
  373. parsed_header_values: list[ExtensionHeader] = sum(
  374. [parse_extension(header_value) for header_value in header_values], []
  375. )
  376. for name, request_params in parsed_header_values:
  377. for ext_factory in available_extensions:
  378. # Skip non-matching extensions based on their name.
  379. if ext_factory.name != name:
  380. continue
  381. # Skip non-matching extensions based on their params.
  382. try:
  383. response_params, extension = ext_factory.process_request_params(
  384. request_params, accepted_extensions
  385. )
  386. except NegotiationError:
  387. continue
  388. # Add matching extension to the final list.
  389. extension_headers.append((name, response_params))
  390. accepted_extensions.append(extension)
  391. # Break out of the loop once we have a match.
  392. break
  393. # If we didn't break from the loop, no extension in our list
  394. # matched what the client sent. The extension is declined.
  395. # Serialize extension header.
  396. if extension_headers:
  397. response_header_value = build_extension(extension_headers)
  398. return response_header_value, accepted_extensions
  399. # Not @staticmethod because it calls self.select_subprotocol()
  400. def process_subprotocol(
  401. self, headers: Headers, available_subprotocols: Sequence[Subprotocol] | None
  402. ) -> Subprotocol | None:
  403. """
  404. Handle the Sec-WebSocket-Protocol HTTP request header.
  405. Return Sec-WebSocket-Protocol HTTP response header, which is the same
  406. as the selected subprotocol.
  407. Args:
  408. headers: Request headers.
  409. available_subprotocols: Optional list of supported subprotocols.
  410. Raises:
  411. InvalidHandshake: To abort the handshake with an HTTP 400 error.
  412. """
  413. subprotocol: Subprotocol | None = None
  414. header_values = headers.get_all("Sec-WebSocket-Protocol")
  415. if header_values and available_subprotocols:
  416. parsed_header_values: list[Subprotocol] = sum(
  417. [parse_subprotocol(header_value) for header_value in header_values], []
  418. )
  419. subprotocol = self.select_subprotocol(
  420. parsed_header_values, available_subprotocols
  421. )
  422. return subprotocol
  423. def select_subprotocol(
  424. self,
  425. client_subprotocols: Sequence[Subprotocol],
  426. server_subprotocols: Sequence[Subprotocol],
  427. ) -> Subprotocol | None:
  428. """
  429. Pick a subprotocol among those supported by the client and the server.
  430. If several subprotocols are available, select the preferred subprotocol
  431. by giving equal weight to the preferences of the client and the server.
  432. If no subprotocol is available, proceed without a subprotocol.
  433. You may provide a ``select_subprotocol`` argument to :func:`serve` or
  434. :class:`WebSocketServerProtocol` to override this logic. For example,
  435. you could reject the handshake if the client doesn't support a
  436. particular subprotocol, rather than accept the handshake without that
  437. subprotocol.
  438. Args:
  439. client_subprotocols: List of subprotocols offered by the client.
  440. server_subprotocols: List of subprotocols available on the server.
  441. Returns:
  442. Selected subprotocol, if a common subprotocol was found.
  443. :obj:`None` to continue without a subprotocol.
  444. """
  445. if self._select_subprotocol is not None:
  446. return self._select_subprotocol(client_subprotocols, server_subprotocols)
  447. subprotocols = set(client_subprotocols) & set(server_subprotocols)
  448. if not subprotocols:
  449. return None
  450. return sorted(
  451. subprotocols,
  452. key=lambda p: client_subprotocols.index(p) + server_subprotocols.index(p),
  453. )[0]
  454. async def handshake(
  455. self,
  456. origins: Sequence[Origin | None] | None = None,
  457. available_extensions: Sequence[ServerExtensionFactory] | None = None,
  458. available_subprotocols: Sequence[Subprotocol] | None = None,
  459. extra_headers: HeadersLikeOrCallable | None = None,
  460. ) -> str:
  461. """
  462. Perform the server side of the opening handshake.
  463. Args:
  464. origins: List of acceptable values of the Origin HTTP header;
  465. include :obj:`None` if the lack of an origin is acceptable.
  466. extensions: List of supported extensions, in order in which they
  467. should be tried.
  468. subprotocols: List of supported subprotocols, in order of
  469. decreasing preference.
  470. extra_headers: Arbitrary HTTP headers to add to the response when
  471. the handshake succeeds.
  472. Returns:
  473. path of the URI of the request.
  474. Raises:
  475. InvalidHandshake: If the handshake fails.
  476. """
  477. path, request_headers = await self.read_http_request()
  478. # Hook for customizing request handling, for example checking
  479. # authentication or treating some paths as plain HTTP endpoints.
  480. early_response_awaitable = self.process_request(path, request_headers)
  481. if isinstance(early_response_awaitable, Awaitable):
  482. early_response = await early_response_awaitable
  483. else:
  484. # For backwards compatibility with 7.0.
  485. warnings.warn("declare process_request as a coroutine", DeprecationWarning)
  486. early_response = early_response_awaitable
  487. # The connection may drop while process_request is running.
  488. if self.state is State.CLOSED:
  489. # This subclass of ConnectionError is silently ignored in handler().
  490. raise BrokenPipeError("connection closed during opening handshake")
  491. # Change the response to a 503 error if the server is shutting down.
  492. if not self.ws_server.is_serving():
  493. early_response = (
  494. http.HTTPStatus.SERVICE_UNAVAILABLE,
  495. [],
  496. b"Server is shutting down.\n",
  497. )
  498. if early_response is not None:
  499. raise AbortHandshake(*early_response)
  500. key = check_request(request_headers)
  501. self.origin = self.process_origin(request_headers, origins)
  502. extensions_header, self.extensions = self.process_extensions(
  503. request_headers, available_extensions
  504. )
  505. protocol_header = self.subprotocol = self.process_subprotocol(
  506. request_headers, available_subprotocols
  507. )
  508. response_headers = Headers()
  509. build_response(response_headers, key)
  510. if extensions_header is not None:
  511. response_headers["Sec-WebSocket-Extensions"] = extensions_header
  512. if protocol_header is not None:
  513. response_headers["Sec-WebSocket-Protocol"] = protocol_header
  514. if callable(extra_headers):
  515. extra_headers = extra_headers(path, self.request_headers)
  516. if extra_headers is not None:
  517. response_headers.update(extra_headers)
  518. response_headers.setdefault("Date", email.utils.formatdate(usegmt=True))
  519. if self.server_header is not None:
  520. response_headers.setdefault("Server", self.server_header)
  521. self.write_http_response(http.HTTPStatus.SWITCHING_PROTOCOLS, response_headers)
  522. self.logger.info("connection open")
  523. self.connection_open()
  524. return path
  525. class WebSocketServer:
  526. """
  527. WebSocket server returned by :func:`serve`.
  528. This class mirrors the API of :class:`~asyncio.Server`.
  529. It keeps track of WebSocket connections in order to close them properly
  530. when shutting down.
  531. Args:
  532. logger: Logger for this server.
  533. It defaults to ``logging.getLogger("websockets.server")``.
  534. See the :doc:`logging guide <../../topics/logging>` for details.
  535. """
  536. def __init__(self, logger: LoggerLike | None = None) -> None:
  537. if logger is None:
  538. logger = logging.getLogger("websockets.server")
  539. self.logger = logger
  540. # Keep track of active connections.
  541. self.websockets: set[WebSocketServerProtocol] = set()
  542. # Task responsible for closing the server and terminating connections.
  543. self.close_task: asyncio.Task[None] | None = None
  544. # Completed when the server is closed and connections are terminated.
  545. self.closed_waiter: asyncio.Future[None]
  546. def wrap(self, server: asyncio.base_events.Server) -> None:
  547. """
  548. Attach to a given :class:`~asyncio.Server`.
  549. Since :meth:`~asyncio.loop.create_server` doesn't support injecting a
  550. custom ``Server`` class, the easiest solution that doesn't rely on
  551. private :mod:`asyncio` APIs is to:
  552. - instantiate a :class:`WebSocketServer`
  553. - give the protocol factory a reference to that instance
  554. - call :meth:`~asyncio.loop.create_server` with the factory
  555. - attach the resulting :class:`~asyncio.Server` with this method
  556. """
  557. self.server = server
  558. for sock in server.sockets:
  559. if sock.family == socket.AF_INET:
  560. name = "%s:%d" % sock.getsockname()
  561. elif sock.family == socket.AF_INET6:
  562. name = "[%s]:%d" % sock.getsockname()[:2]
  563. elif sock.family == socket.AF_UNIX:
  564. name = sock.getsockname()
  565. # In the unlikely event that someone runs websockets over a
  566. # protocol other than IP or Unix sockets, avoid crashing.
  567. else: # pragma: no cover
  568. name = str(sock.getsockname())
  569. self.logger.info("server listening on %s", name)
  570. # Initialized here because we need a reference to the event loop.
  571. # This could be moved back to __init__ now that Python < 3.10 isn't
  572. # supported anymore, but I'm not taking that risk in legacy code.
  573. self.closed_waiter = server.get_loop().create_future()
  574. def register(self, protocol: WebSocketServerProtocol) -> None:
  575. """
  576. Register a connection with this server.
  577. """
  578. self.websockets.add(protocol)
  579. def unregister(self, protocol: WebSocketServerProtocol) -> None:
  580. """
  581. Unregister a connection with this server.
  582. """
  583. self.websockets.remove(protocol)
  584. def close(self, close_connections: bool = True) -> None:
  585. """
  586. Close the server.
  587. * Close the underlying :class:`~asyncio.Server`.
  588. * When ``close_connections`` is :obj:`True`, which is the default,
  589. close existing connections. Specifically:
  590. * Reject opening WebSocket connections with an HTTP 503 (service
  591. unavailable) error. This happens when the server accepted the TCP
  592. connection but didn't complete the opening handshake before closing.
  593. * Close open WebSocket connections with close code 1001 (going away).
  594. * Wait until all connection handlers terminate.
  595. :meth:`close` is idempotent.
  596. """
  597. if self.close_task is None:
  598. self.close_task = self.get_loop().create_task(
  599. self._close(close_connections)
  600. )
  601. async def _close(self, close_connections: bool) -> None:
  602. """
  603. Implementation of :meth:`close`.
  604. This calls :meth:`~asyncio.Server.close` on the underlying
  605. :class:`~asyncio.Server` object to stop accepting new connections and
  606. then closes open connections with close code 1001.
  607. """
  608. self.logger.info("server closing")
  609. # Stop accepting new connections.
  610. self.server.close()
  611. if close_connections:
  612. # Close OPEN connections with close code 1001. After server.close(),
  613. # handshake() closes OPENING connections with an HTTP 503 error.
  614. close_tasks = [
  615. asyncio.create_task(websocket.close(1001))
  616. for websocket in self.websockets
  617. if websocket.state is not State.CONNECTING
  618. ]
  619. # asyncio.wait doesn't accept an empty first argument.
  620. if close_tasks:
  621. await asyncio.wait(close_tasks)
  622. # Wait until all TCP connections are closed.
  623. await self.server.wait_closed()
  624. # Wait until all connection handlers terminate.
  625. # asyncio.wait doesn't accept an empty first argument.
  626. if self.websockets:
  627. await asyncio.wait(
  628. [websocket.handler_task for websocket in self.websockets]
  629. )
  630. # Tell wait_closed() to return.
  631. self.closed_waiter.set_result(None)
  632. self.logger.info("server closed")
  633. async def wait_closed(self) -> None:
  634. """
  635. Wait until the server is closed.
  636. When :meth:`wait_closed` returns, all TCP connections are closed and
  637. all connection handlers have returned.
  638. To ensure a fast shutdown, a connection handler should always be
  639. awaiting at least one of:
  640. * :meth:`~WebSocketServerProtocol.recv`: when the connection is closed,
  641. it raises :exc:`~websockets.exceptions.ConnectionClosedOK`;
  642. * :meth:`~WebSocketServerProtocol.wait_closed`: when the connection is
  643. closed, it returns.
  644. Then the connection handler is immediately notified of the shutdown;
  645. it can clean up and exit.
  646. """
  647. await asyncio.shield(self.closed_waiter)
  648. def get_loop(self) -> asyncio.AbstractEventLoop:
  649. """
  650. See :meth:`asyncio.Server.get_loop`.
  651. """
  652. return self.server.get_loop()
  653. def is_serving(self) -> bool:
  654. """
  655. See :meth:`asyncio.Server.is_serving`.
  656. """
  657. return self.server.is_serving()
  658. async def start_serving(self) -> None: # pragma: no cover
  659. """
  660. See :meth:`asyncio.Server.start_serving`.
  661. Typical use::
  662. server = await serve(..., start_serving=False)
  663. # perform additional setup here...
  664. # ... then start the server
  665. await server.start_serving()
  666. """
  667. await self.server.start_serving()
  668. async def serve_forever(self) -> None: # pragma: no cover
  669. """
  670. See :meth:`asyncio.Server.serve_forever`.
  671. Typical use::
  672. server = await serve(...)
  673. # this coroutine doesn't return
  674. # canceling it stops the server
  675. await server.serve_forever()
  676. This is an alternative to using :func:`serve` as an asynchronous context
  677. manager. Shutdown is triggered by canceling :meth:`serve_forever`
  678. instead of exiting a :func:`serve` context.
  679. """
  680. await self.server.serve_forever()
  681. @property
  682. def sockets(self) -> Iterable[socket.socket]:
  683. """
  684. See :attr:`asyncio.Server.sockets`.
  685. """
  686. return self.server.sockets
  687. async def __aenter__(self) -> Self: # pragma: no cover
  688. return self
  689. async def __aexit__(
  690. self,
  691. exc_type: type[BaseException] | None,
  692. exc_value: BaseException | None,
  693. traceback: TracebackType | None,
  694. ) -> None: # pragma: no cover
  695. self.close()
  696. await self.wait_closed()
  697. class Serve:
  698. """
  699. Start a WebSocket server listening on ``host`` and ``port``.
  700. Whenever a client connects, the server creates a
  701. :class:`WebSocketServerProtocol`, performs the opening handshake, and
  702. delegates to the connection handler, ``ws_handler``.
  703. The handler receives the :class:`WebSocketServerProtocol` and uses it to
  704. send and receive messages.
  705. Once the handler completes, either normally or with an exception, the
  706. server performs the closing handshake and closes the connection.
  707. Awaiting :func:`serve` yields a :class:`WebSocketServer`. This object
  708. provides a :meth:`~WebSocketServer.close` method to shut down the server::
  709. # set this future to exit the server
  710. stop = asyncio.get_running_loop().create_future()
  711. server = await serve(...)
  712. await stop
  713. server.close()
  714. await server.wait_closed()
  715. :func:`serve` can be used as an asynchronous context manager. Then, the
  716. server is shut down automatically when exiting the context::
  717. # set this future to exit the server
  718. stop = asyncio.get_running_loop().create_future()
  719. async with serve(...):
  720. await stop
  721. Args:
  722. ws_handler: Connection handler. It receives the WebSocket connection,
  723. which is a :class:`WebSocketServerProtocol`, in argument.
  724. host: Network interfaces the server binds to.
  725. See :meth:`~asyncio.loop.create_server` for details.
  726. port: TCP port the server listens on.
  727. See :meth:`~asyncio.loop.create_server` for details.
  728. create_protocol: Factory for the :class:`asyncio.Protocol` managing
  729. the connection. It defaults to :class:`WebSocketServerProtocol`.
  730. Set it to a wrapper or a subclass to customize connection handling.
  731. logger: Logger for this server.
  732. It defaults to ``logging.getLogger("websockets.server")``.
  733. See the :doc:`logging guide <../../topics/logging>` for details.
  734. compression: The "permessage-deflate" extension is enabled by default.
  735. Set ``compression`` to :obj:`None` to disable it. See the
  736. :doc:`compression guide <../../topics/compression>` for details.
  737. origins: Acceptable values of the ``Origin`` header, for defending
  738. against Cross-Site WebSocket Hijacking attacks. Include :obj:`None`
  739. in the list if the lack of an origin is acceptable.
  740. extensions: List of supported extensions, in order in which they
  741. should be negotiated and run.
  742. subprotocols: List of supported subprotocols, in order of decreasing
  743. preference.
  744. extra_headers (HeadersLike | Callable[[str, Headers] | HeadersLike]):
  745. Arbitrary HTTP headers to add to the response. This can be
  746. a :data:`~websockets.datastructures.HeadersLike` or a callable
  747. taking the request path and headers in arguments and returning
  748. a :data:`~websockets.datastructures.HeadersLike`.
  749. server_header: Value of the ``Server`` response header.
  750. It defaults to ``"Python/x.y.z websockets/X.Y"``.
  751. Setting it to :obj:`None` removes the header.
  752. process_request (Callable[[str, Headers], \
  753. Awaitable[tuple[StatusLike, HeadersLike, bytes] | None]] | None):
  754. Intercept HTTP request before the opening handshake.
  755. See :meth:`~WebSocketServerProtocol.process_request` for details.
  756. select_subprotocol: Select a subprotocol supported by the client.
  757. See :meth:`~WebSocketServerProtocol.select_subprotocol` for details.
  758. open_timeout: Timeout for opening connections in seconds.
  759. :obj:`None` disables the timeout.
  760. See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the
  761. documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``,
  762. ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``.
  763. Any other keyword arguments are passed the event loop's
  764. :meth:`~asyncio.loop.create_server` method.
  765. For example:
  766. * You can set ``ssl`` to a :class:`~ssl.SSLContext` to enable TLS.
  767. * You can set ``sock`` to a :obj:`~socket.socket` that you created
  768. outside of websockets.
  769. Returns:
  770. WebSocket server.
  771. """
  772. def __init__(
  773. self,
  774. # The version that accepts the path in the second argument is deprecated.
  775. ws_handler: (
  776. Callable[[WebSocketServerProtocol], Awaitable[Any]]
  777. | Callable[[WebSocketServerProtocol, str], Awaitable[Any]]
  778. ),
  779. host: str | Sequence[str] | None = None,
  780. port: int | None = None,
  781. *,
  782. create_protocol: Callable[..., WebSocketServerProtocol] | None = None,
  783. logger: LoggerLike | None = None,
  784. compression: str | None = "deflate",
  785. origins: Sequence[Origin | None] | None = None,
  786. extensions: Sequence[ServerExtensionFactory] | None = None,
  787. subprotocols: Sequence[Subprotocol] | None = None,
  788. extra_headers: HeadersLikeOrCallable | None = None,
  789. server_header: str | None = SERVER,
  790. process_request: (
  791. Callable[[str, Headers], Awaitable[HTTPResponse | None]] | None
  792. ) = None,
  793. select_subprotocol: (
  794. Callable[[Sequence[Subprotocol], Sequence[Subprotocol]], Subprotocol] | None
  795. ) = None,
  796. open_timeout: float | None = 10,
  797. ping_interval: float | None = 20,
  798. ping_timeout: float | None = 20,
  799. close_timeout: float | None = None,
  800. max_size: int | None = 2**20,
  801. max_queue: int | None = 2**5,
  802. read_limit: int = 2**16,
  803. write_limit: int = 2**16,
  804. **kwargs: Any,
  805. ) -> None:
  806. # Backwards compatibility: close_timeout used to be called timeout.
  807. timeout: float | None = kwargs.pop("timeout", None)
  808. if timeout is None:
  809. timeout = 10
  810. else:
  811. warnings.warn("rename timeout to close_timeout", DeprecationWarning)
  812. # If both are specified, timeout is ignored.
  813. if close_timeout is None:
  814. close_timeout = timeout
  815. # Backwards compatibility: create_protocol used to be called klass.
  816. klass: type[WebSocketServerProtocol] | None = kwargs.pop("klass", None)
  817. if klass is None:
  818. klass = WebSocketServerProtocol
  819. else:
  820. warnings.warn("rename klass to create_protocol", DeprecationWarning)
  821. # If both are specified, klass is ignored.
  822. if create_protocol is None:
  823. create_protocol = klass
  824. # Backwards compatibility: recv() used to return None on closed connections
  825. legacy_recv: bool = kwargs.pop("legacy_recv", False)
  826. # Backwards compatibility: the loop parameter used to be supported.
  827. _loop: asyncio.AbstractEventLoop | None = kwargs.pop("loop", None)
  828. if _loop is None:
  829. loop = asyncio.get_event_loop()
  830. else:
  831. loop = _loop
  832. warnings.warn("remove loop argument", DeprecationWarning)
  833. ws_server = WebSocketServer(logger=logger)
  834. secure = kwargs.get("ssl") is not None
  835. if compression == "deflate":
  836. extensions = enable_server_permessage_deflate(extensions)
  837. elif compression is not None:
  838. raise ValueError(f"unsupported compression: {compression}")
  839. if subprotocols is not None:
  840. validate_subprotocols(subprotocols)
  841. # Help mypy and avoid this error: "type[WebSocketServerProtocol] |
  842. # Callable[..., WebSocketServerProtocol]" not callable [misc]
  843. create_protocol = cast(Callable[..., WebSocketServerProtocol], create_protocol)
  844. factory = functools.partial(
  845. create_protocol,
  846. # For backwards compatibility with 10.0 or earlier. Done here in
  847. # addition to WebSocketServerProtocol to trigger the deprecation
  848. # warning once per serve() call rather than once per connection.
  849. remove_path_argument(ws_handler),
  850. ws_server,
  851. host=host,
  852. port=port,
  853. secure=secure,
  854. open_timeout=open_timeout,
  855. ping_interval=ping_interval,
  856. ping_timeout=ping_timeout,
  857. close_timeout=close_timeout,
  858. max_size=max_size,
  859. max_queue=max_queue,
  860. read_limit=read_limit,
  861. write_limit=write_limit,
  862. loop=_loop,
  863. legacy_recv=legacy_recv,
  864. origins=origins,
  865. extensions=extensions,
  866. subprotocols=subprotocols,
  867. extra_headers=extra_headers,
  868. server_header=server_header,
  869. process_request=process_request,
  870. select_subprotocol=select_subprotocol,
  871. logger=logger,
  872. )
  873. if kwargs.pop("unix", False):
  874. path: str | None = kwargs.pop("path", None)
  875. # unix_serve(path) must not specify host and port parameters.
  876. assert host is None and port is None
  877. create_server = functools.partial(
  878. loop.create_unix_server, factory, path, **kwargs
  879. )
  880. else:
  881. create_server = functools.partial(
  882. loop.create_server, factory, host, port, **kwargs
  883. )
  884. # This is a coroutine function.
  885. self._create_server = create_server
  886. self.ws_server = ws_server
  887. # async with serve(...)
  888. async def __aenter__(self) -> WebSocketServer:
  889. return await self
  890. async def __aexit__(
  891. self,
  892. exc_type: type[BaseException] | None,
  893. exc_value: BaseException | None,
  894. traceback: TracebackType | None,
  895. ) -> None:
  896. self.ws_server.close()
  897. await self.ws_server.wait_closed()
  898. # await serve(...)
  899. def __await__(self) -> Generator[Any, None, WebSocketServer]:
  900. # Create a suitable iterator by calling __await__ on a coroutine.
  901. return self.__await_impl__().__await__()
  902. async def __await_impl__(self) -> WebSocketServer:
  903. server = await self._create_server()
  904. self.ws_server.wrap(server)
  905. return self.ws_server
  906. serve = Serve
  907. def unix_serve(
  908. # The version that accepts the path in the second argument is deprecated.
  909. ws_handler: (
  910. Callable[[WebSocketServerProtocol], Awaitable[Any]]
  911. | Callable[[WebSocketServerProtocol, str], Awaitable[Any]]
  912. ),
  913. path: str | None = None,
  914. **kwargs: Any,
  915. ) -> Serve:
  916. """
  917. Start a WebSocket server listening on a Unix socket.
  918. This function is identical to :func:`serve`, except the ``host`` and
  919. ``port`` arguments are replaced by ``path``. It is only available on Unix.
  920. Unrecognized keyword arguments are passed the event loop's
  921. :meth:`~asyncio.loop.create_unix_server` method.
  922. It's useful for deploying a server behind a reverse proxy such as nginx.
  923. Args:
  924. path: File system path to the Unix socket.
  925. """
  926. return serve(ws_handler, path=path, unix=True, **kwargs)
  927. def remove_path_argument(
  928. ws_handler: (
  929. Callable[[WebSocketServerProtocol], Awaitable[Any]]
  930. | Callable[[WebSocketServerProtocol, str], Awaitable[Any]]
  931. ),
  932. ) -> Callable[[WebSocketServerProtocol], Awaitable[Any]]:
  933. try:
  934. inspect.signature(ws_handler).bind(None)
  935. except TypeError:
  936. try:
  937. inspect.signature(ws_handler).bind(None, "")
  938. except TypeError: # pragma: no cover
  939. # ws_handler accepts neither one nor two arguments; leave it alone.
  940. pass
  941. else:
  942. # ws_handler accepts two arguments; activate backwards compatibility.
  943. warnings.warn("remove second argument of ws_handler", DeprecationWarning)
  944. async def _ws_handler(websocket: WebSocketServerProtocol) -> Any:
  945. return await cast(
  946. Callable[[WebSocketServerProtocol, str], Awaitable[Any]],
  947. ws_handler,
  948. )(websocket, websocket.path)
  949. return _ws_handler
  950. return cast(
  951. Callable[[WebSocketServerProtocol], Awaitable[Any]],
  952. ws_handler,
  953. )