Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

474 рядки
12 KiB

  1. """
  2. :mod:`websockets.exceptions` defines the following hierarchy of exceptions.
  3. * :exc:`WebSocketException`
  4. * :exc:`ConnectionClosed`
  5. * :exc:`ConnectionClosedOK`
  6. * :exc:`ConnectionClosedError`
  7. * :exc:`InvalidURI`
  8. * :exc:`InvalidProxy`
  9. * :exc:`InvalidHandshake`
  10. * :exc:`SecurityError`
  11. * :exc:`ProxyError`
  12. * :exc:`InvalidProxyMessage`
  13. * :exc:`InvalidProxyStatus`
  14. * :exc:`InvalidMessage`
  15. * :exc:`InvalidStatus`
  16. * :exc:`InvalidStatusCode` (legacy)
  17. * :exc:`InvalidHeader`
  18. * :exc:`InvalidHeaderFormat`
  19. * :exc:`InvalidHeaderValue`
  20. * :exc:`InvalidOrigin`
  21. * :exc:`InvalidUpgrade`
  22. * :exc:`NegotiationError`
  23. * :exc:`DuplicateParameter`
  24. * :exc:`InvalidParameterName`
  25. * :exc:`InvalidParameterValue`
  26. * :exc:`AbortHandshake` (legacy)
  27. * :exc:`RedirectHandshake` (legacy)
  28. * :exc:`ProtocolError` (Sans-I/O)
  29. * :exc:`PayloadTooBig` (Sans-I/O)
  30. * :exc:`InvalidState` (Sans-I/O)
  31. * :exc:`ConcurrencyError`
  32. """
  33. from __future__ import annotations
  34. import warnings
  35. from .imports import lazy_import
  36. __all__ = [
  37. "WebSocketException",
  38. "ConnectionClosed",
  39. "ConnectionClosedOK",
  40. "ConnectionClosedError",
  41. "InvalidURI",
  42. "InvalidProxy",
  43. "InvalidHandshake",
  44. "SecurityError",
  45. "ProxyError",
  46. "InvalidProxyMessage",
  47. "InvalidProxyStatus",
  48. "InvalidMessage",
  49. "InvalidStatus",
  50. "InvalidHeader",
  51. "InvalidHeaderFormat",
  52. "InvalidHeaderValue",
  53. "InvalidOrigin",
  54. "InvalidUpgrade",
  55. "NegotiationError",
  56. "DuplicateParameter",
  57. "InvalidParameterName",
  58. "InvalidParameterValue",
  59. "ProtocolError",
  60. "PayloadTooBig",
  61. "InvalidState",
  62. "ConcurrencyError",
  63. ]
  64. class WebSocketException(Exception):
  65. """
  66. Base class for all exceptions defined by websockets.
  67. """
  68. class ConnectionClosed(WebSocketException):
  69. """
  70. Raised when trying to interact with a closed connection.
  71. Attributes:
  72. rcvd: If a close frame was received, its code and reason are available
  73. in ``rcvd.code`` and ``rcvd.reason``.
  74. sent: If a close frame was sent, its code and reason are available
  75. in ``sent.code`` and ``sent.reason``.
  76. rcvd_then_sent: If close frames were received and sent, this attribute
  77. tells in which order this happened, from the perspective of this
  78. side of the connection.
  79. """
  80. def __init__(
  81. self,
  82. rcvd: frames.Close | None,
  83. sent: frames.Close | None,
  84. rcvd_then_sent: bool | None = None,
  85. ) -> None:
  86. self.rcvd = rcvd
  87. self.sent = sent
  88. self.rcvd_then_sent = rcvd_then_sent
  89. assert (self.rcvd_then_sent is None) == (self.rcvd is None or self.sent is None)
  90. def __str__(self) -> str:
  91. if self.rcvd is None:
  92. if self.sent is None:
  93. return "no close frame received or sent"
  94. else:
  95. return f"sent {self.sent}; no close frame received"
  96. else:
  97. if self.sent is None:
  98. return f"received {self.rcvd}; no close frame sent"
  99. else:
  100. if self.rcvd_then_sent:
  101. return f"received {self.rcvd}; then sent {self.sent}"
  102. else:
  103. return f"sent {self.sent}; then received {self.rcvd}"
  104. # code and reason attributes are provided for backwards-compatibility
  105. @property
  106. def code(self) -> int:
  107. warnings.warn( # deprecated in 13.1 - 2024-09-21
  108. "ConnectionClosed.code is deprecated; "
  109. "use Protocol.close_code or ConnectionClosed.rcvd.code",
  110. DeprecationWarning,
  111. )
  112. if self.rcvd is None:
  113. return frames.CloseCode.ABNORMAL_CLOSURE
  114. return self.rcvd.code
  115. @property
  116. def reason(self) -> str:
  117. warnings.warn( # deprecated in 13.1 - 2024-09-21
  118. "ConnectionClosed.reason is deprecated; "
  119. "use Protocol.close_reason or ConnectionClosed.rcvd.reason",
  120. DeprecationWarning,
  121. )
  122. if self.rcvd is None:
  123. return ""
  124. return self.rcvd.reason
  125. class ConnectionClosedOK(ConnectionClosed):
  126. """
  127. Like :exc:`ConnectionClosed`, when the connection terminated properly.
  128. A close code with code 1000 (OK) or 1001 (going away) or without a code was
  129. received and sent.
  130. """
  131. class ConnectionClosedError(ConnectionClosed):
  132. """
  133. Like :exc:`ConnectionClosed`, when the connection terminated with an error.
  134. A close frame with a code other than 1000 (OK) or 1001 (going away) was
  135. received or sent, or the closing handshake didn't complete properly.
  136. """
  137. class InvalidURI(WebSocketException):
  138. """
  139. Raised when connecting to a URI that isn't a valid WebSocket URI.
  140. """
  141. def __init__(self, uri: str, msg: str) -> None:
  142. self.uri = uri
  143. self.msg = msg
  144. def __str__(self) -> str:
  145. return f"{self.uri} isn't a valid URI: {self.msg}"
  146. class InvalidProxy(WebSocketException):
  147. """
  148. Raised when connecting via a proxy that isn't valid.
  149. """
  150. def __init__(self, proxy: str, msg: str) -> None:
  151. self.proxy = proxy
  152. self.msg = msg
  153. def __str__(self) -> str:
  154. return f"{self.proxy} isn't a valid proxy: {self.msg}"
  155. class InvalidHandshake(WebSocketException):
  156. """
  157. Base class for exceptions raised when the opening handshake fails.
  158. """
  159. class SecurityError(InvalidHandshake):
  160. """
  161. Raised when a handshake request or response breaks a security rule.
  162. Security limits can be configured with :doc:`environment variables
  163. <../reference/variables>`.
  164. """
  165. class ProxyError(InvalidHandshake):
  166. """
  167. Raised when failing to connect to a proxy.
  168. """
  169. class InvalidProxyMessage(ProxyError):
  170. """
  171. Raised when an HTTP proxy response is malformed.
  172. """
  173. class InvalidProxyStatus(ProxyError):
  174. """
  175. Raised when an HTTP proxy rejects the connection.
  176. """
  177. def __init__(self, response: http11.Response) -> None:
  178. self.response = response
  179. def __str__(self) -> str:
  180. return f"proxy rejected connection: HTTP {self.response.status_code:d}"
  181. class InvalidMessage(InvalidHandshake):
  182. """
  183. Raised when a handshake request or response is malformed.
  184. """
  185. class InvalidStatus(InvalidHandshake):
  186. """
  187. Raised when a handshake response rejects the WebSocket upgrade.
  188. """
  189. def __init__(self, response: http11.Response) -> None:
  190. self.response = response
  191. def __str__(self) -> str:
  192. return (
  193. f"server rejected WebSocket connection: HTTP {self.response.status_code:d}"
  194. )
  195. class InvalidHeader(InvalidHandshake):
  196. """
  197. Raised when an HTTP header doesn't have a valid format or value.
  198. """
  199. def __init__(self, name: str, value: str | None = None) -> None:
  200. self.name = name
  201. self.value = value
  202. def __str__(self) -> str:
  203. if self.value is None:
  204. return f"missing {self.name} header"
  205. elif self.value == "":
  206. return f"empty {self.name} header"
  207. else:
  208. return f"invalid {self.name} header: {self.value}"
  209. class InvalidHeaderFormat(InvalidHeader):
  210. """
  211. Raised when an HTTP header cannot be parsed.
  212. The format of the header doesn't match the grammar for that header.
  213. """
  214. def __init__(self, name: str, error: str, header: str, pos: int) -> None:
  215. super().__init__(name, f"{error} at {pos} in {header}")
  216. class InvalidHeaderValue(InvalidHeader):
  217. """
  218. Raised when an HTTP header has a wrong value.
  219. The format of the header is correct but the value isn't acceptable.
  220. """
  221. class InvalidOrigin(InvalidHeader):
  222. """
  223. Raised when the Origin header in a request isn't allowed.
  224. """
  225. def __init__(self, origin: str | None) -> None:
  226. super().__init__("Origin", origin)
  227. class InvalidUpgrade(InvalidHeader):
  228. """
  229. Raised when the Upgrade or Connection header isn't correct.
  230. """
  231. class NegotiationError(InvalidHandshake):
  232. """
  233. Raised when negotiating an extension or a subprotocol fails.
  234. """
  235. class DuplicateParameter(NegotiationError):
  236. """
  237. Raised when a parameter name is repeated in an extension header.
  238. """
  239. def __init__(self, name: str) -> None:
  240. self.name = name
  241. def __str__(self) -> str:
  242. return f"duplicate parameter: {self.name}"
  243. class InvalidParameterName(NegotiationError):
  244. """
  245. Raised when a parameter name in an extension header is invalid.
  246. """
  247. def __init__(self, name: str) -> None:
  248. self.name = name
  249. def __str__(self) -> str:
  250. return f"invalid parameter name: {self.name}"
  251. class InvalidParameterValue(NegotiationError):
  252. """
  253. Raised when a parameter value in an extension header is invalid.
  254. """
  255. def __init__(self, name: str, value: str | None) -> None:
  256. self.name = name
  257. self.value = value
  258. def __str__(self) -> str:
  259. if self.value is None:
  260. return f"missing value for parameter {self.name}"
  261. elif self.value == "":
  262. return f"empty value for parameter {self.name}"
  263. else:
  264. return f"invalid value for parameter {self.name}: {self.value}"
  265. class ProtocolError(WebSocketException):
  266. """
  267. Raised when receiving or sending a frame that breaks the protocol.
  268. The Sans-I/O implementation raises this exception when:
  269. * receiving or sending a frame that contains invalid data;
  270. * receiving or sending an invalid sequence of frames.
  271. """
  272. class PayloadTooBig(WebSocketException):
  273. """
  274. Raised when parsing a frame with a payload that exceeds the maximum size.
  275. The Sans-I/O layer uses this exception internally. It doesn't bubble up to
  276. the I/O layer.
  277. The :meth:`~websockets.extensions.Extension.decode` method of extensions
  278. must raise :exc:`PayloadTooBig` if decoding a frame would exceed the limit.
  279. """
  280. def __init__(
  281. self,
  282. size_or_message: int | None | str,
  283. max_size: int | None = None,
  284. cur_size: int | None = None,
  285. ) -> None:
  286. if isinstance(size_or_message, str):
  287. assert max_size is None
  288. assert cur_size is None
  289. warnings.warn( # deprecated in 14.0 - 2024-11-09
  290. "PayloadTooBig(message) is deprecated; "
  291. "change to PayloadTooBig(size, max_size)",
  292. DeprecationWarning,
  293. )
  294. self.message: str | None = size_or_message
  295. else:
  296. self.message = None
  297. self.size: int | None = size_or_message
  298. assert max_size is not None
  299. self.max_size: int = max_size
  300. self.cur_size: int | None = None
  301. self.set_current_size(cur_size)
  302. def __str__(self) -> str:
  303. if self.message is not None:
  304. return self.message
  305. else:
  306. message = "frame "
  307. if self.size is not None:
  308. message += f"with {self.size} bytes "
  309. if self.cur_size is not None:
  310. message += f"after reading {self.cur_size} bytes "
  311. message += f"exceeds limit of {self.max_size} bytes"
  312. return message
  313. def set_current_size(self, cur_size: int | None) -> None:
  314. assert self.cur_size is None
  315. if cur_size is not None:
  316. self.max_size += cur_size
  317. self.cur_size = cur_size
  318. class InvalidState(WebSocketException, AssertionError):
  319. """
  320. Raised when sending a frame is forbidden in the current state.
  321. Specifically, the Sans-I/O layer raises this exception when:
  322. * sending a data frame to a connection in a state other
  323. :attr:`~websockets.protocol.State.OPEN`;
  324. * sending a control frame to a connection in a state other than
  325. :attr:`~websockets.protocol.State.OPEN` or
  326. :attr:`~websockets.protocol.State.CLOSING`.
  327. """
  328. class ConcurrencyError(WebSocketException, RuntimeError):
  329. """
  330. Raised when receiving or sending messages concurrently.
  331. WebSocket is a connection-oriented protocol. Reads must be serialized; so
  332. must be writes. However, reading and writing concurrently is possible.
  333. """
  334. # At the bottom to break import cycles created by type annotations.
  335. from . import frames, http11 # noqa: E402
  336. lazy_import(
  337. globals(),
  338. deprecated_aliases={
  339. # deprecated in 14.0 - 2024-11-09
  340. "AbortHandshake": ".legacy.exceptions",
  341. "InvalidStatusCode": ".legacy.exceptions",
  342. "RedirectHandshake": ".legacy.exceptions",
  343. "WebSocketProtocolError": ".legacy.exceptions",
  344. },
  345. )