Skip to content

Commit

Permalink
Fix ratelimiter with method checks
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Apr 14, 2024
1 parent a46f1d5 commit da4d1ba
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions starlette_plus/middleware/ratelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


if TYPE_CHECKING:
from starlette.routing import Route
from starlette.routing import Mount, WebSocketRoute
from starlette.types import ASGIApp, Receive, Scope, Send

from ..redis import Redis
Expand Down Expand Up @@ -70,12 +70,15 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
request: Request = Request(scope)
forwarded: str | None = request.headers.get("X-Forwarded-For", None)

routes: list[Route] = scope["app"].routes
route: Route | None = None
routes: list[Route | Mount | WebSocketRoute] = scope["app"].routes
route: Route | Mount | WebSocketRoute | None = None

for r in routes:
methods: set[str] = r.methods or set()
if r.path == request.url.path and request.method in methods:
methods: set[str] | None = r.methods if isinstance(r, Route) else None
if r.path != request.url.path:
continue

if not methods or request.method in methods:
route = r
break

Expand Down

0 comments on commit da4d1ba

Please sign in to comment.