Struct clia_ntex_cors::Cors

source ·
pub struct Cors { /* private fields */ }
Expand description

Structure that follows the builder pattern for building Cors middleware structs.

To construct a cors:

  1. Call Cors::build to start building.
  2. Use any of the builder methods to set fields in the backend.
  3. Call finish to retrieve the constructed backend.

Example

use ntex_cors::Cors;
use ntex::http::header;

let cors = Cors::new()
    .allowed_origin("https://www.rust-lang.org/")
    .allowed_methods(vec!["GET", "POST"])
    .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
    .allowed_header(header::CONTENT_TYPE)
    .max_age(3600);

Implementations§

source§

impl Cors

source

pub fn new() -> Self

Build a new CORS middleware instance

source

pub fn default<Err>() -> CorsFactory<Err>

Build a new CORS default middleware

source

pub fn allowed_origin(self, origin: &str) -> Self

Add an origin that are allowed to make requests. Will be verified against the Origin request header.

When All is set, and send_wildcard is set, “*” will be sent in the Access-Control-Allow-Origin response header. Otherwise, the client’s Origin request header will be echoed back in the Access-Control-Allow-Origin response header.

When Some is set, the client’s Origin request header will be checked in a case-sensitive manner.

This is the list of origins in the Resource Processing Model.

Defaults to All.

Builder panics if supplied origin is not valid uri.

source

pub fn allowed_methods<U, M>(self, methods: U) -> Selfwhere U: IntoIterator<Item = M>, Method: TryFrom<M>, <Method as TryFrom<M>>::Error: Into<HttpError>,

Set a list of methods which the allowed origins are allowed to access for requests.

This is the list of methods in the Resource Processing Model.

Defaults to [GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]

source

pub fn allowed_header<H>(self, header: H) -> Selfwhere HeaderName: TryFrom<H>, <HeaderName as TryFrom<H>>::Error: Into<HttpError>,

Set an allowed header

source

pub fn allowed_headers<U, H>(self, headers: U) -> Selfwhere U: IntoIterator<Item = H>, HeaderName: TryFrom<H>, <HeaderName as TryFrom<H>>::Error: Into<HttpError>,

Set a list of header field names which can be used when this resource is accessed by allowed origins.

If All is set, whatever is requested by the client in Access-Control-Request-Headers will be echoed back in the Access-Control-Allow-Headers header.

This is the list of headers in the Resource Processing Model.

Defaults to All.

source

pub fn expose_headers<U, H>(self, headers: U) -> Selfwhere U: IntoIterator<Item = H>, HeaderName: TryFrom<H>, <HeaderName as TryFrom<H>>::Error: Into<HttpError>,

Set a list of headers which are safe to expose to the API of a CORS API specification. This corresponds to the Access-Control-Expose-Headers response header.

This is the list of exposed headers in the Resource Processing Model.

This defaults to an empty set.

source

pub fn max_age(self, max_age: usize) -> Self

Set a maximum time for which this CORS request maybe cached. This value is set as the Access-Control-Max-Age header.

This defaults to None (unset).

source

pub fn send_wildcard(self) -> Self

Set a wildcard origins

If send wildcard is set and the allowed_origins parameter is All, a wildcard Access-Control-Allow-Origin response header is sent, rather than the request’s Origin header.

This is the supports credentials flag in the Resource Processing Model.

This CANNOT be used in conjunction with allowed_origins set to All and allow_credentials set to true. Depending on the mode of usage, this will either result in an Error:: CredentialsWithWildcardOrigin error during ntex launch or runtime.

Defaults to false.

source

pub fn supports_credentials(self) -> Self

Allows users to make authenticated requests

If true, injects the Access-Control-Allow-Credentials header in responses. This allows cookies and credentials to be submitted across domains.

This option cannot be used in conjunction with an allowed_origin set to All and send_wildcards set to true.

Defaults to false.

Builder panics if credentials are allowed, but the Origin is set to “*”. This is not allowed by W3C

source

pub fn disable_vary_header(self) -> Self

Disable Vary header support.

When enabled the header Vary: Origin will be returned as per the W3 implementation guidelines.

Setting this header when the Access-Control-Allow-Origin is dynamically generated (e.g. when there is more than one allowed origin, and an Origin than ‘*’ is returned) informs CDNs and other caches that the CORS headers are dynamic, and cannot be cached.

By default vary header support is enabled.

source

pub fn disable_preflight(self) -> Self

Disable preflight request support.

When enabled cors middleware automatically handles OPTIONS request. This is useful application level middleware.

By default preflight support is enabled.

source

pub fn finish<Err>(self) -> CorsFactory<Err>

Construct cors middleware

Trait Implementations§

source§

impl Default for Cors

source§

fn default() -> Cors

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Cors

§

impl Send for Cors

§

impl Sync for Cors

§

impl Unpin for Cors

§

impl !UnwindSafe for Cors

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.