Skip to content

Commit f389505

Browse files
committed
Add MultiHostTCPSite
1 parent 1e38c7d commit f389505

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""MultiHost capable aiohttp Site."""
2+
from __future__ import annotations
3+
4+
import asyncio
5+
from ssl import SSLContext
6+
7+
from aiohttp import web
8+
from yarl import URL
9+
10+
11+
class MultiHostTCPSite(web.TCPSite):
12+
"""MultiHost capable aiohttp Site.
13+
14+
Vanilla TCPSite accepts only str as host. However, the underlying asyncio's
15+
create_server() implementation does take a list of strings to bind to multiple
16+
host IP's. To support multiple server_host entries (e.g. to enable dual-stack
17+
explicitly), we would like to pass an array of strings.
18+
"""
19+
20+
__slots__ = ("_host", "_port", "_reuse_address", "_reuse_port", "_hosturl")
21+
22+
def __init__(
23+
self,
24+
runner: web.BaseRunner,
25+
host: None | str | list[str],
26+
port: int,
27+
*,
28+
ssl_context: SSLContext | None = None,
29+
backlog: int = 128,
30+
reuse_address: bool | None = None,
31+
reuse_port: bool | None = None,
32+
) -> None:
33+
"""Initialize HomeAssistantTCPSite."""
34+
super().__init__(
35+
runner,
36+
ssl_context=ssl_context,
37+
backlog=backlog,
38+
)
39+
self._host = host
40+
self._port = port
41+
self._reuse_address = reuse_address
42+
self._reuse_port = reuse_port
43+
44+
@property
45+
def name(self) -> str:
46+
"""Return server URL."""
47+
scheme = "https" if self._ssl_context else "http"
48+
host = self._host[0] if isinstance(self._host, list) else "0.0.0.0"
49+
return str(URL.build(scheme=scheme, host=host, port=self._port))

matter_server/server/server.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from aiohttp import web
1111

12+
from matter_server.server.helpers.custom_web_runner import MultiHostTCPSite
13+
1214
from ..common.const import SCHEMA_VERSION
1315
from ..common.errors import VersionMismatch
1416
from ..common.helpers.api import APICommandHandler, api_command
@@ -104,7 +106,7 @@ async def start(self) -> None:
104106
self.app.router.add_route("GET", "/", self._handle_info)
105107
self._runner = web.AppRunner(self.app, access_log=None)
106108
await self._runner.setup()
107-
self._http = web.TCPSite(
109+
self._http = MultiHostTCPSite(
108110
self._runner, host=self.listen_addresses, port=self.port
109111
)
110112
await self._http.start()

0 commit comments

Comments
 (0)