summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/litestar/middleware/_utils.py
blob: 778a5083af34310c0d860fc000a3bd88ba04e0f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Pattern, Sequence

from litestar.exceptions import ImproperlyConfiguredException

__all__ = ("build_exclude_path_pattern", "should_bypass_middleware")


if TYPE_CHECKING:
    from litestar.types import Method, Scope, Scopes


def build_exclude_path_pattern(*, exclude: str | list[str] | None = None) -> Pattern | None:
    """Build single path pattern from list of patterns to opt-out from middleware processing.

    Args:
        exclude: A pattern or a list of patterns.

    Returns:
        An optional pattern to match against scope["path"] to opt-out from middleware processing.
    """
    if exclude is None:
        return None

    try:
        return re.compile("|".join(exclude)) if isinstance(exclude, list) else re.compile(exclude)
    except re.error as e:  # pragma: no cover
        raise ImproperlyConfiguredException(
            "Unable to compile exclude patterns for middleware. Please make sure you passed a valid regular expression."
        ) from e


def should_bypass_middleware(
    *,
    exclude_http_methods: Sequence[Method] | None = None,
    exclude_opt_key: str | None = None,
    exclude_path_pattern: Pattern | None = None,
    scope: Scope,
    scopes: Scopes,
) -> bool:
    """Determine weather a middleware should be bypassed.

    Args:
        exclude_http_methods: A sequence of http methods that do not require authentication.
        exclude_opt_key: Key in ``opt`` with which a route handler can "opt-out" of a middleware.
        exclude_path_pattern: If this pattern matches scope["path"], the middleware should be bypassed.
        scope: The ASGI scope.
        scopes: A set with the ASGI scope types that are supported by the middleware.

    Returns:
        A boolean indicating if a middleware should be bypassed
    """
    if scope["type"] not in scopes:
        return True

    if exclude_opt_key and scope["route_handler"].opt.get(exclude_opt_key):
        return True

    if exclude_http_methods and scope.get("method") in exclude_http_methods:
        return True

    return bool(
        exclude_path_pattern
        and exclude_path_pattern.findall(
            scope["raw_path"].decode() if getattr(scope.get("route_handler", {}), "is_mount", False) else scope["path"]
        )
    )