Skip to content

Commit ee0657d

Browse files
[PR #9976/2b40f6b7 backport][3.11] Restore only construct the allowed_methods set once for a StaticResource (#9979)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 72935db commit ee0657d

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

CHANGES/9972.bugfix.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Reverted an optimization to avoid rebuilding the allowed methods for ``StaticResource`` on every request -- by :user:`bdraco`.
2-
3-
``aiohttp-cors`` needs to be able to modify the allowed methods at run time via this internal.
1+
Fixed ``StaticResource`` not allowing the ``OPTIONS`` method after calling ``set_options_route`` -- by :user:`bdraco`.

CHANGES/9976.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9972.bugfix.rst

aiohttp/web_urldispatcher.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def __init__(
580580
"HEAD", self._handle, self, expect_handler=expect_handler
581581
),
582582
}
583+
self._allowed_methods = set(self._routes)
583584

584585
def url_for( # type: ignore[override]
585586
self,
@@ -642,14 +643,15 @@ def set_options_route(self, handler: Handler) -> None:
642643
self._routes["OPTIONS"] = ResourceRoute(
643644
"OPTIONS", handler, self, expect_handler=self._expect_handler
644645
)
646+
self._allowed_methods.add("OPTIONS")
645647

646648
async def resolve(self, request: Request) -> _Resolve:
647649
path = request.rel_url.path_safe
648650
method = request.method
649651
if not path.startswith(self._prefix2) and path != self._prefix:
650652
return None, set()
651653

652-
allowed_methods = set(self._routes)
654+
allowed_methods = self._allowed_methods
653655
if method not in allowed_methods:
654656
return None, allowed_methods
655657

tests/test_urldispatch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ async def test_add_static_access_resources(router: web.UrlDispatcher) -> None:
498498
"/st", pathlib.Path(aiohttp.__file__).parent, name="static"
499499
)
500500
resource._routes[hdrs.METH_OPTIONS] = resource._routes[hdrs.METH_GET]
501+
resource._allowed_methods.add(hdrs.METH_OPTIONS)
501502
mapping, allowed_methods = await resource.resolve(
502503
make_mocked_request("OPTIONS", "/st/path")
503504
)

0 commit comments

Comments
 (0)