server.py 24 KB

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