MCPcopy Create free account
hub / github.com/encode/starlette / CORSMiddleware

Class CORSMiddleware

starlette/middleware/cors.py:15–177  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15class CORSMiddleware:
16 def __init__(
17 self,
18 app: ASGIApp,
19 allow_origins: Collection[str] = (),
20 allow_methods: Collection[str] = ("GET",),
21 allow_headers: Collection[str] = (),
22 allow_credentials: bool = False,
23 allow_origin_regex: str | None = None,
24 allow_private_network: bool = False,
25 expose_headers: Collection[str] = (),
26 max_age: int = 600,
27 ) -> None:
28 if "*" in allow_methods:
29 allow_methods = ALL_METHODS
30
31 compiled_allow_origin_regex = None
32 if allow_origin_regex is not None:
33 compiled_allow_origin_regex = re.compile(allow_origin_regex)
34
35 allow_all_origins = "*" in allow_origins
36 allow_all_headers = "*" in allow_headers
37 preflight_explicit_allow_origin = not allow_all_origins or allow_credentials
38
39 simple_headers: dict[str, str] = {}
40 if allow_all_origins:
41 simple_headers["Access-Control-Allow-Origin"] = "*"
42 if allow_credentials:
43 simple_headers["Access-Control-Allow-Credentials"] = "true"
44 if expose_headers:
45 simple_headers["Access-Control-Expose-Headers"] = ", ".join(expose_headers)
46
47 preflight_headers: dict[str, str] = {
48 "Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers, Access-Control-Request-Private-Network" # noqa: E501 # fmt: skip
49 }
50 if not preflight_explicit_allow_origin:
51 preflight_headers["Access-Control-Allow-Origin"] = "*"
52 preflight_headers.update(
53 {
54 "Access-Control-Allow-Methods": ", ".join(allow_methods),
55 "Access-Control-Max-Age": str(max_age),
56 }
57 )
58 allow_headers = sorted(SAFELISTED_HEADERS | set(allow_headers))
59 if allow_headers and not allow_all_headers:
60 preflight_headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
61 if allow_credentials:
62 preflight_headers["Access-Control-Allow-Credentials"] = "true"
63
64 self.app = app
65 self.allow_origins = allow_origins
66 self.allow_methods = allow_methods
67 self.allow_headers = [h.lower() for h in allow_headers]
68 self.allow_all_origins = allow_all_origins
69 self.allow_all_headers = allow_all_headers
70 self.allow_credentials = allow_credentials
71 self.preflight_explicit_allow_origin = preflight_explicit_allow_origin
72 self.allow_origin_regex = compiled_allow_origin_regex

Callers 1

Calls

no outgoing calls

Tested by 1