mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Migrate to using aiohttp-fast-url-dispatcher (#103656)
This commit is contained in:
parent
1a6c3a4944
commit
d1f1bbe304
@ -18,11 +18,7 @@ from aiohttp.typedefs import JSONDecoder, StrOrURL
|
||||
from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection
|
||||
from aiohttp.web_log import AccessLogger
|
||||
from aiohttp.web_protocol import RequestHandler
|
||||
from aiohttp.web_urldispatcher import (
|
||||
AbstractResource,
|
||||
UrlDispatcher,
|
||||
UrlMappingMatchInfo,
|
||||
)
|
||||
from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher
|
||||
from aiohttp_zlib_ng import enable_zlib_ng
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
@ -321,7 +317,7 @@ class HomeAssistantHTTP:
|
||||
# By default aiohttp does a linear search for routing rules,
|
||||
# we have a lot of routes, so use a dict lookup with a fallback
|
||||
# to the linear search.
|
||||
self.app._router = FastUrlDispatcher()
|
||||
attach_fast_url_dispatcher(self.app, FastUrlDispatcher())
|
||||
self.hass = hass
|
||||
self.ssl_certificate = ssl_certificate
|
||||
self.ssl_peer_certificate = ssl_peer_certificate
|
||||
@ -587,40 +583,3 @@ async def start_http_server_and_save_config(
|
||||
]
|
||||
|
||||
store.async_delay_save(lambda: conf, SAVE_DELAY)
|
||||
|
||||
|
||||
class FastUrlDispatcher(UrlDispatcher):
|
||||
"""UrlDispatcher that uses a dict lookup for resolving."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the dispatcher."""
|
||||
super().__init__()
|
||||
self._resource_index: dict[str, list[AbstractResource]] = {}
|
||||
|
||||
def register_resource(self, resource: AbstractResource) -> None:
|
||||
"""Register a resource."""
|
||||
super().register_resource(resource)
|
||||
canonical = resource.canonical
|
||||
if "{" in canonical: # strip at the first { to allow for variables
|
||||
canonical = canonical.split("{")[0].rstrip("/")
|
||||
# There may be multiple resources for a canonical path
|
||||
# so we use a list to avoid falling back to a full linear search
|
||||
self._resource_index.setdefault(canonical, []).append(resource)
|
||||
|
||||
async def resolve(self, request: web.Request) -> UrlMappingMatchInfo:
|
||||
"""Resolve a request."""
|
||||
url_parts = request.rel_url.raw_parts
|
||||
resource_index = self._resource_index
|
||||
|
||||
# Walk the url parts looking for candidates
|
||||
for i in range(len(url_parts), 0, -1):
|
||||
url_part = "/" + "/".join(url_parts[1:i])
|
||||
if (resource_candidates := resource_index.get(url_part)) is not None:
|
||||
for candidate in resource_candidates:
|
||||
if (
|
||||
match_dict := (await candidate.resolve(request))[0]
|
||||
) is not None:
|
||||
return match_dict
|
||||
|
||||
# Finally, fallback to the linear search
|
||||
return await super().resolve(request)
|
||||
|
@ -6,5 +6,9 @@
|
||||
"integration_type": "system",
|
||||
"iot_class": "local_push",
|
||||
"quality_scale": "internal",
|
||||
"requirements": ["aiohttp_cors==0.7.0", "aiohttp-zlib-ng==0.1.1"]
|
||||
"requirements": [
|
||||
"aiohttp_cors==0.7.0",
|
||||
"aiohttp-fast-url-dispatcher==0.1.0",
|
||||
"aiohttp-zlib-ng==0.1.1"
|
||||
]
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
aiodiscover==1.5.1
|
||||
aiohttp-fast-url-dispatcher==0.1.0
|
||||
aiohttp-zlib-ng==0.1.1
|
||||
aiohttp==3.8.5;python_version<'3.12'
|
||||
aiohttp==3.9.0b0;python_version>='3.12'
|
||||
|
@ -26,6 +26,7 @@ dependencies = [
|
||||
"aiohttp==3.9.0b0;python_version>='3.12'",
|
||||
"aiohttp==3.8.5;python_version<'3.12'",
|
||||
"aiohttp_cors==0.7.0",
|
||||
"aiohttp-fast-url-dispatcher==0.1.0",
|
||||
"aiohttp-zlib-ng==0.1.1",
|
||||
"astral==2.2",
|
||||
"attrs==23.1.0",
|
||||
|
@ -4,6 +4,7 @@
|
||||
aiohttp==3.9.0b0;python_version>='3.12'
|
||||
aiohttp==3.8.5;python_version<'3.12'
|
||||
aiohttp_cors==0.7.0
|
||||
aiohttp-fast-url-dispatcher==0.1.0
|
||||
aiohttp-zlib-ng==0.1.1
|
||||
astral==2.2
|
||||
attrs==23.1.0
|
||||
|
@ -257,6 +257,9 @@ aioharmony==0.2.10
|
||||
# homeassistant.components.homekit_controller
|
||||
aiohomekit==3.0.9
|
||||
|
||||
# homeassistant.components.http
|
||||
aiohttp-fast-url-dispatcher==0.1.0
|
||||
|
||||
# homeassistant.components.http
|
||||
aiohttp-zlib-ng==0.1.1
|
||||
|
||||
|
@ -235,6 +235,9 @@ aioharmony==0.2.10
|
||||
# homeassistant.components.homekit_controller
|
||||
aiohomekit==3.0.9
|
||||
|
||||
# homeassistant.components.http
|
||||
aiohttp-fast-url-dispatcher==0.1.0
|
||||
|
||||
# homeassistant.components.http
|
||||
aiohttp-zlib-ng==0.1.1
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user