Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions jvspatial/api/components/endpoint_auth_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import logging
import re
from functools import lru_cache
from typing import Any, Dict, List, Optional

from fastapi import Request
Expand Down Expand Up @@ -67,9 +68,16 @@ def _path_matches(pattern: str, path: str) -> bool:
"""
if pattern == path:
return True
compiled = _compile_path_pattern(pattern)
return path == "/" if compiled is None else bool(compiled.match(path))


@lru_cache(maxsize=1024)
def _compile_path_pattern(pattern: str) -> Optional[re.Pattern[str]]:
"""Cache route syntax only, leaving method and auth metadata live."""
segments = [s for s in pattern.split("/") if s]
if not segments:
return path == "/"
return None
regex_parts: List[str] = []
for seg in segments:
if seg.startswith("{") and seg.endswith("}"):
Expand All @@ -81,8 +89,7 @@ def _path_matches(pattern: str, path: str) -> bool:
regex_parts.append(r"[^/]+")
else:
regex_parts.append(re.escape(seg))
regex = "^/" + "/".join(regex_parts) + "$"
return bool(re.match(regex, path))
return re.compile("^/" + "/".join(regex_parts) + "$")


def _endpoint_info_matches_method(
Expand Down
Loading