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.
 
 
 
 

124 line
2.8 KiB

  1. from __future__ import annotations
  2. from collections.abc import Sequence
  3. from ..frames import Frame
  4. from ..typing import ExtensionName, ExtensionParameter
  5. __all__ = ["Extension", "ClientExtensionFactory", "ServerExtensionFactory"]
  6. class Extension:
  7. """
  8. Base class for extensions.
  9. """
  10. name: ExtensionName
  11. """Extension identifier."""
  12. def decode(self, frame: Frame, *, max_size: int | None = None) -> Frame:
  13. """
  14. Decode an incoming frame.
  15. Args:
  16. frame: Incoming frame.
  17. max_size: Maximum payload size in bytes.
  18. Returns:
  19. Decoded frame.
  20. Raises:
  21. PayloadTooBig: If decoding the payload exceeds ``max_size``.
  22. """
  23. raise NotImplementedError
  24. def encode(self, frame: Frame) -> Frame:
  25. """
  26. Encode an outgoing frame.
  27. Args:
  28. frame: Outgoing frame.
  29. Returns:
  30. Encoded frame.
  31. """
  32. raise NotImplementedError
  33. class ClientExtensionFactory:
  34. """
  35. Base class for client-side extension factories.
  36. """
  37. name: ExtensionName
  38. """Extension identifier."""
  39. def get_request_params(self) -> Sequence[ExtensionParameter]:
  40. """
  41. Build parameters to send to the server for this extension.
  42. Returns:
  43. Parameters to send to the server.
  44. """
  45. raise NotImplementedError
  46. def process_response_params(
  47. self,
  48. params: Sequence[ExtensionParameter],
  49. accepted_extensions: Sequence[Extension],
  50. ) -> Extension:
  51. """
  52. Process parameters received from the server.
  53. Args:
  54. params: Parameters received from the server for this extension.
  55. accepted_extensions: List of previously accepted extensions.
  56. Returns:
  57. An extension instance.
  58. Raises:
  59. NegotiationError: If parameters aren't acceptable.
  60. """
  61. raise NotImplementedError
  62. class ServerExtensionFactory:
  63. """
  64. Base class for server-side extension factories.
  65. """
  66. name: ExtensionName
  67. """Extension identifier."""
  68. def process_request_params(
  69. self,
  70. params: Sequence[ExtensionParameter],
  71. accepted_extensions: Sequence[Extension],
  72. ) -> tuple[list[ExtensionParameter], Extension]:
  73. """
  74. Process parameters received from the client.
  75. Args:
  76. params: Parameters received from the client for this extension.
  77. accepted_extensions: List of previously accepted extensions.
  78. Returns:
  79. To accept the offer, parameters to send to the client for this
  80. extension and an extension instance.
  81. Raises:
  82. NegotiationError: To reject the offer, if parameters received from
  83. the client aren't acceptable.
  84. """
  85. raise NotImplementedError