    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68

    //! Cross-Origin Resource Sharing (CORS) controls for Actix Web.
    //!
    //! 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()`, `Scope::wrap()`, or
    //! `Resource::wrap()` methods.
    //!
    //! This CORS middleware automatically handles `OPTIONS` preflight requests.
    //!
    //! # Crate Features
    //! - `draft-private-network-access`: ⚠️ Unstable. Adds opt-in support for the [Private Network
    //!   Access] spec extensions. This feature is unstable since it will follow breaking changes in the
    //!   draft spec until it is finalized.
    //!
    //! # Example
    //! ```no_run
    //! use actix_cors::Cors;
    //! use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
    //!
    //! #[get("/index.html")]
    //! async fn index(req: HttpRequest) -> &'static str {
    //!     "<p>Hello World!</p>"
    //! }
    //!
    //! #[actix_web::main]
    //! async fn main() -> std::io::Result<()> {
    //!     HttpServer::new(|| {
    //!         let cors = Cors::default()
    //!             .allowed_origin("https://www.rust-lang.org")
    //!             .allowed_origin_fn(|origin, _req_head| {
    //!                 origin.as_bytes().ends_with(b".rust-lang.org")
    //!             })
    //!             .allowed_methods(vec!["GET", "POST"])
    //!             .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
    //!             .allowed_header(http::header::CONTENT_TYPE)
    //!             .max_age(3600);
    //!
    //!         App::new()
    //!             .wrap(cors)
    //!             .service(index)
    //!     })
    //!     .bind(("127.0.0.1", 8080))?
    //!     .run()
    //!     .await;
    //!
    //!     Ok(())
    //! }
    //! ```
    //!
    //! [Private Network Access]: https://wicg.github.io/private-network-access

    #![forbid(unsafe_code)]
    #![deny(rust_2018_idioms, nonstandard_style)]
    #![warn(future_incompatible, missing_docs, missing_debug_implementations)]
    #![doc(html_logo_url = "https://actix.rs/img/logo.png")]
    #![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
    #![cfg_attr(docsrs, feature(doc_auto_cfg))]

    mod all_or_some;
    mod builder;
    mod error;
    mod inner;
    mod middleware;

    use crate::{
        all_or_some::AllOrSome,
        inner::{Inner, OriginFn},
    };
    pub use crate::{builder::Cors, error::CorsError, middleware::CorsMiddleware};