Skip to content

Commit b65cd9f

Browse files
authored
Pass cors domains as list (#107)
* Pass cors domains as list * Annotate config * Bump version
1 parent 4a6dda9 commit b65cd9f

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

sanic_ext/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import os
4-
from typing import Any, Dict, Optional, Sequence, Union
4+
from typing import Any, Dict, List, Optional, Sequence, Union
55

66
from sanic import Sanic
77
from sanic.config import Config as SanicConfig
@@ -17,7 +17,7 @@ def __init__(
1717
cors_expose_headers: str = "",
1818
cors_max_age: int = 5,
1919
cors_methods: str = "",
20-
cors_origins: str = "",
20+
cors_origins: Union[str, List[str]] = "",
2121
cors_send_wildcard: bool = False,
2222
cors_supports_credentials: bool = False,
2323
cors_vary_header: bool = True,

sanic_ext/extensions/http/cors.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,25 @@ def _get_allow_origins(app: Sanic) -> Tuple[re.Pattern, ...]:
299299

300300

301301
def _parse_allow_origins(
302-
value: Union[str, re.Pattern]
302+
value: Union[str, re.Pattern, List[Union[str, re.Pattern]]]
303303
) -> Tuple[re.Pattern, ...]:
304-
origins: Optional[Union[List[str], List[re.Pattern]]] = None
304+
origins: Optional[
305+
Union[List[str], List[re.Pattern], List[Union[str, re.Pattern]]]
306+
] = None
305307
if value and isinstance(value, str):
306308
if value == "*":
307309
origins = [WILDCARD_PATTERN]
308310
else:
309311
origins = value.split(",")
310312
elif isinstance(value, re.Pattern):
311313
origins = [value]
314+
elif isinstance(value, list):
315+
origins = value
312316

313317
return tuple(
314-
pattern if isinstance(pattern, re.Pattern) else re.compile(pattern)
318+
pattern
319+
if isinstance(pattern, re.Pattern)
320+
else re.compile(re.escape(pattern))
315321
for pattern in (origins or [])
316322
)
317323

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = sanic-ext
3-
version = 22.6.1
3+
version = 22.6.2
44
url = http://github.com/sanic-org/sanic-ext/
55
license = MIT
66
author = Sanic Community

0 commit comments

Comments
 (0)