actix_cors/lib.rs
1//! Cross-Origin Resource Sharing (CORS) controls for Actix Web.
2//!
3//! This middleware can be applied to both applications and resources. Once built, a [`Cors`]
4//! builder can be used as an argument for Actix Web's `App::wrap()`, `Scope::wrap()`, or
5//! `Resource::wrap()` methods.
6//!
7//! This CORS middleware automatically handles `OPTIONS` preflight requests.
8//!
9//! # Crate Features
10//! - `draft-private-network-access`: ⚠️ Unstable. Adds opt-in support for the [Private Network
11//! Access] spec extensions. This feature is unstable since it will follow breaking changes in the
12//! draft spec until it is finalized.
13//!
14//! # Example
15//! ```no_run
16//! use actix_cors::Cors;
17//! use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
18//!
19//! #[get("/index.html")]
20//! async fn index(req: HttpRequest) -> &'static str {
21//! "<p>Hello World!</p>"
22//! }
23//!
24//! #[actix_web::main]
25//! async fn main() -> std::io::Result<()> {
26//! HttpServer::new(|| {
27//! let cors = Cors::default()
28//! .allowed_origin("https://www.rust-lang.org")
29//! .allowed_origin_fn(|origin, _req_head| {
30//! origin.as_bytes().ends_with(b".rust-lang.org")
31//! })
32//! .allowed_methods(vec!["GET", "POST"])
33//! .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
34//! .allowed_header(http::header::CONTENT_TYPE)
35//! .max_age(3600);
36//!
37//! App::new()
38//! .wrap(cors)
39//! .service(index)
40//! })
41//! .bind(("127.0.0.1", 8080))?
42//! .run()
43//! .await;
44//!
45//! Ok(())
46//! }
47//! ```
48//!
49//! [Private Network Access]: https://wicg.github.io/private-network-access
50
51#![forbid(unsafe_code)]
52#![deny(rust_2018_idioms, nonstandard_style)]
53#![warn(future_incompatible, missing_docs, missing_debug_implementations)]
54#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
55#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
56#![cfg_attr(docsrs, feature(doc_auto_cfg))]
57
58mod all_or_some;
59mod builder;
60mod error;
61mod inner;
62mod middleware;
63
64use all_or_some::AllOrSome;
65pub use builder::Cors;
66pub use error::CorsError;
67use inner::{Inner, OriginFn};
68pub use middleware::CorsMiddleware;