_protocol_server.py 931 B

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from ._protocol_connect import ConnectServerProtocol
  4. from ._protocol_grpc import (
  5. GRPC_CONTENT_TYPE_DEFAULT,
  6. GRPC_CONTENT_TYPE_PREFIX,
  7. GRPC_WEB_CONTENT_TYPE_DEFAULT,
  8. GRPC_WEB_CONTENT_TYPE_PREFIX,
  9. GRPCServerProtocol,
  10. GRPCWebServerProtocol,
  11. )
  12. if TYPE_CHECKING:
  13. from ._protocol import ServerProtocol
  14. _CONNECT_PROTOCOL = ConnectServerProtocol()
  15. _GRPC_PROTOCOL = GRPCServerProtocol()
  16. _GRPC_WEB_PROTOCOL = GRPCWebServerProtocol()
  17. def negotiate_server_protocol(content_type: str) -> ServerProtocol:
  18. if content_type == GRPC_CONTENT_TYPE_DEFAULT or content_type.startswith(
  19. GRPC_CONTENT_TYPE_PREFIX
  20. ):
  21. return _GRPC_PROTOCOL
  22. if content_type == GRPC_WEB_CONTENT_TYPE_DEFAULT or content_type.startswith(
  23. GRPC_WEB_CONTENT_TYPE_PREFIX
  24. ):
  25. return _GRPC_WEB_PROTOCOL
  26. return _CONNECT_PROTOCOL