jpayne@7
|
1 from __future__ import annotations
|
jpayne@7
|
2
|
jpayne@7
|
3 import os
|
jpayne@7
|
4 import typing
|
jpayne@7
|
5
|
jpayne@7
|
6 # use http.client.HTTPException for consistency with non-emscripten
|
jpayne@7
|
7 from http.client import HTTPException as HTTPException # noqa: F401
|
jpayne@7
|
8 from http.client import ResponseNotReady
|
jpayne@7
|
9
|
jpayne@7
|
10 from ..._base_connection import _TYPE_BODY
|
jpayne@7
|
11 from ...connection import HTTPConnection, ProxyConfig, port_by_scheme
|
jpayne@7
|
12 from ...exceptions import TimeoutError
|
jpayne@7
|
13 from ...response import BaseHTTPResponse
|
jpayne@7
|
14 from ...util.connection import _TYPE_SOCKET_OPTIONS
|
jpayne@7
|
15 from ...util.timeout import _DEFAULT_TIMEOUT, _TYPE_TIMEOUT
|
jpayne@7
|
16 from ...util.url import Url
|
jpayne@7
|
17 from .fetch import _RequestError, _TimeoutError, send_request, send_streaming_request
|
jpayne@7
|
18 from .request import EmscriptenRequest
|
jpayne@7
|
19 from .response import EmscriptenHttpResponseWrapper, EmscriptenResponse
|
jpayne@7
|
20
|
jpayne@7
|
21 if typing.TYPE_CHECKING:
|
jpayne@7
|
22 from ..._base_connection import BaseHTTPConnection, BaseHTTPSConnection
|
jpayne@7
|
23
|
jpayne@7
|
24
|
jpayne@7
|
25 class EmscriptenHTTPConnection:
|
jpayne@7
|
26 default_port: typing.ClassVar[int] = port_by_scheme["http"]
|
jpayne@7
|
27 default_socket_options: typing.ClassVar[_TYPE_SOCKET_OPTIONS]
|
jpayne@7
|
28
|
jpayne@7
|
29 timeout: None | (float)
|
jpayne@7
|
30
|
jpayne@7
|
31 host: str
|
jpayne@7
|
32 port: int
|
jpayne@7
|
33 blocksize: int
|
jpayne@7
|
34 source_address: tuple[str, int] | None
|
jpayne@7
|
35 socket_options: _TYPE_SOCKET_OPTIONS | None
|
jpayne@7
|
36
|
jpayne@7
|
37 proxy: Url | None
|
jpayne@7
|
38 proxy_config: ProxyConfig | None
|
jpayne@7
|
39
|
jpayne@7
|
40 is_verified: bool = False
|
jpayne@7
|
41 proxy_is_verified: bool | None = None
|
jpayne@7
|
42
|
jpayne@7
|
43 _response: EmscriptenResponse | None
|
jpayne@7
|
44
|
jpayne@7
|
45 def __init__(
|
jpayne@7
|
46 self,
|
jpayne@7
|
47 host: str,
|
jpayne@7
|
48 port: int = 0,
|
jpayne@7
|
49 *,
|
jpayne@7
|
50 timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT,
|
jpayne@7
|
51 source_address: tuple[str, int] | None = None,
|
jpayne@7
|
52 blocksize: int = 8192,
|
jpayne@7
|
53 socket_options: _TYPE_SOCKET_OPTIONS | None = None,
|
jpayne@7
|
54 proxy: Url | None = None,
|
jpayne@7
|
55 proxy_config: ProxyConfig | None = None,
|
jpayne@7
|
56 ) -> None:
|
jpayne@7
|
57 self.host = host
|
jpayne@7
|
58 self.port = port
|
jpayne@7
|
59 self.timeout = timeout if isinstance(timeout, float) else 0.0
|
jpayne@7
|
60 self.scheme = "http"
|
jpayne@7
|
61 self._closed = True
|
jpayne@7
|
62 self._response = None
|
jpayne@7
|
63 # ignore these things because we don't
|
jpayne@7
|
64 # have control over that stuff
|
jpayne@7
|
65 self.proxy = None
|
jpayne@7
|
66 self.proxy_config = None
|
jpayne@7
|
67 self.blocksize = blocksize
|
jpayne@7
|
68 self.source_address = None
|
jpayne@7
|
69 self.socket_options = None
|
jpayne@7
|
70 self.is_verified = False
|
jpayne@7
|
71
|
jpayne@7
|
72 def set_tunnel(
|
jpayne@7
|
73 self,
|
jpayne@7
|
74 host: str,
|
jpayne@7
|
75 port: int | None = 0,
|
jpayne@7
|
76 headers: typing.Mapping[str, str] | None = None,
|
jpayne@7
|
77 scheme: str = "http",
|
jpayne@7
|
78 ) -> None:
|
jpayne@7
|
79 pass
|
jpayne@7
|
80
|
jpayne@7
|
81 def connect(self) -> None:
|
jpayne@7
|
82 pass
|
jpayne@7
|
83
|
jpayne@7
|
84 def request(
|
jpayne@7
|
85 self,
|
jpayne@7
|
86 method: str,
|
jpayne@7
|
87 url: str,
|
jpayne@7
|
88 body: _TYPE_BODY | None = None,
|
jpayne@7
|
89 headers: typing.Mapping[str, str] | None = None,
|
jpayne@7
|
90 # We know *at least* botocore is depending on the order of the
|
jpayne@7
|
91 # first 3 parameters so to be safe we only mark the later ones
|
jpayne@7
|
92 # as keyword-only to ensure we have space to extend.
|
jpayne@7
|
93 *,
|
jpayne@7
|
94 chunked: bool = False,
|
jpayne@7
|
95 preload_content: bool = True,
|
jpayne@7
|
96 decode_content: bool = True,
|
jpayne@7
|
97 enforce_content_length: bool = True,
|
jpayne@7
|
98 ) -> None:
|
jpayne@7
|
99 self._closed = False
|
jpayne@7
|
100 if url.startswith("/"):
|
jpayne@7
|
101 # no scheme / host / port included, make a full url
|
jpayne@7
|
102 url = f"{self.scheme}://{self.host}:{self.port}" + url
|
jpayne@7
|
103 request = EmscriptenRequest(
|
jpayne@7
|
104 url=url,
|
jpayne@7
|
105 method=method,
|
jpayne@7
|
106 timeout=self.timeout if self.timeout else 0,
|
jpayne@7
|
107 decode_content=decode_content,
|
jpayne@7
|
108 )
|
jpayne@7
|
109 request.set_body(body)
|
jpayne@7
|
110 if headers:
|
jpayne@7
|
111 for k, v in headers.items():
|
jpayne@7
|
112 request.set_header(k, v)
|
jpayne@7
|
113 self._response = None
|
jpayne@7
|
114 try:
|
jpayne@7
|
115 if not preload_content:
|
jpayne@7
|
116 self._response = send_streaming_request(request)
|
jpayne@7
|
117 if self._response is None:
|
jpayne@7
|
118 self._response = send_request(request)
|
jpayne@7
|
119 except _TimeoutError as e:
|
jpayne@7
|
120 raise TimeoutError(e.message) from e
|
jpayne@7
|
121 except _RequestError as e:
|
jpayne@7
|
122 raise HTTPException(e.message) from e
|
jpayne@7
|
123
|
jpayne@7
|
124 def getresponse(self) -> BaseHTTPResponse:
|
jpayne@7
|
125 if self._response is not None:
|
jpayne@7
|
126 return EmscriptenHttpResponseWrapper(
|
jpayne@7
|
127 internal_response=self._response,
|
jpayne@7
|
128 url=self._response.request.url,
|
jpayne@7
|
129 connection=self,
|
jpayne@7
|
130 )
|
jpayne@7
|
131 else:
|
jpayne@7
|
132 raise ResponseNotReady()
|
jpayne@7
|
133
|
jpayne@7
|
134 def close(self) -> None:
|
jpayne@7
|
135 self._closed = True
|
jpayne@7
|
136 self._response = None
|
jpayne@7
|
137
|
jpayne@7
|
138 @property
|
jpayne@7
|
139 def is_closed(self) -> bool:
|
jpayne@7
|
140 """Whether the connection either is brand new or has been previously closed.
|
jpayne@7
|
141 If this property is True then both ``is_connected`` and ``has_connected_to_proxy``
|
jpayne@7
|
142 properties must be False.
|
jpayne@7
|
143 """
|
jpayne@7
|
144 return self._closed
|
jpayne@7
|
145
|
jpayne@7
|
146 @property
|
jpayne@7
|
147 def is_connected(self) -> bool:
|
jpayne@7
|
148 """Whether the connection is actively connected to any origin (proxy or target)"""
|
jpayne@7
|
149 return True
|
jpayne@7
|
150
|
jpayne@7
|
151 @property
|
jpayne@7
|
152 def has_connected_to_proxy(self) -> bool:
|
jpayne@7
|
153 """Whether the connection has successfully connected to its proxy.
|
jpayne@7
|
154 This returns False if no proxy is in use. Used to determine whether
|
jpayne@7
|
155 errors are coming from the proxy layer or from tunnelling to the target origin.
|
jpayne@7
|
156 """
|
jpayne@7
|
157 return False
|
jpayne@7
|
158
|
jpayne@7
|
159
|
jpayne@7
|
160 class EmscriptenHTTPSConnection(EmscriptenHTTPConnection):
|
jpayne@7
|
161 default_port = port_by_scheme["https"]
|
jpayne@7
|
162 # all this is basically ignored, as browser handles https
|
jpayne@7
|
163 cert_reqs: int | str | None = None
|
jpayne@7
|
164 ca_certs: str | None = None
|
jpayne@7
|
165 ca_cert_dir: str | None = None
|
jpayne@7
|
166 ca_cert_data: None | str | bytes = None
|
jpayne@7
|
167 cert_file: str | None
|
jpayne@7
|
168 key_file: str | None
|
jpayne@7
|
169 key_password: str | None
|
jpayne@7
|
170 ssl_context: typing.Any | None
|
jpayne@7
|
171 ssl_version: int | str | None = None
|
jpayne@7
|
172 ssl_minimum_version: int | None = None
|
jpayne@7
|
173 ssl_maximum_version: int | None = None
|
jpayne@7
|
174 assert_hostname: None | str | typing.Literal[False]
|
jpayne@7
|
175 assert_fingerprint: str | None = None
|
jpayne@7
|
176
|
jpayne@7
|
177 def __init__(
|
jpayne@7
|
178 self,
|
jpayne@7
|
179 host: str,
|
jpayne@7
|
180 port: int = 0,
|
jpayne@7
|
181 *,
|
jpayne@7
|
182 timeout: _TYPE_TIMEOUT = _DEFAULT_TIMEOUT,
|
jpayne@7
|
183 source_address: tuple[str, int] | None = None,
|
jpayne@7
|
184 blocksize: int = 16384,
|
jpayne@7
|
185 socket_options: None
|
jpayne@7
|
186 | _TYPE_SOCKET_OPTIONS = HTTPConnection.default_socket_options,
|
jpayne@7
|
187 proxy: Url | None = None,
|
jpayne@7
|
188 proxy_config: ProxyConfig | None = None,
|
jpayne@7
|
189 cert_reqs: int | str | None = None,
|
jpayne@7
|
190 assert_hostname: None | str | typing.Literal[False] = None,
|
jpayne@7
|
191 assert_fingerprint: str | None = None,
|
jpayne@7
|
192 server_hostname: str | None = None,
|
jpayne@7
|
193 ssl_context: typing.Any | None = None,
|
jpayne@7
|
194 ca_certs: str | None = None,
|
jpayne@7
|
195 ca_cert_dir: str | None = None,
|
jpayne@7
|
196 ca_cert_data: None | str | bytes = None,
|
jpayne@7
|
197 ssl_minimum_version: int | None = None,
|
jpayne@7
|
198 ssl_maximum_version: int | None = None,
|
jpayne@7
|
199 ssl_version: int | str | None = None, # Deprecated
|
jpayne@7
|
200 cert_file: str | None = None,
|
jpayne@7
|
201 key_file: str | None = None,
|
jpayne@7
|
202 key_password: str | None = None,
|
jpayne@7
|
203 ) -> None:
|
jpayne@7
|
204 super().__init__(
|
jpayne@7
|
205 host,
|
jpayne@7
|
206 port=port,
|
jpayne@7
|
207 timeout=timeout,
|
jpayne@7
|
208 source_address=source_address,
|
jpayne@7
|
209 blocksize=blocksize,
|
jpayne@7
|
210 socket_options=socket_options,
|
jpayne@7
|
211 proxy=proxy,
|
jpayne@7
|
212 proxy_config=proxy_config,
|
jpayne@7
|
213 )
|
jpayne@7
|
214 self.scheme = "https"
|
jpayne@7
|
215
|
jpayne@7
|
216 self.key_file = key_file
|
jpayne@7
|
217 self.cert_file = cert_file
|
jpayne@7
|
218 self.key_password = key_password
|
jpayne@7
|
219 self.ssl_context = ssl_context
|
jpayne@7
|
220 self.server_hostname = server_hostname
|
jpayne@7
|
221 self.assert_hostname = assert_hostname
|
jpayne@7
|
222 self.assert_fingerprint = assert_fingerprint
|
jpayne@7
|
223 self.ssl_version = ssl_version
|
jpayne@7
|
224 self.ssl_minimum_version = ssl_minimum_version
|
jpayne@7
|
225 self.ssl_maximum_version = ssl_maximum_version
|
jpayne@7
|
226 self.ca_certs = ca_certs and os.path.expanduser(ca_certs)
|
jpayne@7
|
227 self.ca_cert_dir = ca_cert_dir and os.path.expanduser(ca_cert_dir)
|
jpayne@7
|
228 self.ca_cert_data = ca_cert_data
|
jpayne@7
|
229
|
jpayne@7
|
230 self.cert_reqs = None
|
jpayne@7
|
231
|
jpayne@7
|
232 # The browser will automatically verify all requests.
|
jpayne@7
|
233 # We have no control over that setting.
|
jpayne@7
|
234 self.is_verified = True
|
jpayne@7
|
235
|
jpayne@7
|
236 def set_cert(
|
jpayne@7
|
237 self,
|
jpayne@7
|
238 key_file: str | None = None,
|
jpayne@7
|
239 cert_file: str | None = None,
|
jpayne@7
|
240 cert_reqs: int | str | None = None,
|
jpayne@7
|
241 key_password: str | None = None,
|
jpayne@7
|
242 ca_certs: str | None = None,
|
jpayne@7
|
243 assert_hostname: None | str | typing.Literal[False] = None,
|
jpayne@7
|
244 assert_fingerprint: str | None = None,
|
jpayne@7
|
245 ca_cert_dir: str | None = None,
|
jpayne@7
|
246 ca_cert_data: None | str | bytes = None,
|
jpayne@7
|
247 ) -> None:
|
jpayne@7
|
248 pass
|
jpayne@7
|
249
|
jpayne@7
|
250
|
jpayne@7
|
251 # verify that this class implements BaseHTTP(s) connection correctly
|
jpayne@7
|
252 if typing.TYPE_CHECKING:
|
jpayne@7
|
253 _supports_http_protocol: BaseHTTPConnection = EmscriptenHTTPConnection("", 0)
|
jpayne@7
|
254 _supports_https_protocol: BaseHTTPSConnection = EmscriptenHTTPSConnection("", 0)
|