aiohttp: Allow headers to be passed to a WebSocketClient.

This commit will make it possible to add headers to a Websocket.

Among other things, this allows making a connection to online MQTT brokers
over websocket, using the header entry "Sec-WebSocket-Protocol":"mqtt" in
the handshake of the upgrade protocol.

Signed-off-by: Damien George <damien@micropython.org>
pull/941/head
jomas 2024-12-02 11:23:07 +01:00 zatwierdzone przez Damien George
rodzic 86df723301
commit 05a56c3cad
3 zmienionych plików z 3 dodań i 3 usunięć

Wyświetl plik

@ -269,7 +269,7 @@ class ClientSession:
return _WSRequestContextManager(self, self._ws_connect(url, ssl=ssl))
async def _ws_connect(self, url, ssl=None):
ws_client = WebSocketClient(None)
ws_client = WebSocketClient(self._base_headers.copy())
await ws_client.connect(url, ssl=ssl, handshake_request=self.request_raw)
self._reader = ws_client.reader
return ClientWebSocketResponse(ws_client)

Wyświetl plik

@ -136,7 +136,7 @@ class WebSocketClient:
return frame + payload
async def handshake(self, uri, ssl, req):
headers = {}
headers = self.params
_http_proto = "http" if uri.protocol != "wss" else "https"
url = f"{_http_proto}://{uri.hostname}:{uri.port}{uri.path or '/'}"
key = binascii.b2a_base64(bytes(random.getrandbits(8) for _ in range(16)))[:-1]

Wyświetl plik

@ -1,6 +1,6 @@
metadata(
description="HTTP client module for MicroPython asyncio module",
version="0.0.4",
version="0.0.5",
pypi="aiohttp",
)