Skip to main content

CorsConfig

Struct CorsConfig 

Source
pub struct CorsConfig {
    pub allow_origins: Vec<String>,
    pub allow_all_origins: bool,
    pub credentials: bool,
}
Expand description

Cross-Origin Resource Sharing (CORS) configuration for an HTTP server.

Controls which origins are allowed to make cross-origin requests, whether credentials are included in responses, and generates appropriate CORS headers.

Instantiated via CorsConfigBuilder to ensure the unsafe credentialed-wildcard combination cannot be represented.

Fields§

§allow_origins: Vec<String>

List of origins allowed to access the server (may contain "*").

§allow_all_origins: bool

Whether all origins ("*") are allowed.

§credentials: bool

Whether credentials (Authorization, cookies, etc.) are included in responses.

Implementations§

Source§

impl CorsConfig

Source

pub fn preflight_response( &self, req_origin: Option<&str>, requested_headers: Option<&str>, allowed_methods: &[Method], ) -> Response<ResponseBody>

Build a CORS preflight response (HTTP 204).

Called for OPTIONS requests. Returns appropriate CORS headers based on the request’s Origin header and this config’s allowed origins.

requested_headers is the incoming preflight’s own Access-Control-Request-Headers value, echoed back verbatim as Access-Control-Allow-Headers — a real cross-origin request is never a “simple request” once it sets a non-safelisted header (content-type: application/json is the common case; none of the three safelisted Content-Type values is JSON), so the browser always preflights it first and blocks the real request outright if the preflight doesn’t confirm the header it’s about to send is allowed. Echoing back exactly what was asked grants nothing broader than the caller already requested. allowed_methods becomes Access-Control-Allow-Methods — the caller passes the same per-path method list it already computes for a plain 405 response, so this never drifts from what the route actually accepts.

Source

pub fn apply_to_response( &self, resp: &mut Response<ResponseBody>, req_origin: Option<&str>, )

Apply CORS headers to a response based on the request’s Origin header.

Called after a handler completes successfully. Mutates the response to add appropriate Access-Control-* headers.

Trait Implementations§

Source§

impl Clone for CorsConfig

Source§

fn clone(&self) -> CorsConfig

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 Debug for CorsConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for CorsConfig

Source§

impl PartialEq for CorsConfig

Source§

fn eq(&self, other: &CorsConfig) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CorsConfig

Auto Trait Implementations§

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.