[−][src]Crate actix_cors
Cross-Origin Resource Sharing (CORS) controls for Actix web.
This middleware can be applied to both applications and resources.
Once built, CorsFactory
can be used as a
parameter for actix-web App::wrap()
, Resource::wrap()
or
Scope::wrap()
methods.
This CORS middleware automatically handles OPTIONS
preflight requests.
Example
In this example a custom CORS middleware is registered for the "/index.html" endpoint.
use actix_cors::Cors; use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer}; async fn index(req: HttpRequest) -> &'static str { "Hello world" } fn main() -> std::io::Result<()> { HttpServer::new(|| App::new() .wrap( Cors::new() // <- Construct CORS middleware builder .allowed_origin("https://www.rust-lang.org/") .allowed_origin_fn(|req| { req.headers .get(http::header::ORIGIN) .map(http::HeaderValue::as_bytes) .filter(|b| b.ends_with(b".rust-lang.org")) .is_some() }) .allowed_methods(vec!["GET", "POST"]) .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) .allowed_header(http::header::CONTENT_TYPE) .max_age(3600) .finish()) .service( web::resource("/index.html") .route(web::get().to(index)) .route(web::head().to(|| HttpResponse::MethodNotAllowed())) )) .bind("127.0.0.1:8080")?; Ok(()) }
Structs
Cors | Builder for |
CorsFactory | Middleware for Cross-Origin Resource Sharing support. |
CorsMiddleware | Service wrapper for Cross-Origin Resource Sharing support. |
Enums
AllOrSome | An enum signifying that some of type T is allowed, or |
CorsError | A set of errors that can occur as a result of processing CORS. |