server.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. from __future__ import annotations
  2. import base64
  3. import binascii
  4. import email.utils
  5. import http
  6. import re
  7. import warnings
  8. from collections.abc import Generator, Sequence
  9. from typing import Any, Callable, cast
  10. from .datastructures import Headers, MultipleValuesError
  11. from .exceptions import (
  12. InvalidHandshake,
  13. InvalidHeader,
  14. InvalidHeaderValue,
  15. InvalidMessage,
  16. InvalidOrigin,
  17. InvalidUpgrade,
  18. NegotiationError,
  19. )
  20. from .extensions import Extension, ServerExtensionFactory
  21. from .headers import (
  22. build_extension,
  23. parse_connection,
  24. parse_extension,
  25. parse_subprotocol,
  26. parse_upgrade,
  27. )
  28. from .http11 import Request, Response
  29. from .imports import lazy_import
  30. from .protocol import CONNECTING, OPEN, SERVER, Protocol, State
  31. from .typing import (
  32. ConnectionOption,
  33. ExtensionHeader,
  34. LoggerLike,
  35. Origin,
  36. StatusLike,
  37. Subprotocol,
  38. UpgradeProtocol,
  39. )
  40. from .utils import accept_key
  41. __all__ = ["ServerProtocol"]
  42. class ServerProtocol(Protocol):
  43. """
  44. Sans-I/O implementation of a WebSocket server connection.
  45. Args:
  46. origins: Acceptable values of the ``Origin`` header. Values can be
  47. :class:`str` to test for an exact match or regular expressions
  48. compiled by :func:`re.compile` to test against a pattern. Include
  49. :obj:`None` in the list if the lack of an origin is acceptable.
  50. This is useful for defending against Cross-Site WebSocket
  51. Hijacking attacks.
  52. extensions: List of supported extensions, in order in which they
  53. should be tried.
  54. subprotocols: List of supported subprotocols, in order of decreasing
  55. preference.
  56. select_subprotocol: Callback for selecting a subprotocol among
  57. those supported by the client and the server. It has the same
  58. signature as the :meth:`select_subprotocol` method, including a
  59. :class:`ServerProtocol` instance as first argument.
  60. state: Initial state of the WebSocket connection.
  61. max_size: Maximum size of incoming messages in bytes.
  62. :obj:`None` disables the limit. You may pass a ``(max_message_size,
  63. max_fragment_size)`` tuple to set different limits for messages and
  64. fragments when you expect long messages sent in short fragments.
  65. logger: Logger for this connection;
  66. defaults to ``logging.getLogger("websockets.server")``;
  67. see the :doc:`logging guide <../../topics/logging>` for details.
  68. """
  69. def __init__(
  70. self,
  71. *,
  72. origins: Sequence[Origin | re.Pattern[str] | None] | None = None,
  73. extensions: Sequence[ServerExtensionFactory] | None = None,
  74. subprotocols: Sequence[Subprotocol] | None = None,
  75. select_subprotocol: (
  76. Callable[
  77. [ServerProtocol, Sequence[Subprotocol]],
  78. Subprotocol | None,
  79. ]
  80. | None
  81. ) = None,
  82. state: State = CONNECTING,
  83. max_size: int | None | tuple[int | None, int | None] = 2**20,
  84. logger: LoggerLike | None = None,
  85. ) -> None:
  86. super().__init__(
  87. side=SERVER,
  88. state=state,
  89. max_size=max_size,
  90. logger=logger,
  91. )
  92. self.origins = origins
  93. self.available_extensions = extensions
  94. self.available_subprotocols = subprotocols
  95. if select_subprotocol is not None:
  96. # Bind select_subprotocol then shadow self.select_subprotocol.
  97. # Use setattr to work around https://github.com/python/mypy/issues/2427.
  98. setattr(
  99. self,
  100. "select_subprotocol",
  101. select_subprotocol.__get__(self, self.__class__),
  102. )
  103. def accept(self, request: Request) -> Response:
  104. """
  105. Create a handshake response to accept the connection.
  106. If the handshake request is valid and the handshake successful,
  107. :meth:`accept` returns an HTTP response with status code 101.
  108. Else, it returns an HTTP response with another status code. This rejects
  109. the connection, like :meth:`reject` would.
  110. You must send the handshake response with :meth:`send_response`.
  111. You may modify the response before sending it, typically by adding HTTP
  112. headers.
  113. Args:
  114. request: WebSocket handshake request received from the client.
  115. Returns:
  116. WebSocket handshake response or HTTP response to send to the client.
  117. """
  118. try:
  119. (
  120. accept_header,
  121. extensions_header,
  122. protocol_header,
  123. ) = self.process_request(request)
  124. except InvalidOrigin as exc:
  125. request._exception = exc
  126. self.handshake_exc = exc
  127. if self.debug:
  128. self.logger.debug("! invalid origin", exc_info=True)
  129. return self.reject(
  130. http.HTTPStatus.FORBIDDEN,
  131. f"Failed to open a WebSocket connection: {exc}.\n",
  132. )
  133. except InvalidUpgrade as exc:
  134. request._exception = exc
  135. self.handshake_exc = exc
  136. if self.debug:
  137. self.logger.debug("! invalid upgrade", exc_info=True)
  138. response = self.reject(
  139. http.HTTPStatus.UPGRADE_REQUIRED,
  140. (
  141. f"Failed to open a WebSocket connection: {exc}.\n"
  142. f"\n"
  143. f"You cannot access a WebSocket server directly "
  144. f"with a browser. You need a WebSocket client.\n"
  145. ),
  146. )
  147. response.headers["Upgrade"] = "websocket"
  148. return response
  149. except InvalidHandshake as exc:
  150. request._exception = exc
  151. self.handshake_exc = exc
  152. if self.debug:
  153. self.logger.debug("! invalid handshake", exc_info=True)
  154. exc_chain = cast(BaseException, exc)
  155. exc_str = f"{exc_chain}"
  156. while exc_chain.__cause__ is not None:
  157. exc_chain = exc_chain.__cause__
  158. exc_str += f"; {exc_chain}"
  159. return self.reject(
  160. http.HTTPStatus.BAD_REQUEST,
  161. f"Failed to open a WebSocket connection: {exc_str}.\n",
  162. )
  163. except Exception as exc:
  164. # Handle exceptions raised by user-provided select_subprotocol and
  165. # unexpected errors.
  166. request._exception = exc
  167. self.handshake_exc = exc
  168. self.logger.error("opening handshake failed", exc_info=True)
  169. return self.reject(
  170. http.HTTPStatus.INTERNAL_SERVER_ERROR,
  171. (
  172. "Failed to open a WebSocket connection.\n"
  173. "See server log for more information.\n"
  174. ),
  175. )
  176. headers = Headers()
  177. headers["Date"] = email.utils.formatdate(usegmt=True)
  178. headers["Upgrade"] = "websocket"
  179. headers["Connection"] = "Upgrade"
  180. headers["Sec-WebSocket-Accept"] = accept_header
  181. if extensions_header is not None:
  182. headers["Sec-WebSocket-Extensions"] = extensions_header
  183. if protocol_header is not None:
  184. headers["Sec-WebSocket-Protocol"] = protocol_header
  185. return Response(101, "Switching Protocols", headers)
  186. def process_request(
  187. self,
  188. request: Request,
  189. ) -> tuple[str, str | None, str | None]:
  190. """
  191. Check a handshake request and negotiate extensions and subprotocol.
  192. This function doesn't verify that the request is an HTTP/1.1 or higher
  193. GET request and doesn't check the ``Host`` header. These controls are
  194. usually performed earlier in the HTTP request handling code. They're
  195. the responsibility of the caller.
  196. Args:
  197. request: WebSocket handshake request received from the client.
  198. Returns:
  199. ``Sec-WebSocket-Accept``, ``Sec-WebSocket-Extensions``, and
  200. ``Sec-WebSocket-Protocol`` headers for the handshake response.
  201. Raises:
  202. InvalidHandshake: If the handshake request is invalid;
  203. then the server must return 400 Bad Request error.
  204. """
  205. headers = request.headers
  206. connection: list[ConnectionOption] = sum(
  207. [parse_connection(value) for value in headers.get_all("Connection")], []
  208. )
  209. if not any(value.lower() == "upgrade" for value in connection):
  210. raise InvalidUpgrade(
  211. "Connection", ", ".join(connection) if connection else None
  212. )
  213. upgrade: list[UpgradeProtocol] = sum(
  214. [parse_upgrade(value) for value in headers.get_all("Upgrade")], []
  215. )
  216. # For compatibility with non-strict implementations, ignore case when
  217. # checking the Upgrade header. The RFC always uses "websocket", except
  218. # in section 11.2. (IANA registration) where it uses "WebSocket".
  219. if not (len(upgrade) == 1 and upgrade[0].lower() == "websocket"):
  220. raise InvalidUpgrade("Upgrade", ", ".join(upgrade) if upgrade else None)
  221. try:
  222. key = headers["Sec-WebSocket-Key"]
  223. except KeyError:
  224. raise InvalidHeader("Sec-WebSocket-Key") from None
  225. except MultipleValuesError:
  226. raise InvalidHeader("Sec-WebSocket-Key", "multiple values") from None
  227. try:
  228. raw_key = base64.b64decode(key.encode(), validate=True)
  229. except binascii.Error as exc:
  230. raise InvalidHeaderValue("Sec-WebSocket-Key", key) from exc
  231. if len(raw_key) != 16:
  232. raise InvalidHeaderValue("Sec-WebSocket-Key", key)
  233. accept_header = accept_key(key)
  234. try:
  235. version = headers["Sec-WebSocket-Version"]
  236. except KeyError:
  237. raise InvalidHeader("Sec-WebSocket-Version") from None
  238. except MultipleValuesError:
  239. raise InvalidHeader("Sec-WebSocket-Version", "multiple values") from None
  240. if version != "13":
  241. raise InvalidHeaderValue("Sec-WebSocket-Version", version)
  242. self.origin = self.process_origin(headers)
  243. extensions_header, self.extensions = self.process_extensions(headers)
  244. protocol_header = self.subprotocol = self.process_subprotocol(headers)
  245. return (accept_header, extensions_header, protocol_header)
  246. def process_origin(self, headers: Headers) -> Origin | None:
  247. """
  248. Handle the Origin HTTP request header.
  249. Args:
  250. headers: WebSocket handshake request headers.
  251. Returns:
  252. origin, if it is acceptable.
  253. Raises:
  254. InvalidHandshake: If the Origin header is invalid.
  255. InvalidOrigin: If the origin isn't acceptable.
  256. """
  257. # "The user agent MUST NOT include more than one Origin header field"
  258. # per https://datatracker.ietf.org/doc/html/rfc6454#section-7.3.
  259. try:
  260. origin = headers.get("Origin")
  261. except MultipleValuesError:
  262. raise InvalidHeader("Origin", "multiple values") from None
  263. if origin is not None:
  264. origin = cast(Origin, origin)
  265. if self.origins is not None:
  266. for origin_or_regex in self.origins:
  267. if origin_or_regex == origin or (
  268. isinstance(origin_or_regex, re.Pattern)
  269. and origin is not None
  270. and origin_or_regex.fullmatch(origin) is not None
  271. ):
  272. break
  273. else:
  274. raise InvalidOrigin(origin)
  275. return origin
  276. def process_extensions(
  277. self,
  278. headers: Headers,
  279. ) -> tuple[str | None, list[Extension]]:
  280. """
  281. Handle the Sec-WebSocket-Extensions HTTP request header.
  282. Accept or reject each extension proposed in the client request.
  283. Negotiate parameters for accepted extensions.
  284. Per :rfc:`6455`, negotiation rules are defined by the specification of
  285. each extension.
  286. To provide this level of flexibility, for each extension proposed by
  287. the client, we check for a match with each extension available in the
  288. server configuration. If no match is found, the extension is ignored.
  289. If several variants of the same extension are proposed by the client,
  290. it may be accepted several times, which won't make sense in general.
  291. Extensions must implement their own requirements. For this purpose,
  292. the list of previously accepted extensions is provided.
  293. This process doesn't allow the server to reorder extensions. It can
  294. only select a subset of the extensions proposed by the client.
  295. Other requirements, for example related to mandatory extensions or the
  296. order of extensions, may be implemented by overriding this method.
  297. Args:
  298. headers: WebSocket handshake request headers.
  299. Returns:
  300. ``Sec-WebSocket-Extensions`` HTTP response header and list of
  301. accepted extensions.
  302. Raises:
  303. InvalidHandshake: If the Sec-WebSocket-Extensions header is invalid.
  304. """
  305. response_header_value: str | None = None
  306. extension_headers: list[ExtensionHeader] = []
  307. accepted_extensions: list[Extension] = []
  308. header_values = headers.get_all("Sec-WebSocket-Extensions")
  309. if header_values and self.available_extensions:
  310. parsed_header_values: list[ExtensionHeader] = sum(
  311. [parse_extension(header_value) for header_value in header_values], []
  312. )
  313. for name, request_params in parsed_header_values:
  314. for ext_factory in self.available_extensions:
  315. # Skip non-matching extensions based on their name.
  316. if ext_factory.name != name:
  317. continue
  318. # Skip non-matching extensions based on their params.
  319. try:
  320. response_params, extension = ext_factory.process_request_params(
  321. request_params, accepted_extensions
  322. )
  323. except NegotiationError:
  324. continue
  325. # Add matching extension to the final list.
  326. extension_headers.append((name, response_params))
  327. accepted_extensions.append(extension)
  328. # Break out of the loop once we have a match.
  329. break
  330. # If we didn't break from the loop, no extension in our list
  331. # matched what the client sent. The extension is declined.
  332. # Serialize extension header.
  333. if extension_headers:
  334. response_header_value = build_extension(extension_headers)
  335. return response_header_value, accepted_extensions
  336. def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
  337. """
  338. Handle the Sec-WebSocket-Protocol HTTP request header.
  339. Args:
  340. headers: WebSocket handshake request headers.
  341. Returns:
  342. Subprotocol, if one was selected; this is also the value of the
  343. ``Sec-WebSocket-Protocol`` response header.
  344. Raises:
  345. InvalidHandshake: If the Sec-WebSocket-Subprotocol header is invalid.
  346. """
  347. subprotocols: Sequence[Subprotocol] = sum(
  348. [
  349. parse_subprotocol(header_value)
  350. for header_value in headers.get_all("Sec-WebSocket-Protocol")
  351. ],
  352. [],
  353. )
  354. return self.select_subprotocol(subprotocols)
  355. def select_subprotocol(
  356. self,
  357. subprotocols: Sequence[Subprotocol],
  358. ) -> Subprotocol | None:
  359. """
  360. Pick a subprotocol among those offered by the client.
  361. If several subprotocols are supported by both the client and the server,
  362. pick the first one in the list declared the server.
  363. If the server doesn't support any subprotocols, continue without a
  364. subprotocol, regardless of what the client offers.
  365. If the server supports at least one subprotocol and the client doesn't
  366. offer any, abort the handshake with an HTTP 400 error.
  367. You provide a ``select_subprotocol`` argument to :class:`ServerProtocol`
  368. to override this logic. For example, you could accept the connection
  369. even if client doesn't offer a subprotocol, rather than reject it.
  370. Here's how to negotiate the ``chat`` subprotocol if the client supports
  371. it and continue without a subprotocol otherwise::
  372. def select_subprotocol(protocol, subprotocols):
  373. if "chat" in subprotocols:
  374. return "chat"
  375. Args:
  376. subprotocols: List of subprotocols offered by the client.
  377. Returns:
  378. Selected subprotocol, if a common subprotocol was found.
  379. :obj:`None` to continue without a subprotocol.
  380. Raises:
  381. NegotiationError: Custom implementations may raise this exception
  382. to abort the handshake with an HTTP 400 error.
  383. """
  384. # Server doesn't offer any subprotocols.
  385. if not self.available_subprotocols: # None or empty list
  386. return None
  387. # Server offers at least one subprotocol but client doesn't offer any.
  388. if not subprotocols:
  389. raise NegotiationError("missing subprotocol")
  390. # Server and client both offer subprotocols. Look for a shared one.
  391. proposed_subprotocols = set(subprotocols)
  392. for subprotocol in self.available_subprotocols:
  393. if subprotocol in proposed_subprotocols:
  394. return subprotocol
  395. # No common subprotocol was found.
  396. raise NegotiationError(
  397. "invalid subprotocol; expected one of "
  398. + ", ".join(self.available_subprotocols)
  399. )
  400. def reject(self, status: StatusLike, text: str) -> Response:
  401. """
  402. Create a handshake response to reject the connection.
  403. A short plain text response is the best fallback when failing to
  404. establish a WebSocket connection.
  405. You must send the handshake response with :meth:`send_response`.
  406. You may modify the response before sending it, for example by changing
  407. HTTP headers.
  408. Args:
  409. status: HTTP status code.
  410. text: HTTP response body; it will be encoded to UTF-8.
  411. Returns:
  412. HTTP response to send to the client.
  413. """
  414. # If status is an int instead of an HTTPStatus, fix it automatically.
  415. status = http.HTTPStatus(status)
  416. body = text.encode()
  417. headers = Headers(
  418. [
  419. ("Date", email.utils.formatdate(usegmt=True)),
  420. ("Connection", "close"),
  421. ("Content-Length", str(len(body))),
  422. ("Content-Type", "text/plain; charset=utf-8"),
  423. ]
  424. )
  425. return Response(status.value, status.phrase, headers, body)
  426. def send_response(self, response: Response) -> None:
  427. """
  428. Send a handshake response to the client.
  429. Args:
  430. response: WebSocket handshake response event to send.
  431. """
  432. if self.debug:
  433. code, phrase = response.status_code, response.reason_phrase
  434. self.logger.debug("> HTTP/1.1 %d %s", code, phrase)
  435. for key, value in response.headers.raw_items():
  436. self.logger.debug("> %s: %s", key, value)
  437. if response.body:
  438. self.logger.debug("> [body] (%d bytes)", len(response.body))
  439. self.writes.append(response.serialize())
  440. if response.status_code == 101:
  441. assert self.state is CONNECTING
  442. self.state = OPEN
  443. self.logger.info("connection open")
  444. else:
  445. self.logger.info(
  446. "connection rejected (%d %s)",
  447. response.status_code,
  448. response.reason_phrase,
  449. )
  450. self.send_eof()
  451. self.parser = self.discard()
  452. next(self.parser) # start coroutine
  453. def parse(self) -> Generator[None]:
  454. if self.state is CONNECTING:
  455. try:
  456. request = yield from Request.parse(
  457. self.reader.read_line,
  458. )
  459. except Exception as exc:
  460. self.handshake_exc = InvalidMessage(
  461. "did not receive a valid HTTP request"
  462. )
  463. self.handshake_exc.__cause__ = exc
  464. self.send_eof()
  465. self.parser = self.discard()
  466. next(self.parser) # start coroutine
  467. yield
  468. if self.debug:
  469. self.logger.debug("< GET %s HTTP/1.1", request.path)
  470. for key, value in request.headers.raw_items():
  471. self.logger.debug("< %s: %s", key, value)
  472. self.events.append(request)
  473. yield from super().parse()
  474. class ServerConnection(ServerProtocol):
  475. def __init__(self, *args: Any, **kwargs: Any) -> None:
  476. warnings.warn( # deprecated in 11.0 - 2023-04-02
  477. "ServerConnection was renamed to ServerProtocol",
  478. DeprecationWarning,
  479. )
  480. super().__init__(*args, **kwargs)
  481. lazy_import(
  482. globals(),
  483. deprecated_aliases={
  484. # deprecated in 14.0 - 2024-11-09
  485. "WebSocketServer": ".legacy.server",
  486. "WebSocketServerProtocol": ".legacy.server",
  487. "broadcast": ".legacy.server",
  488. "serve": ".legacy.server",
  489. "unix_serve": ".legacy.server",
  490. },
  491. )