Parse allow_origins into literal origins and a combined regex pattern. Args: allow_origins: List of origin strings. Entries prefixed with 'regex:' are treated as regex patterns; all others are treated as literal origins. Returns: A tuple of (literal_origins, combined_regex) where
(
allow_origins: list[str],
)
| 97 | |
| 98 | |
| 99 | def _parse_cors_origins( |
| 100 | allow_origins: list[str], |
| 101 | ) -> tuple[list[str], Optional[str]]: |
| 102 | """Parse allow_origins into literal origins and a combined regex pattern. |
| 103 | |
| 104 | Args: |
| 105 | allow_origins: List of origin strings. Entries prefixed with 'regex:' are |
| 106 | treated as regex patterns; all others are treated as literal origins. |
| 107 | |
| 108 | Returns: |
| 109 | A tuple of (literal_origins, combined_regex) where combined_regex is None |
| 110 | if no regex patterns were provided, or a single pattern joining all regex |
| 111 | patterns with '|'. |
| 112 | """ |
| 113 | literal_origins = [] |
| 114 | regex_patterns = [] |
| 115 | for origin in allow_origins: |
| 116 | if origin.startswith(_REGEX_PREFIX): |
| 117 | pattern = origin[len(_REGEX_PREFIX) :] |
| 118 | if pattern: |
| 119 | regex_patterns.append(pattern) |
| 120 | else: |
| 121 | literal_origins.append(origin) |
| 122 | |
| 123 | combined_regex = "|".join(regex_patterns) if regex_patterns else None |
| 124 | return literal_origins, combined_regex |
| 125 | |
| 126 | |
| 127 | def _is_origin_allowed( |