|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import abc |
| 4 | +import warnings |
4 | 5 | from abc import abstractmethod |
5 | 6 | from typing import TYPE_CHECKING, Any, Callable, Protocol, runtime_checkable |
6 | 7 |
|
7 | 8 | from litestar.enums import ScopeType |
8 | 9 | from litestar.middleware._utils import ( |
9 | 10 | build_exclude_path_pattern, |
| 11 | + should_bypass_for_path_pattern, |
10 | 12 | should_bypass_middleware, |
11 | 13 | ) |
12 | 14 | from litestar.utils.deprecation import warn_deprecation |
@@ -246,16 +248,48 @@ def __call__(self, app: ASGIApp) -> ASGIApp: |
246 | 248 | exclude_pattern = build_exclude_path_pattern(exclude=self.exclude_path_pattern, middleware_cls=type(self)) |
247 | 249 | scopes = set(self.scopes) |
248 | 250 | exclude_opt_key = self.exclude_opt_key |
| 251 | + should_bypass_for_scope = self.should_bypass_for_scope |
| 252 | + |
| 253 | + def exclude_pattern_matches_handler_path(scope: Scope) -> bool: |
| 254 | + if exclude_pattern is None: |
| 255 | + return False |
| 256 | + handler = scope["route_handler"] |
| 257 | + return any(exclude_pattern.search(path) for path in handler.paths) |
249 | 258 |
|
250 | 259 | async def middleware(scope: Scope, receive: Receive, send: Send) -> None: |
251 | | - if should_bypass_middleware( |
252 | | - scope=scope, |
253 | | - scopes=scopes, # type: ignore[arg-type] |
254 | | - exclude_opt_key=exclude_opt_key, |
255 | | - exclude_path_pattern=exclude_pattern, |
| 260 | + path_excluded = False |
| 261 | + if ( |
| 262 | + should_bypass_middleware( |
| 263 | + scope=scope, |
| 264 | + scopes=scopes, # type: ignore[arg-type] |
| 265 | + exclude_opt_key=exclude_opt_key, |
| 266 | + ) |
| 267 | + or (path_excluded := should_bypass_for_path_pattern(scope, exclude_pattern)) |
| 268 | + or (should_bypass_for_scope and should_bypass_for_scope(scope)) |
256 | 269 | ): |
| 270 | + if path_excluded and not exclude_pattern_matches_handler_path(scope): |
| 271 | + warnings.warn( |
| 272 | + f"{type(self).__name__}.exclude_path_pattern={exclude_pattern.pattern!r} " |
| 273 | + "did match the request path but did not match the route " |
| 274 | + "handler's path. When upgrading to Litestar 3, this middleware " |
| 275 | + "would NOT be excluded. To keep the current behaviour, use " |
| 276 | + "'should_bypass_for_scope' instead.", |
| 277 | + category=DeprecationWarning, |
| 278 | + stacklevel=2, |
| 279 | + ) |
| 280 | + |
257 | 281 | await app(scope, receive, send) |
258 | 282 | else: |
| 283 | + if exclude_pattern_matches_handler_path(scope): |
| 284 | + warnings.warn( |
| 285 | + f"{type(self).__name__}.exclude_path_pattern={exclude_pattern.pattern!r} " |
| 286 | + "did not match the request path but did match the route " |
| 287 | + "handler's path. When upgrading to Litestar 3, this middleware " |
| 288 | + "would be excluded. To keep the current behaviour, use " |
| 289 | + "'should_bypass_for_scope' instead.", |
| 290 | + category=DeprecationWarning, |
| 291 | + stacklevel=2, |
| 292 | + ) |
259 | 293 | await handle(scope=scope, receive=receive, send=send, next_app=app) |
260 | 294 |
|
261 | 295 | return middleware |
|
0 commit comments