Module actix_web::middleware::cors
[−]
[src]
Cross-origin resource sharing (CORS) for Actix applications
CORS middleware could be used with application and with resource. First you need to construct CORS middleware instance.
To construct a cors:
- Call
Cors::build
to start building. - Use any of the builder methods to set fields in the backend.
- Call finish to retrieve the constructed backend.
Cors middleware could be used as parameter for Application::middleware()
or
Resource::middleware()
methods. But you have to use Cors::register()
method to
support preflight OPTIONS request.
Example
use http::header; use actix_web::middleware::cors; fn index(mut req: HttpRequest) -> &'static str { "Hello world" } fn main() { let app = Application::new() .resource("/index.html", |r| { cors::Cors::build() // <- Construct CORS middleware .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) .finish().expect("Can not create CORS middleware") .register(r); // <- Register CORS middleware r.method(Method::GET).f(|_| httpcodes::HTTPOk); r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed); }) .finish(); }
In this example custom CORS middleware get registered for "/index.html" endpoint.
Cors middleware automatically handle OPTIONS preflight request.
Structs
Cors |
|
CorsBuilder |
Structure that follows the builder pattern for building |
Enums
AllOrSome |
An enum signifying that some of type T is allowed, or |
CorsBuilderError |
A set of errors that can occur during building CORS middleware |
CorsError |
A set of errors that can occur during processing CORS |