Skip to content

Commit

Permalink
[PR #9976/2b40f6b7 backport][3.11] Restore only construct the allowed…
Browse files Browse the repository at this point in the history
…_methods set once for a `StaticResource` (#9979)

Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
patchback[bot] and bdraco authored Nov 19, 2024
1 parent 72935db commit ee0657d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
4 changes: 1 addition & 3 deletions CHANGES/9972.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Reverted an optimization to avoid rebuilding the allowed methods for ``StaticResource`` on every request -- by :user:`bdraco`.

``aiohttp-cors`` needs to be able to modify the allowed methods at run time via this internal.
Fixed ``StaticResource`` not allowing the ``OPTIONS`` method after calling ``set_options_route`` -- by :user:`bdraco`.
1 change: 1 addition & 0 deletions CHANGES/9976.bugfix.rst
4 changes: 3 additions & 1 deletion aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ def __init__(
"HEAD", self._handle, self, expect_handler=expect_handler
),
}
self._allowed_methods = set(self._routes)

def url_for( # type: ignore[override]
self,
Expand Down Expand Up @@ -642,14 +643,15 @@ def set_options_route(self, handler: Handler) -> None:
self._routes["OPTIONS"] = ResourceRoute(
"OPTIONS", handler, self, expect_handler=self._expect_handler
)
self._allowed_methods.add("OPTIONS")

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

allowed_methods = set(self._routes)
allowed_methods = self._allowed_methods
if method not in allowed_methods:
return None, allowed_methods

Expand Down
1 change: 1 addition & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ async def test_add_static_access_resources(router: web.UrlDispatcher) -> None:
"/st", pathlib.Path(aiohttp.__file__).parent, name="static"
)
resource._routes[hdrs.METH_OPTIONS] = resource._routes[hdrs.METH_GET]
resource._allowed_methods.add(hdrs.METH_OPTIONS)
mapping, allowed_methods = await resource.resolve(
make_mocked_request("OPTIONS", "/st/path")
)
Expand Down

0 comments on commit ee0657d

Please sign in to comment.