comparison urllib3/contrib/emscripten/connection.py @ 7:5eb2d5e3bf22

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