2020-10-07 12:32:27 +02:00
|
|
|
//! Cross-Origin Resource Sharing (CORS) controls for Actix Web.
|
2020-07-03 18:56:46 +02:00
|
|
|
//!
|
2020-10-19 06:51:31 +02:00
|
|
|
//! This middleware can be applied to both applications and resources. Once built, a
|
|
|
|
//! [`Cors`] builder can be used as an argument for Actix Web's `App::wrap()`,
|
2020-10-08 12:50:56 +02:00
|
|
|
//! `Scope::wrap()`, or `Resource::wrap()` methods.
|
2018-01-10 08:55:42 +01:00
|
|
|
//!
|
2020-07-03 18:56:46 +02:00
|
|
|
//! This CORS middleware automatically handles `OPTIONS` preflight requests.
|
2018-01-10 08:55:42 +01:00
|
|
|
//!
|
|
|
|
//! # Example
|
2021-12-08 07:11:13 +01:00
|
|
|
//! ```no_run
|
2019-06-15 05:34:16 +02:00
|
|
|
//! use actix_cors::Cors;
|
2020-10-08 12:50:56 +02:00
|
|
|
//! use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
|
2018-01-10 08:55:42 +01:00
|
|
|
//!
|
2020-10-08 12:50:56 +02:00
|
|
|
//! #[get("/index.html")]
|
2019-11-22 06:49:35 +01:00
|
|
|
//! async fn index(req: HttpRequest) -> &'static str {
|
2020-10-08 12:50:56 +02:00
|
|
|
//! "<p>Hello World!</p>"
|
2018-01-10 08:55:42 +01:00
|
|
|
//! }
|
|
|
|
//!
|
2020-10-08 12:50:56 +02:00
|
|
|
//! #[actix_web::main]
|
|
|
|
//! async fn main() -> std::io::Result<()> {
|
|
|
|
//! HttpServer::new(|| {
|
2020-10-19 06:51:31 +02:00
|
|
|
//! let cors = Cors::default()
|
2019-03-24 05:29:16 +01:00
|
|
|
//! .allowed_origin("https://www.rust-lang.org/")
|
2020-10-19 20:30:46 +02:00
|
|
|
//! .allowed_origin_fn(|origin, _req_head| {
|
|
|
|
//! origin.as_bytes().ends_with(b".rust-lang.org")
|
2020-09-25 01:36:53 +02:00
|
|
|
//! })
|
2019-03-24 05:29:16 +01:00
|
|
|
//! .allowed_methods(vec!["GET", "POST"])
|
|
|
|
//! .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
|
|
|
|
//! .allowed_header(http::header::CONTENT_TYPE)
|
2020-10-19 06:51:31 +02:00
|
|
|
//! .max_age(3600);
|
2020-10-08 12:50:56 +02:00
|
|
|
//!
|
|
|
|
//! App::new()
|
|
|
|
//! .wrap(cors)
|
|
|
|
//! .service(index)
|
|
|
|
//! })
|
|
|
|
//! .bind(("127.0.0.1", 8080))?
|
|
|
|
//! .run()
|
|
|
|
//! .await;
|
2019-03-24 05:29:16 +01:00
|
|
|
//!
|
|
|
|
//! Ok(())
|
2018-01-10 08:55:42 +01:00
|
|
|
//! }
|
|
|
|
//! ```
|
2020-07-03 18:56:46 +02:00
|
|
|
|
2020-10-08 12:50:56 +02:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
2021-12-08 07:11:13 +01:00
|
|
|
#![warn(future_incompatible, missing_docs, missing_debug_implementations)]
|
2020-10-08 12:50:56 +02:00
|
|
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
|
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
|
|
|
|
|
|
|
mod all_or_some;
|
|
|
|
mod builder;
|
|
|
|
mod error;
|
|
|
|
mod inner;
|
|
|
|
mod middleware;
|
|
|
|
|
2020-10-19 06:51:31 +02:00
|
|
|
use all_or_some::AllOrSome;
|
|
|
|
pub use builder::Cors;
|
2020-10-08 12:50:56 +02:00
|
|
|
pub use error::CorsError;
|
2020-10-19 06:51:31 +02:00
|
|
|
use inner::{Inner, OriginFn};
|
2020-10-08 12:50:56 +02:00
|
|
|
pub use middleware::CorsMiddleware;
|