connection.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. from __future__ import annotations
  2. import contextlib
  3. import logging
  4. import random
  5. import struct
  6. import traceback
  7. import uuid
  8. import weakref
  9. from collections.abc import AsyncIterable, AsyncIterator, Iterable, Mapping
  10. from types import TracebackType
  11. from typing import Any, Literal, Self, overload
  12. import trio
  13. import trio.abc
  14. from ..exceptions import (
  15. ConcurrencyError,
  16. ConnectionClosed,
  17. ConnectionClosedOK,
  18. ProtocolError,
  19. )
  20. from ..frames import DATA_OPCODES, PONG, CloseCode, Frame
  21. from ..http11 import Request, Response
  22. from ..protocol import CLOSED, OPEN, Event, Protocol, State
  23. from ..typing import BytesLike, Data, DataLike, LoggerLike, Subprotocol
  24. from .messages import Assembler
  25. __all__ = ["Connection"]
  26. class Connection(trio.abc.AsyncResource):
  27. """
  28. :mod:`trio` implementation of a WebSocket connection.
  29. :class:`Connection` provides APIs shared between WebSocket servers and
  30. clients.
  31. You shouldn't use it directly. Instead, use
  32. :class:`~websockets.trio.client.ClientConnection` or
  33. :class:`~websockets.trio.server.ServerConnection`.
  34. """
  35. def __init__(
  36. self,
  37. nursery: trio.Nursery,
  38. stream: trio.abc.Stream,
  39. protocol: Protocol,
  40. *,
  41. ping_interval: float | None = 20,
  42. ping_timeout: float | None = 20,
  43. close_timeout: float | None = 10,
  44. max_queue: int | None | tuple[int | None, int | None] = 16,
  45. ) -> None:
  46. self.nursery = nursery
  47. self.stream = stream
  48. self.protocol = protocol
  49. self.ping_interval = ping_interval
  50. self.ping_timeout = ping_timeout
  51. self.close_timeout = close_timeout
  52. if isinstance(max_queue, int) or max_queue is None:
  53. max_queue_high, max_queue_low = max_queue, None
  54. else:
  55. max_queue_high, max_queue_low = max_queue
  56. # Inject reference to this instance in the protocol's logger.
  57. self.protocol.logger = logging.LoggerAdapter(
  58. self.protocol.logger,
  59. {"websocket": weakref.proxy(self)},
  60. )
  61. # Copy attributes from the protocol for convenience.
  62. self.id: uuid.UUID = self.protocol.id
  63. """Unique identifier of the connection. Useful in logs."""
  64. self.logger: LoggerLike = self.protocol.logger
  65. """Logger for this connection."""
  66. self.debug = self.protocol.debug
  67. # HTTP handshake request and response.
  68. self.request: Request | None = None
  69. """Opening handshake request."""
  70. self.response: Response | None = None
  71. """Opening handshake response."""
  72. # Lock stopping reads when the assembler buffer is full.
  73. self.recv_flow_control: trio.Event | None = None
  74. # Assembler turning frames into messages and serializing reads.
  75. self.recv_messages = Assembler(
  76. max_queue_high,
  77. max_queue_low,
  78. pause=self.pause_reading,
  79. resume=self.resume_reading,
  80. )
  81. # Deadline for the closing handshake.
  82. self.close_deadline: float | None = None
  83. # Lock preventing concurrent calls to send_all or send_eof.
  84. self.send_lock = trio.Lock()
  85. # Protect sending fragmented messages.
  86. self.send_in_progress: trio.Event | None = None
  87. # Mapping of ping IDs to pong waiters, in chronological order.
  88. self.pending_pings: dict[bytes, tuple[trio.Event, float, bool]] = {}
  89. self.latency: float = 0.0
  90. """
  91. Latency of the connection, in seconds.
  92. Latency is defined as the round-trip time of the connection. It is
  93. measured by sending a Ping frame and waiting for a matching Pong frame.
  94. Before the first measurement, :attr:`latency` is ``0``.
  95. By default, websockets enables a :ref:`keepalive <keepalive>` mechanism
  96. that sends Ping frames automatically at regular intervals. You can also
  97. send Ping frames and measure latency with :meth:`ping`.
  98. """
  99. # Exception raised while reading from the connection, to be chained to
  100. # ConnectionClosed in order to show why the TCP connection dropped.
  101. self.recv_exc: BaseException | None = None
  102. # Completed when the TCP connection is closed and the WebSocket
  103. # connection state becomes CLOSED.
  104. self.stream_closed: trio.Event = trio.Event()
  105. # Start recv_events only after all attributes are initialized.
  106. self.nursery.start_soon(self.recv_events)
  107. # Public attributes
  108. @property
  109. def local_address(self) -> Any:
  110. """
  111. Local address of the connection.
  112. For IPv4 connections, this is a ``(host, port)`` tuple.
  113. The format of the address depends on the address family.
  114. See :meth:`~socket.socket.getsockname`.
  115. """
  116. if isinstance(self.stream, trio.SSLStream): # pragma: no cover
  117. stream = self.stream.transport_stream
  118. else:
  119. stream = self.stream
  120. if isinstance(stream, trio.SocketStream):
  121. return stream.socket.getsockname()
  122. else: # pragma: no cover
  123. raise NotImplementedError(f"unsupported stream type: {stream}")
  124. @property
  125. def remote_address(self) -> Any:
  126. """
  127. Remote address of the connection.
  128. For IPv4 connections, this is a ``(host, port)`` tuple.
  129. The format of the address depends on the address family.
  130. See :meth:`~socket.socket.getpeername`.
  131. """
  132. if isinstance(self.stream, trio.SSLStream): # pragma: no cover
  133. stream = self.stream.transport_stream
  134. else:
  135. stream = self.stream
  136. if isinstance(stream, trio.SocketStream):
  137. return stream.socket.getpeername()
  138. else: # pragma: no cover
  139. raise NotImplementedError(f"unsupported stream type: {stream}")
  140. @property
  141. def state(self) -> State:
  142. """
  143. State of the WebSocket connection, defined in :rfc:`6455`.
  144. This attribute is provided for completeness. Typical applications
  145. shouldn't check its value. Instead, they should call :meth:`~recv` or
  146. :meth:`send` and handle :exc:`~websockets.exceptions.ConnectionClosed`
  147. exceptions.
  148. """
  149. return self.protocol.state
  150. @property
  151. def subprotocol(self) -> Subprotocol | None:
  152. """
  153. Subprotocol negotiated during the opening handshake.
  154. :obj:`None` if no subprotocol was negotiated.
  155. """
  156. return self.protocol.subprotocol
  157. @property
  158. def close_code(self) -> int | None:
  159. """
  160. State of the WebSocket connection, defined in :rfc:`6455`.
  161. This attribute is provided for completeness. Typical applications
  162. shouldn't check its value. Instead, they should inspect attributes
  163. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  164. """
  165. return self.protocol.close_code
  166. @property
  167. def close_reason(self) -> str | None:
  168. """
  169. State of the WebSocket connection, defined in :rfc:`6455`.
  170. This attribute is provided for completeness. Typical applications
  171. shouldn't check its value. Instead, they should inspect attributes
  172. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  173. """
  174. return self.protocol.close_reason
  175. # Public methods
  176. async def __aenter__(self) -> Self:
  177. return self
  178. async def __aexit__(
  179. self,
  180. exc_type: type[BaseException] | None,
  181. exc_value: BaseException | None,
  182. traceback: TracebackType | None,
  183. ) -> None:
  184. if exc_type is None:
  185. await self.aclose()
  186. else:
  187. await self.aclose(CloseCode.INTERNAL_ERROR)
  188. async def __aiter__(self) -> AsyncIterator[Data]:
  189. """
  190. Iterate on incoming messages.
  191. The iterator calls :meth:`recv` and yields messages asynchronously in an
  192. infinite loop.
  193. It exits when the connection is closed normally. It raises a
  194. :exc:`~websockets.exceptions.ConnectionClosedError` exception after a
  195. protocol error or a network failure.
  196. """
  197. try:
  198. while True:
  199. yield await self.recv()
  200. except ConnectionClosedOK:
  201. return
  202. @overload
  203. async def recv(self, decode: Literal[True]) -> str: ...
  204. @overload
  205. async def recv(self, decode: Literal[False]) -> bytes: ...
  206. @overload
  207. async def recv(self, decode: bool | None = None) -> Data: ...
  208. async def recv(self, decode: bool | None = None) -> Data:
  209. """
  210. Receive the next message.
  211. When the connection is closed, :meth:`recv` raises
  212. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises
  213. :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal closure
  214. and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  215. error or a network failure. This is how you detect the end of the
  216. message stream.
  217. Canceling :meth:`recv` is safe. There's no risk of losing data. The next
  218. invocation of :meth:`recv` will return the next message.
  219. This makes it possible to enforce a timeout by wrapping :meth:`recv` in
  220. :func:`~trio.move_on_after` or :func:`~trio.fail_after`.
  221. When the message is fragmented, :meth:`recv` waits until all fragments
  222. are received, reassembles them, and returns the whole message.
  223. Args:
  224. decode: Set this flag to override the default behavior of returning
  225. :class:`str` or :class:`bytes`. See below for details.
  226. Returns:
  227. A string (:class:`str`) for a Text_ frame or a bytestring
  228. (:class:`bytes`) for a Binary_ frame.
  229. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  230. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  231. You may override this behavior with the ``decode`` argument:
  232. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames and
  233. return a bytestring (:class:`bytes`). This improves performance
  234. when decoding isn't needed, for example if the message contains
  235. JSON and you're using a JSON library that expects a bytestring.
  236. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
  237. and return a string (:class:`str`). This may be useful for
  238. servers that send binary frames instead of text frames.
  239. Raises:
  240. ConnectionClosed: When the connection is closed.
  241. ConcurrencyError: If two coroutines call :meth:`recv` or
  242. :meth:`recv_streaming` concurrently.
  243. """
  244. try:
  245. return await self.recv_messages.get(decode)
  246. except EOFError:
  247. pass
  248. # fallthrough
  249. except ConcurrencyError:
  250. raise ConcurrencyError(
  251. "cannot call recv while another coroutine "
  252. "is already running recv or recv_streaming"
  253. ) from None
  254. except UnicodeDecodeError as exc:
  255. async with self.send_context():
  256. self.protocol.fail(
  257. CloseCode.INVALID_DATA,
  258. f"{exc.reason} at position {exc.start}",
  259. )
  260. # fallthrough
  261. # Wait for the protocol state to be CLOSED before accessing close_exc.
  262. await self.stream_closed.wait()
  263. raise self.protocol.close_exc from self.recv_exc
  264. @overload
  265. def recv_streaming(self, decode: Literal[True]) -> AsyncIterator[str]: ...
  266. @overload
  267. def recv_streaming(self, decode: Literal[False]) -> AsyncIterator[bytes]: ...
  268. @overload
  269. def recv_streaming(self, decode: bool | None = None) -> AsyncIterator[Data]: ...
  270. async def recv_streaming(self, decode: bool | None = None) -> AsyncIterator[Data]:
  271. """
  272. Receive the next message frame by frame.
  273. This method is designed for receiving fragmented messages. It returns an
  274. asynchronous iterator that yields each fragment as it is received. This
  275. iterator must be fully consumed. Else, future calls to :meth:`recv` or
  276. :meth:`recv_streaming` will raise
  277. :exc:`~websockets.exceptions.ConcurrencyError`, making the connection
  278. unusable.
  279. :meth:`recv_streaming` raises the same exceptions as :meth:`recv`.
  280. Canceling :meth:`recv_streaming` before receiving the first frame is
  281. safe. Canceling it after receiving one or more frames leaves the
  282. iterator in a partially consumed state, making the connection unusable.
  283. Instead, you should close the connection with :meth:`aclose`.
  284. Args:
  285. decode: Set this flag to override the default behavior of returning
  286. :class:`str` or :class:`bytes`. See below for details.
  287. Returns:
  288. An iterator of strings (:class:`str`) for a Text_ frame or
  289. bytestrings (:class:`bytes`) for a Binary_ frame.
  290. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  291. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  292. You may override this behavior with the ``decode`` argument:
  293. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames
  294. and return bytestrings (:class:`bytes`). This may be useful to
  295. optimize performance when decoding isn't needed.
  296. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames
  297. and return strings (:class:`str`). This is useful for servers
  298. that send binary frames instead of text frames.
  299. Raises:
  300. ConnectionClosed: When the connection is closed.
  301. ConcurrencyError: If two coroutines call :meth:`recv` or
  302. :meth:`recv_streaming` concurrently.
  303. """
  304. try:
  305. async for frame in self.recv_messages.get_iter(decode):
  306. yield frame
  307. return
  308. except EOFError:
  309. pass
  310. # fallthrough
  311. except ConcurrencyError:
  312. raise ConcurrencyError(
  313. "cannot call recv_streaming while another coroutine "
  314. "is already running recv or recv_streaming"
  315. ) from None
  316. except UnicodeDecodeError as exc:
  317. async with self.send_context():
  318. self.protocol.fail(
  319. CloseCode.INVALID_DATA,
  320. f"{exc.reason} at position {exc.start}",
  321. )
  322. # fallthrough
  323. # Wait for the protocol state to be CLOSED before accessing close_exc.
  324. await self.stream_closed.wait()
  325. raise self.protocol.close_exc from self.recv_exc
  326. async def send(
  327. self,
  328. message: DataLike | Iterable[DataLike] | AsyncIterable[DataLike],
  329. *,
  330. text: bool | None = None,
  331. ) -> None:
  332. """
  333. Send a message.
  334. A string (:class:`str`) is sent as a Text_ frame. A bytestring or
  335. bytes-like object (:class:`bytes`, :class:`bytearray`, or
  336. :class:`memoryview`) is sent as a Binary_ frame.
  337. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  338. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  339. You may override this behavior with the ``text`` argument:
  340. * Set ``text=True`` to send an UTF-8 bytestring or bytes-like object
  341. (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) in a
  342. Text_ frame. This improves performance when the message is already
  343. UTF-8 encoded, for example if the message contains JSON and you're
  344. using a JSON library that produces a bytestring.
  345. * Set ``text=False`` to send a string (:class:`str`) in a Binary_
  346. frame. This may be useful for servers that expect binary frames
  347. instead of text frames.
  348. :meth:`send` also accepts an iterable or asynchronous iterable of
  349. strings, bytestrings, or bytes-like objects to enable fragmentation_.
  350. Each item is treated as a message fragment and sent in its own frame.
  351. All items must be of the same type, or else :meth:`send` will raise a
  352. :exc:`TypeError` and the connection will be closed.
  353. .. _fragmentation: https://datatracker.ietf.org/doc/html/rfc6455#section-5.4
  354. :meth:`send` rejects dict-like objects because this is often an error.
  355. (If you really want to send the keys of a dict-like object as fragments,
  356. call its :meth:`~dict.keys` method and pass the result to :meth:`send`.)
  357. Canceling :meth:`send` is discouraged. Instead, you should close the
  358. connection with :meth:`aclose`. Indeed, there are only two situations
  359. where :meth:`send` may yield control to the event loop and then get
  360. canceled; in both cases, :meth:`aclose` has the same effect and is
  361. more clear:
  362. 1. The write buffer is full. If you don't want to wait until enough
  363. data is sent, your only alternative is to close the connection.
  364. :meth:`aclose` will likely time out then abort the TCP connection.
  365. 2. ``message`` is an asynchronous iterator that yields control.
  366. Stopping in the middle of a fragmented message will cause a
  367. protocol error and the connection will be closed.
  368. When the connection is closed, :meth:`send` raises
  369. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it
  370. raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal
  371. connection closure and
  372. :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  373. error or a network failure.
  374. Args:
  375. message: Message to send.
  376. text: Force sending in a Text_ or Binary_ frame.
  377. Raises:
  378. ConnectionClosed: When the connection is closed.
  379. TypeError: If ``message`` doesn't have a supported type.
  380. """
  381. # While sending a fragmented message, prevent sending other messages
  382. # until all fragments are sent.
  383. while self.send_in_progress is not None:
  384. await self.send_in_progress.wait()
  385. # Unfragmented message — this case must be handled first because
  386. # strings and bytes-like objects are iterable.
  387. if isinstance(message, str):
  388. async with self.send_context():
  389. if text is False:
  390. self.protocol.send_binary(message.encode())
  391. else:
  392. self.protocol.send_text(message.encode())
  393. elif isinstance(message, BytesLike):
  394. async with self.send_context():
  395. if text is True:
  396. self.protocol.send_text(message)
  397. else:
  398. self.protocol.send_binary(message)
  399. # Catch a common mistake — passing a dict to send().
  400. elif isinstance(message, Mapping):
  401. raise TypeError("data is a dict-like object")
  402. # Fragmented message — regular iterator.
  403. elif isinstance(message, Iterable):
  404. chunks = iter(message)
  405. try:
  406. chunk = next(chunks)
  407. except StopIteration:
  408. return
  409. assert self.send_in_progress is None
  410. self.send_in_progress = trio.Event()
  411. try:
  412. # First fragment.
  413. if isinstance(chunk, str):
  414. async with self.send_context():
  415. if text is False:
  416. self.protocol.send_binary(chunk.encode(), fin=False)
  417. else:
  418. self.protocol.send_text(chunk.encode(), fin=False)
  419. encode = True
  420. elif isinstance(chunk, BytesLike):
  421. async with self.send_context():
  422. if text is True:
  423. self.protocol.send_text(chunk, fin=False)
  424. else:
  425. self.protocol.send_binary(chunk, fin=False)
  426. encode = False
  427. else:
  428. raise TypeError("iterable must contain bytes or str")
  429. # Other fragments
  430. for chunk in chunks:
  431. if isinstance(chunk, str) and encode:
  432. async with self.send_context():
  433. self.protocol.send_continuation(chunk.encode(), fin=False)
  434. elif isinstance(chunk, BytesLike) and not encode:
  435. async with self.send_context():
  436. self.protocol.send_continuation(chunk, fin=False)
  437. else:
  438. raise TypeError("iterable must contain uniform types")
  439. # Final fragment.
  440. async with self.send_context():
  441. self.protocol.send_continuation(b"", fin=True)
  442. except Exception:
  443. # We're half-way through a fragmented message and we can't
  444. # complete it. This makes the connection unusable.
  445. async with self.send_context():
  446. self.protocol.fail(
  447. CloseCode.INTERNAL_ERROR,
  448. "error in fragmented message",
  449. )
  450. raise
  451. finally:
  452. self.send_in_progress.set()
  453. self.send_in_progress = None
  454. # Fragmented message — async iterator.
  455. elif isinstance(message, AsyncIterable):
  456. achunks = aiter(message)
  457. try:
  458. chunk = await anext(achunks)
  459. except StopAsyncIteration:
  460. return
  461. assert self.send_in_progress is None
  462. self.send_in_progress = trio.Event()
  463. try:
  464. # First fragment.
  465. if isinstance(chunk, str):
  466. if text is False:
  467. async with self.send_context():
  468. self.protocol.send_binary(chunk.encode(), fin=False)
  469. else:
  470. async with self.send_context():
  471. self.protocol.send_text(chunk.encode(), fin=False)
  472. encode = True
  473. elif isinstance(chunk, BytesLike):
  474. if text is True:
  475. async with self.send_context():
  476. self.protocol.send_text(chunk, fin=False)
  477. else:
  478. async with self.send_context():
  479. self.protocol.send_binary(chunk, fin=False)
  480. encode = False
  481. else:
  482. raise TypeError("async iterable must contain bytes or str")
  483. # Other fragments
  484. async for chunk in achunks:
  485. if isinstance(chunk, str) and encode:
  486. async with self.send_context():
  487. self.protocol.send_continuation(chunk.encode(), fin=False)
  488. elif isinstance(chunk, BytesLike) and not encode:
  489. async with self.send_context():
  490. self.protocol.send_continuation(chunk, fin=False)
  491. else:
  492. raise TypeError("async iterable must contain uniform types")
  493. # Final fragment.
  494. async with self.send_context():
  495. self.protocol.send_continuation(b"", fin=True)
  496. except Exception:
  497. # We're half-way through a fragmented message and we can't
  498. # complete it. This makes the connection unusable.
  499. async with self.send_context():
  500. self.protocol.fail(
  501. CloseCode.INTERNAL_ERROR,
  502. "error in fragmented message",
  503. )
  504. raise
  505. finally:
  506. self.send_in_progress.set()
  507. self.send_in_progress = None
  508. else:
  509. raise TypeError("data must be str, bytes, iterable, or async iterable")
  510. async def aclose(
  511. self,
  512. code: CloseCode | int = CloseCode.NORMAL_CLOSURE,
  513. reason: str = "",
  514. ) -> None:
  515. """
  516. Perform the closing handshake.
  517. :meth:`aclose` waits for the other end to complete the handshake and
  518. for the TCP connection to terminate.
  519. :meth:`aclose` is idempotent: it doesn't do anything once the
  520. connection is closed.
  521. Args:
  522. code: WebSocket close code.
  523. reason: WebSocket close reason.
  524. """
  525. try:
  526. # The context manager takes care of waiting for the TCP connection
  527. # to terminate after calling a method that sends a close frame.
  528. async with self.send_context():
  529. if self.send_in_progress is not None:
  530. self.protocol.fail(
  531. CloseCode.INTERNAL_ERROR,
  532. "close during fragmented message",
  533. )
  534. else:
  535. self.protocol.send_close(code, reason)
  536. except ConnectionClosed:
  537. # Ignore ConnectionClosed exceptions raised from send_context().
  538. # They mean that the connection is closed, which was the goal.
  539. pass
  540. # Safety net: enforce the semantics of trio.abc.AsyncResource.aclose().
  541. except BaseException: # pragma: no cover
  542. await trio.aclose_forcefully(self.stream)
  543. async def wait_closed(self) -> None:
  544. """
  545. Wait until the connection is closed.
  546. :meth:`wait_closed` waits for the closing handshake to complete and for
  547. the TCP connection to terminate.
  548. """
  549. await self.stream_closed.wait()
  550. async def ping(
  551. self,
  552. data: DataLike | None = None,
  553. *,
  554. ack_on_close: bool = False,
  555. ) -> trio.Event:
  556. """
  557. Send a Ping_.
  558. .. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2
  559. A ping may serve as a keepalive or as a check that the remote endpoint
  560. received all messages up to this point
  561. Args:
  562. data: Payload of the ping. A :class:`str` will be encoded to UTF-8.
  563. If ``data`` is :obj:`None`, the payload is four random bytes.
  564. ack_on_close: when this option is :obj:`True`, the event will also
  565. be set when the connection is closed. While this avoids getting
  566. stuck waiting for a pong that will never arrive, it requires
  567. checking that the state of the connection is still ``OPEN`` to
  568. confirm that a pong was received, rather than the connection
  569. being closed.
  570. Returns:
  571. An event that will be set when the corresponding pong is received.
  572. You can ignore it if you don't intend to wait.
  573. ::
  574. pong_received = await ws.ping()
  575. # only if you want to wait for the corresponding pong
  576. await pong_received.wait()
  577. Raises:
  578. ConnectionClosed: When the connection is closed.
  579. ConcurrencyError: If another ping was sent with the same data and
  580. the corresponding pong wasn't received yet.
  581. """
  582. if isinstance(data, BytesLike):
  583. data = bytes(data)
  584. elif isinstance(data, str):
  585. data = data.encode()
  586. elif data is not None:
  587. raise TypeError("data must be str or bytes-like")
  588. async with self.send_context():
  589. # Protect against duplicates if a payload is explicitly set.
  590. if data in self.pending_pings:
  591. raise ConcurrencyError("already waiting for a pong with the same data")
  592. # Generate a unique random payload otherwise.
  593. while data is None or data in self.pending_pings:
  594. data = struct.pack("!I", random.getrandbits(32))
  595. pong_received = trio.Event()
  596. ping_timestamp = trio.current_time()
  597. self.pending_pings[data] = (pong_received, ping_timestamp, ack_on_close)
  598. self.protocol.send_ping(data)
  599. return pong_received
  600. async def pong(self, data: DataLike = b"") -> None:
  601. """
  602. Send a Pong_.
  603. .. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3
  604. An unsolicited pong may serve as a unidirectional heartbeat.
  605. Args:
  606. data: Payload of the pong. A :class:`str` will be encoded to UTF-8.
  607. Raises:
  608. ConnectionClosed: When the connection is closed.
  609. """
  610. if isinstance(data, BytesLike):
  611. data = bytes(data)
  612. elif isinstance(data, str):
  613. data = data.encode()
  614. else:
  615. raise TypeError("data must be str or bytes-like")
  616. async with self.send_context():
  617. self.protocol.send_pong(data)
  618. # Private methods
  619. def process_event(self, event: Event) -> None:
  620. """
  621. Process one incoming event.
  622. This method is overridden in subclasses to handle the handshake.
  623. """
  624. assert isinstance(event, Frame)
  625. if event.opcode in DATA_OPCODES:
  626. self.recv_messages.put(event)
  627. if event.opcode is PONG:
  628. self.acknowledge_pings(bytes(event.data))
  629. def acknowledge_pings(self, data: bytes) -> None:
  630. """
  631. Acknowledge pings when receiving a pong.
  632. """
  633. # Ignore unsolicited pong.
  634. if data not in self.pending_pings:
  635. return
  636. pong_timestamp = trio.current_time()
  637. # Sending a pong for only the most recent ping is legal.
  638. # Acknowledge all previous pings too in that case.
  639. ping_id = None
  640. ping_ids = []
  641. for ping_id, (
  642. pong_received,
  643. ping_timestamp,
  644. _ack_on_close,
  645. ) in self.pending_pings.items():
  646. ping_ids.append(ping_id)
  647. pong_received.set()
  648. if ping_id == data:
  649. self.latency = pong_timestamp - ping_timestamp
  650. break
  651. else:
  652. raise AssertionError("solicited pong not found in pings")
  653. # Remove acknowledged pings from self.pending_pings.
  654. for ping_id in ping_ids:
  655. del self.pending_pings[ping_id]
  656. def acknowledge_pending_pings(self) -> None:
  657. """
  658. Acknowledge pending pings when the connection is closed.
  659. """
  660. assert self.protocol.state is CLOSED
  661. for pong_received, _ping_timestamp, ack_on_close in self.pending_pings.values():
  662. if ack_on_close:
  663. pong_received.set()
  664. self.pending_pings.clear()
  665. async def keepalive(self) -> None:
  666. """
  667. Send a Ping frame and wait for a Pong frame at regular intervals.
  668. """
  669. assert self.ping_interval is not None
  670. try:
  671. while True:
  672. # If self.ping_timeout > self.latency > self.ping_interval,
  673. # pings will be sent immediately after receiving pongs.
  674. # The period will be longer than self.ping_interval.
  675. with trio.move_on_after(self.ping_interval - self.latency):
  676. await self.stream_closed.wait()
  677. break
  678. try:
  679. pong_received = await self.ping(ack_on_close=True)
  680. except ConnectionClosed:
  681. break
  682. if self.debug:
  683. self.logger.debug("% sent keepalive ping")
  684. if self.ping_timeout is not None:
  685. with trio.move_on_after(self.ping_timeout) as cancel_scope:
  686. await pong_received.wait()
  687. if self.debug:
  688. self.logger.debug("% received keepalive pong")
  689. if cancel_scope.cancelled_caught:
  690. if self.debug:
  691. self.logger.debug("- timed out waiting for keepalive pong")
  692. async with self.send_context():
  693. self.protocol.fail(
  694. CloseCode.INTERNAL_ERROR,
  695. "keepalive ping timeout",
  696. )
  697. break
  698. except Exception:
  699. self.logger.error("keepalive ping failed", exc_info=True)
  700. def start_keepalive(self) -> None:
  701. """
  702. Run :meth:`keepalive` in a task, unless keepalive is disabled.
  703. """
  704. if self.ping_interval is not None:
  705. self.nursery.start_soon(self.keepalive)
  706. async def recv_events(self) -> None:
  707. """
  708. Read incoming data from the stream and process events.
  709. Run this method in a task as long as the connection is alive.
  710. ``recv_events()`` exits immediately when ``self.stream`` is closed.
  711. """
  712. try:
  713. while True:
  714. try:
  715. # If the assembler buffer is full, block until it drains.
  716. if self.recv_flow_control is not None:
  717. await self.recv_flow_control.wait()
  718. data = await self.stream.receive_some()
  719. except Exception as exc:
  720. if self.debug:
  721. self.logger.debug(
  722. "! error while receiving data",
  723. exc_info=True,
  724. )
  725. # When the closing handshake is initiated by our side,
  726. # recv() may block until send_context() closes the stream.
  727. # In that case, send_context() already set recv_exc.
  728. # Calling set_recv_exc() avoids overwriting it.
  729. self.set_recv_exc(exc)
  730. break
  731. if data == b"":
  732. break
  733. # Feed incoming data to the protocol.
  734. self.protocol.receive_data(data)
  735. # This isn't expected to raise an exception.
  736. events = self.protocol.events_received()
  737. # Write outgoing data to the stream.
  738. try:
  739. await self.send_data()
  740. except Exception as exc:
  741. if self.debug:
  742. self.logger.debug(
  743. "! error while sending data",
  744. exc_info=True,
  745. )
  746. # Similarly to the above, avoid overriding an exception
  747. # set by send_context(), in case of a race condition
  748. # i.e. send_context() closes the transport after recv()
  749. # returns above but before send_data() calls send().
  750. self.set_recv_exc(exc)
  751. break
  752. # If needed, set the close deadline based on the close timeout.
  753. if self.protocol.close_expected():
  754. if self.close_deadline is None and self.close_timeout is not None:
  755. self.close_deadline = trio.current_time() + self.close_timeout
  756. # If self.send_data raised an exception, then events are lost.
  757. # Given that automatic responses write small amounts of data,
  758. # this should be uncommon, so we don't handle the edge case.
  759. for event in events:
  760. # This isn't expected to raise an exception.
  761. self.process_event(event)
  762. # Breaking out of the while True: ... loop means that we believe
  763. # that the stream doesn't work anymore.
  764. # Feed the end of the data stream to the protocol.
  765. self.protocol.receive_eof()
  766. # This isn't expected to raise an exception.
  767. events = self.protocol.events_received()
  768. # There is no error handling because send_data() can only write
  769. # the end of the data stream here and it handles errors itself.
  770. await self.send_data()
  771. # This code path is triggered when receiving an HTTP response
  772. # without a Content-Length header. This is the only case where
  773. # reading until EOF generates an event; all other events have
  774. # a known length. Ignore for coverage measurement because tests
  775. # are in test_client.py rather than test_connection.py.
  776. for event in events: # pragma: no cover
  777. # This isn't expected to raise an exception.
  778. self.process_event(event)
  779. except Exception as exc:
  780. # This branch should never run. It's a safety net in case of bugs.
  781. self.logger.error("unexpected internal error", exc_info=True)
  782. self.set_recv_exc(exc)
  783. finally:
  784. # This isn't expected to raise an exception.
  785. await self.close_stream()
  786. def pause_reading(self) -> None:
  787. """Pause recv_events() until resume_reading() is called."""
  788. self.recv_flow_control = trio.Event()
  789. def resume_reading(self) -> None:
  790. """Resume recv_events() after pause_reading() was called."""
  791. assert self.recv_flow_control is not None
  792. self.recv_flow_control.set()
  793. self.recv_flow_control = None
  794. @contextlib.asynccontextmanager
  795. async def send_context(
  796. self,
  797. *,
  798. expected_state: State = OPEN, # CONNECTING during the opening handshake
  799. ) -> AsyncIterator[None]:
  800. """
  801. Create a context for writing to the connection from user code.
  802. On entry, :meth:`send_context` checks that the connection is open; on
  803. exit, it writes outgoing data to the socket::
  804. async with self.send_context():
  805. self.protocol.send_text(message.encode())
  806. When the connection isn't open on entry, when the connection is expected
  807. to close on exit, or when an unexpected error happens, terminating the
  808. connection, :meth:`send_context` waits until the connection is closed
  809. then raises :exc:`~websockets.exceptions.ConnectionClosed`.
  810. """
  811. # Should we wait until the connection is closed?
  812. wait_for_close = False
  813. # Should we close the stream and raise ConnectionClosed?
  814. raise_close_exc = False
  815. # What exception should we chain ConnectionClosed to?
  816. original_exc: BaseException | None = None
  817. if self.protocol.state is expected_state:
  818. # Let the caller interact with the protocol.
  819. try:
  820. yield
  821. except (ProtocolError, ConcurrencyError):
  822. # The protocol state wasn't changed. Exit immediately.
  823. raise
  824. except Exception as exc:
  825. self.logger.error("unexpected internal error", exc_info=True)
  826. # This branch should never run. It's a safety net in case of
  827. # bugs. Since we don't know what happened, we will close the
  828. # connection and raise the exception to the caller.
  829. wait_for_close = False
  830. raise_close_exc = True
  831. original_exc = exc
  832. else:
  833. # Check if the connection is expected to close soon.
  834. if self.protocol.close_expected():
  835. wait_for_close = True
  836. # Set the close deadline based on the close timeout.
  837. # Since we tested earlier that protocol.state is OPEN
  838. # (or CONNECTING), self.close_deadline is still None.
  839. assert self.close_deadline is None
  840. if self.close_timeout is not None:
  841. self.close_deadline = trio.current_time() + self.close_timeout
  842. # Write outgoing data to the socket with flow control.
  843. try:
  844. await self.send_data()
  845. except Exception as exc:
  846. if self.debug:
  847. self.logger.debug("! error while sending data", exc_info=True)
  848. # While the only expected exception here is OSError,
  849. # other exceptions would be treated identically.
  850. wait_for_close = False
  851. raise_close_exc = True
  852. original_exc = exc
  853. else: # self.protocol.state is not expected_state
  854. # Minor layering violation: we assume that the connection
  855. # will be closing soon if it isn't in the expected state.
  856. wait_for_close = True
  857. # Calculate close_deadline if it wasn't set yet.
  858. if self.close_deadline is None and self.close_timeout is not None:
  859. self.close_deadline = trio.current_time() + self.close_timeout
  860. raise_close_exc = True
  861. # If the connection is expected to close soon and the close timeout
  862. # elapses, close the socket to terminate the connection.
  863. if wait_for_close:
  864. if self.close_deadline is not None:
  865. with trio.move_on_at(self.close_deadline) as cancel_scope:
  866. await self.stream_closed.wait()
  867. if cancel_scope.cancelled_caught:
  868. # There's no risk to overwrite another error because
  869. # original_exc is never set when wait_for_close is True.
  870. assert original_exc is None
  871. original_exc = TimeoutError("timed out while closing connection")
  872. # Set recv_exc before closing the transport in order to get
  873. # proper exception reporting.
  874. raise_close_exc = True
  875. self.set_recv_exc(original_exc)
  876. else:
  877. await self.stream_closed.wait()
  878. # If an error occurred, close the transport to terminate the connection and
  879. # raise an exception.
  880. if raise_close_exc:
  881. await self.close_stream()
  882. raise self.protocol.close_exc from original_exc
  883. async def send_data(self) -> None:
  884. """
  885. Send outgoing data.
  886. """
  887. # Serialize calls to send_all().
  888. async with self.send_lock:
  889. for data in self.protocol.data_to_send():
  890. if data:
  891. await self.stream.send_all(data)
  892. else:
  893. # Half-close the TCP connection when possible i.e. no TLS.
  894. if isinstance(self.stream, trio.abc.HalfCloseableStream):
  895. if self.debug:
  896. self.logger.debug("x half-closing TCP connection")
  897. try:
  898. await self.stream.send_eof()
  899. except Exception: # pragma: no cover
  900. pass
  901. # Else, close the TCP connection.
  902. else: # pragma: no cover
  903. if self.debug:
  904. self.logger.debug("x closing TCP connection")
  905. await self.stream.aclose()
  906. def set_recv_exc(self, exc: BaseException | None) -> None:
  907. """
  908. Set recv_exc, if not set yet.
  909. """
  910. if self.recv_exc is None:
  911. self.recv_exc = exc
  912. async def close_stream(self) -> None:
  913. """
  914. Shutdown and close stream. Close message assembler.
  915. Calling close_stream() guarantees that recv_events() terminates. Indeed,
  916. recv_events() may block only on stream.recv() or on recv_messages.put().
  917. """
  918. # Close the stream.
  919. await self.stream.aclose()
  920. # Calling protocol.receive_eof() is safe because it's idempotent.
  921. # This guarantees that the protocol state becomes CLOSED.
  922. self.protocol.receive_eof()
  923. assert self.protocol.state is CLOSED
  924. # Abort recv() with a ConnectionClosed exception.
  925. self.recv_messages.close()
  926. # Acknowledge pings sent with the ack_on_close option.
  927. self.acknowledge_pending_pings()
  928. # Unblock coroutines waiting on self.stream_closed.
  929. self.stream_closed.set()
  930. # broadcast() is defined in the connection module even though it's primarily
  931. # used by servers and documented in the server module because it works with
  932. # client connections too and because it's easier to test together with the
  933. # Connection class.
  934. async def broadcast(
  935. connections: Iterable[Connection],
  936. message: DataLike,
  937. *,
  938. raise_exceptions: bool = False,
  939. text: bool | None = None,
  940. ) -> None:
  941. """
  942. Broadcast a message to several WebSocket connections.
  943. A string (:class:`str`) is sent as a Text_ frame. A bytestring or bytes-like
  944. object (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) is sent
  945. as a Binary_ frame.
  946. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  947. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  948. You may override this behavior with the ``text`` argument:
  949. * Set ``text=True`` to send an UTF-8 bytestring or bytes-like object
  950. (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) in a
  951. Text_ frame. This improves performance when the message is already
  952. UTF-8 encoded, for example if the message contains JSON and you're
  953. using a JSON library that produces a bytestring.
  954. * Set ``text=False`` to send a string (:class:`str`) in a Binary_
  955. frame. This may be useful for servers that expect binary frames
  956. instead of text frames.
  957. :func:`broadcast` is equivalent to calling
  958. :meth:`~websockets.trio.connection.Connection.send` for each connection.
  959. It returns when all messages have been sent.
  960. Unlike :meth:`~websockets.trio.connection.Connection.send`,
  961. :func:`broadcast` doesn't support sending fragmented messages. Indeed,
  962. fragmentation is useful for sending large messages without buffering them in
  963. memory, while :func:`broadcast` buffers one copy per connection as fast as
  964. possible.
  965. :func:`broadcast` skips connections that aren't open in order to avoid
  966. errors on connections where the closing handshake is in progress.
  967. :func:`broadcast` ignores failures to write the message on some connections.
  968. It continues writing to other connections. You may set ``raise_exceptions``
  969. to :obj:`True` to record failures and raise all exceptions in a :pep:`654`
  970. :exc:`ExceptionGroup`.
  971. While :func:`broadcast` makes more sense for servers, it works identically
  972. with clients, if you have a use case for opening connections to many servers
  973. and broadcasting a message to them.
  974. Args:
  975. websockets: WebSocket connections to which the message will be sent.
  976. message: Message to send.
  977. raise_exceptions: Whether to raise an exception in case of failures.
  978. text: Force sending in Text_ or Binary_ frames.
  979. Raises:
  980. TypeError: If ``message`` doesn't have a supported type.
  981. """
  982. if isinstance(message, str):
  983. send_method = "send_binary" if text is False else "send_text"
  984. message = message.encode()
  985. elif isinstance(message, BytesLike):
  986. send_method = "send_text" if text is True else "send_binary"
  987. else:
  988. raise TypeError("data must be str or bytes")
  989. if raise_exceptions:
  990. exceptions: list[Exception] = []
  991. async def send_message(connection: Connection) -> None:
  992. exception: Exception
  993. if connection.protocol.state is not OPEN:
  994. return
  995. if connection.send_in_progress is not None:
  996. if raise_exceptions:
  997. exception = ConcurrencyError("sending a fragmented message")
  998. exceptions.append(exception)
  999. else:
  1000. connection.logger.warning(
  1001. "skipped broadcast: sending a fragmented message",
  1002. )
  1003. return
  1004. try:
  1005. # Call connection.protocol.send_text or send_binary.
  1006. # Either way, message is already converted to bytes.
  1007. getattr(connection.protocol, send_method)(message)
  1008. await connection.send_data()
  1009. except Exception as write_exception:
  1010. if raise_exceptions:
  1011. exception = RuntimeError("failed to write message")
  1012. exception.__cause__ = write_exception
  1013. exceptions.append(exception)
  1014. else:
  1015. connection.logger.warning(
  1016. "skipped broadcast: failed to write message: %s",
  1017. traceback.format_exception_only(write_exception)[0].strip(),
  1018. )
  1019. async with trio.open_nursery() as nursery:
  1020. for connection in connections:
  1021. nursery.start_soon(send_message, connection)
  1022. if raise_exceptions and exceptions:
  1023. raise ExceptionGroup("skipped broadcast", exceptions)
  1024. # Pretend that broadcast is actually defined in the server module.
  1025. broadcast.__module__ = "websockets.asyncio.server"