1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00
actix-extras/actix-cors/src/lib.rs

69 lines
2.1 KiB
Rust
Raw Normal View History

2020-10-07 12:32:27 +02:00
//! 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()`,
//! `Scope::wrap()`, or `Resource::wrap()` methods.
2018-01-10 08:55:42 +01:00
//!
//! This CORS middleware automatically handles `OPTIONS` preflight requests.
2018-01-10 08:55:42 +01:00
//!
//! # Example
//!
//! In this example a custom CORS middleware is registered for the "/index.html" endpoint.
//!
//! ```rust,no_run
//! use actix_cors::Cors;
//! use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};
2018-01-10 08:55:42 +01:00
//!
//! #[get("/index.html")]
2019-11-22 06:49:35 +01:00
//! async fn index(req: HttpRequest) -> &'static str {
//! "<p>Hello World!</p>"
2018-01-10 08:55:42 +01:00
//! }
//!
//! #[actix_web::main]
//! async fn main() -> std::io::Result<()> {
//! HttpServer::new(|| {
//! let cors = Cors::new()
2019-03-24 05:29:16 +01:00
//! .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()
//! })
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)
2019-11-21 05:54:07 +01:00
//! .max_age(3600)
//! .finish();
//!
//! 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
//! }
//! ```
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(missing_docs, missing_debug_implementations)]
#![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;
pub use all_or_some::AllOrSome;
pub use builder::{Cors, CorsFactory};
pub use error::CorsError;
use inner::{cors, Inner, OriginFn};
pub use middleware::CorsMiddleware;