_base_connection.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import annotations
  2. import typing
  3. from .util.connection import _TYPE_SOCKET_OPTIONS
  4. from .util.timeout import _DEFAULT_TIMEOUT, _TYPE_TIMEOUT
  5. from .util.url import Url
  6. _TYPE_BODY = typing.Union[
  7. bytes, typing.IO[typing.Any], typing.Iterable[bytes | str], str
  8. ]
  9. class ProxyConfig(typing.NamedTuple):
  10. ssl_context: ssl.SSLContext | None
  11. use_forwarding_for_https: bool
  12. assert_hostname: None | str | typing.Literal[False]
  13. assert_fingerprint: str | None
  14. class _ResponseOptions(typing.NamedTuple):
  15. # TODO: Remove this in favor of a better
  16. # HTTP request/response lifecycle tracking.
  17. request_method: str
  18. request_url: str
  19. preload_content: bool
  20. decode_content: bool
  21. enforce_content_length: bool
  22. if typing.TYPE_CHECKING:
  23. import ssl
  24. from typing import Protocol
  25. from .response import BaseHTTPResponse
  26. class BaseHTTPConnection(Protocol):
  27. default_port: typing.ClassVar[int]
  28. default_socket_options: typing.ClassVar[_TYPE_SOCKET_OPTIONS]
  29. host: str
  30. port: int
  31. timeout: None | (
  32. float
  33. ) # Instance doesn't store _DEFAULT_TIMEOUT, must be resolved.
  34. blocksize: int
  35. source_address: tuple[str, int] | None
  36. socket_options: _TYPE_SOCKET_OPTIONS | None
  37. proxy: Url | None
  38. proxy_config: ProxyConfig | None
  39. is_verified: bool
  40. proxy_is_verified: bool | None
  41. def __init__(
  42. self,
  43. host: str,
  44. port: int | None = None,
  45. *,
  46. timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT,
  47. source_address: tuple[str, int] | None = None,
  48. blocksize: int = 8192,
  49. socket_options: _TYPE_SOCKET_OPTIONS | None = ...,
  50. proxy: Url | None = None,
  51. proxy_config: ProxyConfig | None = None,
  52. ) -> None: ...
  53. def set_tunnel(
  54. self,
  55. host: str,
  56. port: int | None = None,
  57. headers: typing.Mapping[str, str] | None = None,
  58. scheme: str = "http",
  59. ) -> None: ...
  60. def connect(self) -> None: ...
  61. def request(
  62. self,
  63. method: str,
  64. url: str,
  65. body: _TYPE_BODY | None = None,
  66. headers: typing.Mapping[str, str] | None = None,
  67. # We know *at least* botocore is depending on the order of the
  68. # first 3 parameters so to be safe we only mark the later ones
  69. # as keyword-only to ensure we have space to extend.
  70. *,
  71. chunked: bool = False,
  72. preload_content: bool = True,
  73. decode_content: bool = True,
  74. enforce_content_length: bool = True,
  75. ) -> None: ...
  76. def getresponse(self) -> BaseHTTPResponse: ...
  77. def close(self) -> None: ...
  78. @property
  79. def is_closed(self) -> bool:
  80. """Whether the connection either is brand new or has been previously closed.
  81. If this property is True then both ``is_connected`` and ``has_connected_to_proxy``
  82. properties must be False.
  83. """
  84. @property
  85. def is_connected(self) -> bool:
  86. """Whether the connection is actively connected to any origin (proxy or target)"""
  87. @property
  88. def has_connected_to_proxy(self) -> bool:
  89. """Whether the connection has successfully connected to its proxy.
  90. This returns False if no proxy is in use. Used to determine whether
  91. errors are coming from the proxy layer or from tunnelling to the target origin.
  92. """
  93. class BaseHTTPSConnection(BaseHTTPConnection, Protocol):
  94. default_port: typing.ClassVar[int]
  95. default_socket_options: typing.ClassVar[_TYPE_SOCKET_OPTIONS]
  96. # Certificate verification methods
  97. cert_reqs: int | str | None
  98. assert_hostname: None | str | typing.Literal[False]
  99. assert_fingerprint: str | None
  100. ssl_context: ssl.SSLContext | None
  101. # Trusted CAs
  102. ca_certs: str | None
  103. ca_cert_dir: str | None
  104. ca_cert_data: None | str | bytes
  105. # TLS version
  106. ssl_minimum_version: int | None
  107. ssl_maximum_version: int | None
  108. ssl_version: int | str | None # Deprecated
  109. # Client certificates
  110. cert_file: str | None
  111. key_file: str | None
  112. key_password: str | None
  113. def __init__(
  114. self,
  115. host: str,
  116. port: int | None = None,
  117. *,
  118. timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT,
  119. source_address: tuple[str, int] | None = None,
  120. blocksize: int = 16384,
  121. socket_options: _TYPE_SOCKET_OPTIONS | None = ...,
  122. proxy: Url | None = None,
  123. proxy_config: ProxyConfig | None = None,
  124. cert_reqs: int | str | None = None,
  125. assert_hostname: None | str | typing.Literal[False] = None,
  126. assert_fingerprint: str | None = None,
  127. server_hostname: str | None = None,
  128. ssl_context: ssl.SSLContext | None = None,
  129. ca_certs: str | None = None,
  130. ca_cert_dir: str | None = None,
  131. ca_cert_data: None | str | bytes = None,
  132. ssl_minimum_version: int | None = None,
  133. ssl_maximum_version: int | None = None,
  134. ssl_version: int | str | None = None, # Deprecated
  135. cert_file: str | None = None,
  136. key_file: str | None = None,
  137. key_password: str | None = None,
  138. ) -> None: ...