pub struct CorsLayer { /* private fields */ }Expand description
A CORS policy. See the module docs.
Implementations§
Source§impl CorsLayer
impl CorsLayer
Sourcepub fn new() -> Self
pub fn new() -> Self
A closed policy — no origin allowed yet. Add some with
allow_origin / allow_any_origin
(and methods/headers/credentials as your API needs). Defaults: methods
GET + POST, no extra headers, no credentials, no preflight cache.
Sourcepub fn permissive() -> Self
pub fn permissive() -> Self
A wide-open policy — any origin, the common verbs (GET/POST/PUT/DELETE/PATCH), any request header, no credentials, a one-day preflight cache. Handy for development; tighten it for production.
Sourcepub fn allow_any_origin(self) -> Self
pub fn allow_any_origin(self) -> Self
Allow any origin. The response still echoes the concrete Origin, so
this composes correctly with allow_credentials.
Sourcepub fn allow_origin(self, origin: impl Into<String>) -> Self
pub fn allow_origin(self, origin: impl Into<String>) -> Self
Add an allowed origin (exact match, e.g. "https://app.example.com").
Calling this after allow_any_origin narrows
back to a list.
Sourcepub fn allow_methods(self, methods: impl IntoIterator<Item = Verb>) -> Self
pub fn allow_methods(self, methods: impl IntoIterator<Item = Verb>) -> Self
Set the methods advertised in preflight responses
(Access-Control-Allow-Methods). Default: GET, POST.
Sourcepub fn allow_any_header(self) -> Self
pub fn allow_any_header(self) -> Self
Allow any request header — mirrors Access-Control-Request-Headers.
Sourcepub fn allow_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> Self
pub fn allow_headers( self, headers: impl IntoIterator<Item = HeaderName>, ) -> Self
Set the allowed request headers explicitly (Access-Control-Allow-Headers).
Sourcepub fn expose_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> Self
pub fn expose_headers( self, headers: impl IntoIterator<Item = HeaderName>, ) -> Self
Response headers JS may read beyond the CORS-safelisted ones
(Access-Control-Expose-Headers).
Sourcepub fn allow_credentials(self, yes: bool) -> Self
pub fn allow_credentials(self, yes: bool) -> Self
Whether credentialed requests (cookies, HTTP auth) are permitted
(Access-Control-Allow-Credentials). When on, the wildcard * token
is never sent — the policy always echoes concrete origin/header values,
which is what the spec requires.