Skip to content

Commit

Permalink
Pass cors domains as list (#107)
Browse files Browse the repository at this point in the history
* Pass cors domains as list

* Annotate config

* Bump version
  • Loading branch information
ahopkins authored Jul 11, 2022
1 parent 4a6dda9 commit b65cd9f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sanic_ext/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
from typing import Any, Dict, Optional, Sequence, Union
from typing import Any, Dict, List, Optional, Sequence, Union

from sanic import Sanic
from sanic.config import Config as SanicConfig
Expand All @@ -17,7 +17,7 @@ def __init__(
cors_expose_headers: str = "",
cors_max_age: int = 5,
cors_methods: str = "",
cors_origins: str = "",
cors_origins: Union[str, List[str]] = "",
cors_send_wildcard: bool = False,
cors_supports_credentials: bool = False,
cors_vary_header: bool = True,
Expand Down
12 changes: 9 additions & 3 deletions sanic_ext/extensions/http/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,25 @@ def _get_allow_origins(app: Sanic) -> Tuple[re.Pattern, ...]:


def _parse_allow_origins(
value: Union[str, re.Pattern]
value: Union[str, re.Pattern, List[Union[str, re.Pattern]]]
) -> Tuple[re.Pattern, ...]:
origins: Optional[Union[List[str], List[re.Pattern]]] = None
origins: Optional[
Union[List[str], List[re.Pattern], List[Union[str, re.Pattern]]]
] = None
if value and isinstance(value, str):
if value == "*":
origins = [WILDCARD_PATTERN]
else:
origins = value.split(",")
elif isinstance(value, re.Pattern):
origins = [value]
elif isinstance(value, list):
origins = value

return tuple(
pattern if isinstance(pattern, re.Pattern) else re.compile(pattern)
pattern
if isinstance(pattern, re.Pattern)
else re.compile(re.escape(pattern))
for pattern in (origins or [])
)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sanic-ext
version = 22.6.1
version = 22.6.2
url = http://github.com/sanic-org/sanic-ext/
license = MIT
author = Sanic Community
Expand Down

0 comments on commit b65cd9f

Please sign in to comment.