diff --git a/CHANGES/9170.misc.rst b/CHANGES/9170.misc.rst new file mode 120000 index 00000000000..e41cbad0125 --- /dev/null +++ b/CHANGES/9170.misc.rst @@ -0,0 +1 @@ +9158.misc.rst \ No newline at end of file diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index b8768064507..c4199b12271 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -84,7 +84,7 @@ def _build_middlewares( handler: Handler, apps: Tuple["Application", ...] ) -> Callable[[Request], Awaitable[StreamResponse]]: """Apply middlewares to handler.""" - for app in apps: + for app in apps[::-1]: for m, _ in app._middlewares_handlers: # type: ignore[union-attr] handler = update_wrapper(partial(m, handler=handler), handler) # type: ignore[misc] return handler @@ -545,7 +545,7 @@ async def _handle(self, request: Request) -> StreamResponse: if self._run_middlewares: if not self._has_legacy_middlewares: - handler = _build_middlewares(handler, match_info.apps[::-1]) + handler = _build_middlewares(handler, match_info.apps) else: for app in match_info.apps[::-1]: for m, new_style in app._middlewares_handlers: # type: ignore[union-attr]