You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

226 lines
6.2 KiB

  1. from __future__ import annotations
  2. import struct
  3. from collections.abc import Awaitable, Sequence
  4. from typing import Any, Callable, NamedTuple
  5. from .. import extensions, frames
  6. from ..exceptions import PayloadTooBig, ProtocolError
  7. from ..frames import BytesLike
  8. from ..typing import Data
  9. try:
  10. from ..speedups import apply_mask
  11. except ImportError:
  12. from ..utils import apply_mask
  13. class Frame(NamedTuple):
  14. fin: bool
  15. opcode: frames.Opcode
  16. data: bytes
  17. rsv1: bool = False
  18. rsv2: bool = False
  19. rsv3: bool = False
  20. @property
  21. def new_frame(self) -> frames.Frame:
  22. return frames.Frame(
  23. self.opcode,
  24. self.data,
  25. self.fin,
  26. self.rsv1,
  27. self.rsv2,
  28. self.rsv3,
  29. )
  30. def __str__(self) -> str:
  31. return str(self.new_frame)
  32. def check(self) -> None:
  33. return self.new_frame.check()
  34. @classmethod
  35. async def read(
  36. cls,
  37. reader: Callable[[int], Awaitable[bytes]],
  38. *,
  39. mask: bool,
  40. max_size: int | None = None,
  41. extensions: Sequence[extensions.Extension] | None = None,
  42. ) -> Frame:
  43. """
  44. Read a WebSocket frame.
  45. Args:
  46. reader: Coroutine that reads exactly the requested number of
  47. bytes, unless the end of file is reached.
  48. mask: Whether the frame should be masked i.e. whether the read
  49. happens on the server side.
  50. max_size: Maximum payload size in bytes.
  51. extensions: List of extensions, applied in reverse order.
  52. Raises:
  53. PayloadTooBig: If the frame exceeds ``max_size``.
  54. ProtocolError: If the frame contains incorrect values.
  55. """
  56. # Read the header.
  57. data = await reader(2)
  58. head1, head2 = struct.unpack("!BB", data)
  59. # While not Pythonic, this is marginally faster than calling bool().
  60. fin = True if head1 & 0b10000000 else False
  61. rsv1 = True if head1 & 0b01000000 else False
  62. rsv2 = True if head1 & 0b00100000 else False
  63. rsv3 = True if head1 & 0b00010000 else False
  64. try:
  65. opcode = frames.Opcode(head1 & 0b00001111)
  66. except ValueError as exc:
  67. raise ProtocolError("invalid opcode") from exc
  68. if (True if head2 & 0b10000000 else False) != mask:
  69. raise ProtocolError("incorrect masking")
  70. length = head2 & 0b01111111
  71. if length == 126:
  72. data = await reader(2)
  73. (length,) = struct.unpack("!H", data)
  74. elif length == 127:
  75. data = await reader(8)
  76. (length,) = struct.unpack("!Q", data)
  77. if max_size is not None and length > max_size:
  78. raise PayloadTooBig(length, max_size)
  79. if mask:
  80. mask_bits = await reader(4)
  81. # Read the data.
  82. data = await reader(length)
  83. if mask:
  84. data = apply_mask(data, mask_bits)
  85. new_frame = frames.Frame(opcode, data, fin, rsv1, rsv2, rsv3)
  86. if extensions is None:
  87. extensions = []
  88. for extension in reversed(extensions):
  89. new_frame = extension.decode(new_frame, max_size=max_size)
  90. new_frame.check()
  91. return cls(
  92. new_frame.fin,
  93. new_frame.opcode,
  94. new_frame.data,
  95. new_frame.rsv1,
  96. new_frame.rsv2,
  97. new_frame.rsv3,
  98. )
  99. def write(
  100. self,
  101. write: Callable[[bytes], Any],
  102. *,
  103. mask: bool,
  104. extensions: Sequence[extensions.Extension] | None = None,
  105. ) -> None:
  106. """
  107. Write a WebSocket frame.
  108. Args:
  109. frame: Frame to write.
  110. write: Function that writes bytes.
  111. mask: Whether the frame should be masked i.e. whether the write
  112. happens on the client side.
  113. extensions: List of extensions, applied in order.
  114. Raises:
  115. ProtocolError: If the frame contains incorrect values.
  116. """
  117. # The frame is written in a single call to write in order to prevent
  118. # TCP fragmentation. See #68 for details. This also makes it safe to
  119. # send frames concurrently from multiple coroutines.
  120. write(self.new_frame.serialize(mask=mask, extensions=extensions))
  121. def prepare_data(data: Data) -> tuple[int, bytes]:
  122. """
  123. Convert a string or byte-like object to an opcode and a bytes-like object.
  124. This function is designed for data frames.
  125. If ``data`` is a :class:`str`, return ``OP_TEXT`` and a :class:`bytes`
  126. object encoding ``data`` in UTF-8.
  127. If ``data`` is a bytes-like object, return ``OP_BINARY`` and a bytes-like
  128. object.
  129. Raises:
  130. TypeError: If ``data`` doesn't have a supported type.
  131. """
  132. if isinstance(data, str):
  133. return frames.Opcode.TEXT, data.encode()
  134. elif isinstance(data, BytesLike):
  135. return frames.Opcode.BINARY, data
  136. else:
  137. raise TypeError("data must be str or bytes-like")
  138. def prepare_ctrl(data: Data) -> bytes:
  139. """
  140. Convert a string or byte-like object to bytes.
  141. This function is designed for ping and pong frames.
  142. If ``data`` is a :class:`str`, return a :class:`bytes` object encoding
  143. ``data`` in UTF-8.
  144. If ``data`` is a bytes-like object, return a :class:`bytes` object.
  145. Raises:
  146. TypeError: If ``data`` doesn't have a supported type.
  147. """
  148. if isinstance(data, str):
  149. return data.encode()
  150. elif isinstance(data, BytesLike):
  151. return bytes(data)
  152. else:
  153. raise TypeError("data must be str or bytes-like")
  154. # Backwards compatibility with previously documented public APIs
  155. encode_data = prepare_ctrl
  156. # Backwards compatibility with previously documented public APIs
  157. from ..frames import Close # noqa: E402 F401, I001
  158. def parse_close(data: bytes) -> tuple[int, str]:
  159. """
  160. Parse the payload from a close frame.
  161. Returns:
  162. Close code and reason.
  163. Raises:
  164. ProtocolError: If data is ill-formed.
  165. UnicodeDecodeError: If the reason isn't valid UTF-8.
  166. """
  167. close = Close.parse(data)
  168. return close.code, close.reason
  169. def serialize_close(code: int, reason: str) -> bytes:
  170. """
  171. Serialize the payload for a close frame.
  172. """
  173. return Close(code, reason).serialize()