|
18 | 18 | from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection
|
19 | 19 | from aiohttp.web_log import AccessLogger
|
20 | 20 | from aiohttp.web_protocol import RequestHandler
|
21 |
| -from aiohttp.web_urldispatcher import ( |
22 |
| - AbstractResource, |
23 |
| - UrlDispatcher, |
24 |
| - UrlMappingMatchInfo, |
25 |
| -) |
| 21 | +from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher |
26 | 22 | from aiohttp_zlib_ng import enable_zlib_ng
|
27 | 23 | from cryptography import x509
|
28 | 24 | from cryptography.hazmat.primitives import hashes, serialization
|
@@ -321,7 +317,7 @@ def __init__(
|
321 | 317 | # By default aiohttp does a linear search for routing rules,
|
322 | 318 | # we have a lot of routes, so use a dict lookup with a fallback
|
323 | 319 | # to the linear search.
|
324 |
| - self.app._router = FastUrlDispatcher() |
| 320 | + attach_fast_url_dispatcher(self.app, FastUrlDispatcher()) |
325 | 321 | self.hass = hass
|
326 | 322 | self.ssl_certificate = ssl_certificate
|
327 | 323 | self.ssl_peer_certificate = ssl_peer_certificate
|
@@ -587,40 +583,3 @@ async def start_http_server_and_save_config(
|
587 | 583 | ]
|
588 | 584 |
|
589 | 585 | store.async_delay_save(lambda: conf, SAVE_DELAY)
|
590 |
| - |
591 |
| - |
592 |
| -class FastUrlDispatcher(UrlDispatcher): |
593 |
| - """UrlDispatcher that uses a dict lookup for resolving.""" |
594 |
| - |
595 |
| - def __init__(self) -> None: |
596 |
| - """Initialize the dispatcher.""" |
597 |
| - super().__init__() |
598 |
| - self._resource_index: dict[str, list[AbstractResource]] = {} |
599 |
| - |
600 |
| - def register_resource(self, resource: AbstractResource) -> None: |
601 |
| - """Register a resource.""" |
602 |
| - super().register_resource(resource) |
603 |
| - canonical = resource.canonical |
604 |
| - if "{" in canonical: # strip at the first { to allow for variables |
605 |
| - canonical = canonical.split("{")[0].rstrip("/") |
606 |
| - # There may be multiple resources for a canonical path |
607 |
| - # so we use a list to avoid falling back to a full linear search |
608 |
| - self._resource_index.setdefault(canonical, []).append(resource) |
609 |
| - |
610 |
| - async def resolve(self, request: web.Request) -> UrlMappingMatchInfo: |
611 |
| - """Resolve a request.""" |
612 |
| - url_parts = request.rel_url.raw_parts |
613 |
| - resource_index = self._resource_index |
614 |
| - |
615 |
| - # Walk the url parts looking for candidates |
616 |
| - for i in range(len(url_parts), 0, -1): |
617 |
| - url_part = "/" + "/".join(url_parts[1:i]) |
618 |
| - if (resource_candidates := resource_index.get(url_part)) is not None: |
619 |
| - for candidate in resource_candidates: |
620 |
| - if ( |
621 |
| - match_dict := (await candidate.resolve(request))[0] |
622 |
| - ) is not None: |
623 |
| - return match_dict |
624 |
| - |
625 |
| - # Finally, fallback to the linear search |
626 |
| - return await super().resolve(request) |
0 commit comments