Skip to main content

Cors

Struct Cors 

Source
pub struct Cors { /* private fields */ }

Implementations§

Source§

impl Cors

Source

pub fn new() -> Self

Deny everything until told otherwise: no origins, no credentials.

Starting closed means a missing line of configuration fails a request in the browser console, where somebody will see it, rather than opening the API to the world.

Source

pub fn permissive() -> Self

Any origin, any method, any header, no credentials.

Right for a genuinely public API. Wrong for anything that reads a session cookie, because the moment allow_credentials is added this becomes “every site on the internet may act as the logged-in user”.

Source

pub fn from_config(config: &Config) -> Self

Read config/cors.json, with Laravel’s key names.

{
  "paths": ["api/*"],
  "allowed_origins": "${CORS_ALLOWED_ORIGINS:*}",
  "allowed_methods": ["*"],
  "allowed_headers": ["*"],
  "exposed_headers": [],
  "max_age": 0,
  "supports_credentials": false
}

Every list accepts either a JSON array or one comma-separated string, which is what makes it settable from .env: a variable can only hold a string.

Source

pub fn paths<I, S>(self, paths: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Apply only under these paths, Laravel-style: api/*, sanctum/csrf-cookie.

Source

pub fn allow_origins<I, S>(self, origins: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Allow these origins. A single * inside a pattern matches one label: https://*.example.com allows https://app.example.com and not https://example.com.evil.net.

Source

pub fn allow_any_origin(self) -> Self

Source

pub fn allow_origin_if( self, allow: impl Fn(&str) -> bool + Send + Sync + 'static, ) -> Self

Decide per origin — for a list that lives in a database, say.

Source

pub fn allow_methods(self, methods: impl IntoIterator<Item = Method>) -> Self

Source

pub fn allow_headers<I, S>(self, headers: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Source

pub fn expose_headers<I, S>(self, headers: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Let scripts read these response headers. By default a browser exposes only the handful the specification calls “safelisted” — a custom X-Request-Id or X-RateLimit-Remaining is invisible until listed.

Source

pub fn allow_credentials(self) -> Self

Allow cookies and Authorization headers to travel with the request.

Source

pub fn max_age(self, duration: Duration) -> Self

How long a browser may cache a preflight answer.

Trait Implementations§

Source§

impl Clone for Cors

Source§

fn clone(&self) -> Cors

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Cors

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Middleware for Cors

Source§

fn handle(&self, request: Request, next: Next) -> BoxFuture<Response>

Auto Trait Implementations§

§

impl !RefUnwindSafe for Cors

§

impl !UnwindSafe for Cors

§

impl Freeze for Cors

§

impl Send for Cors

§

impl Sync for Cors

§

impl Unpin for Cors

§

impl UnsafeUnpin for Cors

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.