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