1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-25 19:42:48 +01:00

69 lines
2.2 KiB
Rust
Raw Normal View History

2020-10-07 11:32:27 +01:00
//! 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.
2018-01-09 23:55:42 -08:00
//!
//! This CORS middleware automatically handles `OPTIONS` preflight requests.
2018-01-09 23:55:42 -08:00
//!
//! # 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 any breaking changes in
//! the draft spec until it is finalized.
//!
2018-01-09 23:55:42 -08:00
//! # Example
2021-12-08 06:11:13 +00:00
//! ```no_run
//! use actix_cors::Cors;
//! use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
2018-01-09 23:55:42 -08:00
//!
//! #[get("/index.html")]
2019-11-22 11:49:35 +06:00
//! async fn index(req: HttpRequest) -> &'static str {
//! "<p>Hello World!</p>"
2018-01-09 23:55:42 -08:00
//! }
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//! HttpServer::new(|| {
2020-10-19 05:51:31 +01:00
//! let cors = Cors::default()
2022-09-22 18:46:24 +07:00
//! .allowed_origin("https://www.rust-lang.org")
//! .allowed_origin_fn(|origin, _req_head| {
//! origin.as_bytes().ends_with(b".rust-lang.org")
//! })
2019-03-23 21:29:16 -07:00
//! .allowed_methods(vec!["GET", "POST"])
//! .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
//! .allowed_header(http::header::CONTENT_TYPE)
2020-10-19 05:51:31 +01:00
//! .max_age(3600);
//!
//! App::new()
//! .wrap(cors)
//! .service(index)
//! })
//! .bind(("127.0.0.1", 8080))?
//! .run()
//! .await;
2019-03-23 21:29:16 -07:00
//!
//! Ok(())
2018-01-09 23:55:42 -08:00
//! }
//! ```
//!
//! [Private Network Access]: https://wicg.github.io/private-network-access
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, nonstandard_style)]
2021-12-08 06:11:13 +00:00
#![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_cfg))]
mod all_or_some;
mod builder;
mod error;
mod inner;
mod middleware;
2020-10-19 05:51:31 +01:00
use all_or_some::AllOrSome;
pub use builder::Cors;
pub use error::CorsError;
2020-10-19 05:51:31 +01:00
use inner::{Inner, OriginFn};
pub use middleware::CorsMiddleware;