mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-16 05:36:29 +00:00
Fix protocol handling (#1027)
This commit is contained in:
parent
d9874c4c3e
commit
113b62ee77
@ -85,7 +85,17 @@ class APIIngress(CoreSysAttributes):
|
|||||||
self, request: web.Request, addon: Addon, path: str
|
self, request: web.Request, addon: Addon, path: str
|
||||||
) -> web.WebSocketResponse:
|
) -> web.WebSocketResponse:
|
||||||
"""Ingress route for websocket."""
|
"""Ingress route for websocket."""
|
||||||
ws_server = web.WebSocketResponse()
|
if hdrs.SEC_WEBSOCKET_PROTOCOL in request.headers:
|
||||||
|
req_protocols = [
|
||||||
|
str(proto.strip())
|
||||||
|
for proto in request.headers[hdrs.SEC_WEBSOCKET_PROTOCOL].split(",")
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
req_protocols = ()
|
||||||
|
|
||||||
|
ws_server = web.WebSocketResponse(
|
||||||
|
protocols=req_protocols, autoclose=False, autoping=False
|
||||||
|
)
|
||||||
await ws_server.prepare(request)
|
await ws_server.prepare(request)
|
||||||
|
|
||||||
# Preparing
|
# Preparing
|
||||||
@ -98,7 +108,11 @@ class APIIngress(CoreSysAttributes):
|
|||||||
|
|
||||||
# Start proxy
|
# Start proxy
|
||||||
async with self.sys_websession.ws_connect(
|
async with self.sys_websession.ws_connect(
|
||||||
url, headers=source_header
|
url,
|
||||||
|
headers=source_header,
|
||||||
|
protocols=req_protocols,
|
||||||
|
autoclose=False,
|
||||||
|
autoping=False,
|
||||||
) as ws_client:
|
) as ws_client:
|
||||||
# Proxy requests
|
# Proxy requests
|
||||||
await asyncio.wait(
|
await asyncio.wait(
|
||||||
@ -204,6 +218,7 @@ def _is_websocket(request: web.Request) -> bool:
|
|||||||
|
|
||||||
async def _websocket_forward(ws_from, ws_to):
|
async def _websocket_forward(ws_from, ws_to):
|
||||||
"""Handle websocket message directly."""
|
"""Handle websocket message directly."""
|
||||||
|
try:
|
||||||
async for msg in ws_from:
|
async for msg in ws_from:
|
||||||
if msg.type == aiohttp.WSMsgType.TEXT:
|
if msg.type == aiohttp.WSMsgType.TEXT:
|
||||||
await ws_to.send_str(msg.data)
|
await ws_to.send_str(msg.data)
|
||||||
@ -215,3 +230,5 @@ async def _websocket_forward(ws_from, ws_to):
|
|||||||
await ws_to.pong()
|
await ws_to.pong()
|
||||||
elif ws_to.closed:
|
elif ws_to.closed:
|
||||||
await ws_to.close(code=ws_to.close_code, message=msg.extra)
|
await ws_to.close(code=ws_to.close_code, message=msg.extra)
|
||||||
|
except RuntimeError:
|
||||||
|
_LOGGER.warning("Ingress Websocket runtime error")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user