Debian Codegraph @

src/websockets/trio/messages.py

raw
1 from __future__ import annotations
2
3 import codecs
4 import math
5 from collections.abc import AsyncIterator
6 from typing import Any, Callable, Literal, overload
7
8 import trio
9
10 from ..exceptions import ConcurrencyError
11 from ..frames import BINARY, CONT, TEXT, Frame
12 from ..typing import Data
13
14
15 __all__ = ["Assembler"]
16
17 UTF8Decoder = codecs.getincrementaldecoder("utf-8")
18
19
20 class Assembler:
21 """
22 Assemble messages from frames.
23
24 :class:`Assembler` expects only data frames. The stream of frames must
25 respect the protocol; if it doesn't, the behavior is undefined.
26
27 Args:
28 pause: Called when the buffer of frames goes above the high water mark;
29 should pause reading from the network.
30 resume: Called when the buffer of frames goes below the low water mark;
31 should resume reading from the network.
32
33 """
34
35 def __init__(
36 self,
37 high: int | None = None,
38 low: int | None = None,
39 pause: Callable[[], Any] = lambda: None,
40 resume: Callable[[], Any] = lambda: None,
41 ) -> None:
42 # Queue of incoming frames.
43 self.send_frames: trio.MemorySendChannel[Frame]
44 self.recv_frames: trio.MemoryReceiveChannel[Frame]
45 self.send_frames, self.recv_frames = trio.open_memory_channel(math.inf)
46
47 # We cannot put a hard limit on the size of the queue because a single
48 # call to Protocol.data_received() could produce thousands of frames,
49 # which must be buffered. Instead, we pause reading when the buffer goes
50 # above the high limit and we resume when it goes under the low limit.
51 if high is not None and low is None:
52 low = high // 4
53 if high is None and low is not None:
54 high = low * 4
55 if high is not None and low is not None:
56 if low < 0:
57 raise ValueError("low must be positive or equal to zero")
58 if high < low:
59 raise ValueError("high must be greater than or equal to low")
60 self.high, self.low = high, low
61 self.pause = pause
62 self.resume = resume
63 self.paused = False
64
65 # This flag prevents concurrent calls to get() by user code.
66 self.get_in_progress = False
67
68 # This flag marks the end of the connection.
69 self.closed = False
70
71 @overload
72 async def get(self, decode: Literal[True]) -> str: ...
73
74 @overload
75 async def get(self, decode: Literal[False]) -> bytes: ...
76
77 @overload
78 async def get(self, decode: bool | None = None) -> Data: ...
79
80 async def get(self, decode: bool | None = None) -> Data:
81 """
82 Read the next message.
83
84 :meth:`get` returns a single :class:`str` or :class:`bytes`.
85
86 If the message is fragmented, :meth:`get` waits until the last frame is
87 received, then it reassembles the message and returns it. To receive
88 messages frame by frame, use :meth:`get_iter` instead.
89
90 Args:
91 decode: :obj:`False` disables UTF-8 decoding of text frames and
92 returns :class:`bytes`. :obj:`True` forces UTF-8 decoding of
93 binary frames and returns :class:`str`.
94
95 Raises:
96 EOFError: If the stream of frames has ended.
97 UnicodeDecodeError: If a text frame contains invalid UTF-8.
98 ConcurrencyError: If two coroutines run :meth:`get` or
99 :meth:`get_iter` concurrently.
100
101 """
102 if self.get_in_progress:
103 raise ConcurrencyError("get() or get_iter() is already running")
104 self.get_in_progress = True
105
106 # Locking with get_in_progress prevents concurrent execution
107 # until get() fetches a complete message or is canceled.
108
109 try:
110 # Fetch the first frame.
111 try:
112 frame = await self.recv_frames.receive()
113 except trio.EndOfChannel:
114 raise EOFError("stream of frames ended")
115 self.maybe_resume()
116 assert frame.opcode is TEXT or frame.opcode is BINARY
117 if decode is None:
118 decode = frame.opcode is TEXT
119 frames = [frame]
120
121 # Fetch subsequent frames for fragmented messages.
122 while not frame.fin:
123 try:
124 frame = await self.recv_frames.receive()
125 except trio.Cancelled:
126 # Put frames already received back into the queue
127 # so that future calls to get() can return them.
128 # Bypass the statistics() method for performance.
129 state = self.send_frames._state
130 assert not state.receive_tasks, "no task should receive"
131 assert not state.data, "queue should be empty"
132 for frame in frames:
133 self.send_frames.send_nowait(frame)
134 raise
135 except trio.EndOfChannel:
136 raise EOFError("stream of frames ended")
137 self.maybe_resume()
138 assert frame.opcode is CONT
139 frames.append(frame)
140
141 finally:
142 self.get_in_progress = False
143
144 # This converts frame.data to bytes when it's a bytearray.
145 data = b"".join(frame.data for frame in frames)
146 if decode:
147 return data.decode()
148 else:
149 return data
150
151 @overload
152 def get_iter(self, decode: Literal[True]) -> AsyncIterator[str]: ...
153
154 @overload
155 def get_iter(self, decode: Literal[False]) -> AsyncIterator[bytes]: ...
156
157 @overload
158 def get_iter(self, decode: bool | None = None) -> AsyncIterator[Data]: ...
159
160 async def get_iter(self, decode: bool | None = None) -> AsyncIterator[Data]:
161 """
162 Stream the next message.
163
164 Iterating the return value of :meth:`get_iter` asynchronously yields a
165 :class:`str` or :class:`bytes` for each frame in the message.
166
167 The iterator must be fully consumed before calling :meth:`get_iter` or
168 :meth:`get` again. Else, :exc:`ConcurrencyError` is raised.
169
170 This method only makes sense for fragmented messages. If messages aren't
171 fragmented, use :meth:`get` instead.
172
173 Args:
174 decode: :obj:`False` disables UTF-8 decoding of text frames and
175 returns :class:`bytes`. :obj:`True` forces UTF-8 decoding of
176 binary frames and returns :class:`str`.
177
178 Raises:
179 EOFError: If the stream of frames has ended.
180 UnicodeDecodeError: If a text frame contains invalid UTF-8.
181 ConcurrencyError: If two coroutines run :meth:`get` or
182 :meth:`get_iter` concurrently.
183
184 """
185 if self.get_in_progress:
186 raise ConcurrencyError("get() or get_iter() is already running")
187 self.get_in_progress = True
188
189 # Locking with get_in_progress prevents concurrent execution
190 # until get_iter() fetches a complete message or is canceled.
191
192 # If get_iter() raises an exception e.g. in decoder.decode(),
193 # get_in_progress remains set and the connection becomes unusable.
194
195 # Yield the first frame.
196 try:
197 frame = await self.recv_frames.receive()
198 except trio.Cancelled:
199 self.get_in_progress = False
200 raise
201 except trio.EndOfChannel:
202 raise EOFError("stream of frames ended")
203 self.maybe_resume()
204 assert frame.opcode is TEXT or frame.opcode is BINARY
205 if decode is None:
206 decode = frame.opcode is TEXT
207 if decode:
208 decoder = UTF8Decoder()
209 yield decoder.decode(frame.data, frame.fin)
210 else:
211 # Convert to bytes when frame.data is a bytearray.
212 yield bytes(frame.data)
213
214 # Yield subsequent frames for fragmented messages.
215 while not frame.fin:
216 # We cannot handle trio.Cancelled because we don't buffer
217 # previous fragments — we're streaming them. Canceling get_iter()
218 # here will leave the assembler in a stuck state. Future calls to
219 # get() or get_iter() will raise ConcurrencyError.
220 try:
221 frame = await self.recv_frames.receive()
222 except trio.EndOfChannel:
223 raise EOFError("stream of frames ended")
224 self.maybe_resume()
225 assert frame.opcode is CONT
226 if decode:
227 yield decoder.decode(frame.data, frame.fin)
228 else:
229 # Convert to bytes when frame.data is a bytearray.
230 yield bytes(frame.data)
231
232 self.get_in_progress = False
233
234 def put(self, frame: Frame) -> None:
235 """
236 Add ``frame`` to the next message.
237
238 Raises:
239 EOFError: If the stream of frames has ended.
240
241 """
242 if self.closed:
243 raise EOFError("stream of frames ended")
244
245 self.send_frames.send_nowait(frame)
246 self.maybe_pause()
247
248 def maybe_pause(self) -> None:
249 """Pause the writer if queue is above the high water mark."""
250 # Skip if flow control is disabled.
251 if self.high is None:
252 return
253
254 # Bypass the statistics() method for performance.
255 # Check for "> high" to support high = 0.
256 if len(self.send_frames._state.data) > self.high and not self.paused:
257 self.paused = True
258 self.pause()
259
260 def maybe_resume(self) -> None:
261 """Resume the writer if queue is below the low water mark."""
262 # Skip if flow control is disabled.
263 if self.low is None:
264 return
265
266 # Bypass the statistics() method for performance.
267 # Check for "<= low" to support low = 0.
268 if len(self.send_frames._state.data) <= self.low and self.paused:
269 self.paused = False
270 self.resume()
271
272 def close(self) -> None:
273 """
274 End the stream of frames.
275
276 Calling :meth:`close` concurrently with :meth:`get`, :meth:`get_iter`,
277 or :meth:`put` is safe. They will raise :exc:`EOFError`.
278
279 """
280 if self.closed:
281 return
282
283 self.closed = True
284
285 # Unblock get() or get_iter().
286 self.send_frames.close()
287
288 # Break reference cycle to allow immediate garbage collection:
289 # Connection -> Assembler -> bound methods -> Connection.
290 self.pause = lambda: None
291 self.resume = lambda: None