hyper_cors/lib.rs
1//! CORS middleware for hyper 1.x.
2//!
3//! [`Cors`] is a [`hyper::service::Service`] that wraps an inner service and applies the
4//! [Cross-Origin Resource Sharing][mdn] protocol to every request.
5//!
6//! Preflight requests (`OPTIONS` with `Access-Control-Request-Method`) are short-circuited
7//! by default, so the inner service never sees them.
8//!
9//! # Example
10//!
11//! ```
12//! use std::{convert::Infallible, time::Duration};
13//! use http::Method;
14//! use hyper::{Request, Response, service::Service, body::Incoming};
15//! use http_body_util::Full;
16//! use bytes::Bytes;
17//! use hyper_cors::{Any, Cors};
18//!
19//! async fn handle(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
20//! Ok(Response::new(Full::new(Bytes::from("ok"))))
21//! }
22//!
23//! # async fn run() {
24//! let svc = hyper_cors::builder()
25//! .allow_origin(Any)
26//! .allow_methods([Method::GET, Method::POST])
27//! .max_age(Duration::from_secs(600))
28//! .build(handle);
29//! # }
30//! ```
31//!
32//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
33
34#![forbid(unsafe_code)]
35#![warn(missing_debug_implementations)]
36#![warn(missing_docs)]
37
38mod allow_credentials;
39mod allow_headers;
40mod allow_methods;
41mod allow_origin;
42mod config;
43mod cors;
44mod expose_headers;
45mod header_buf;
46mod headers;
47mod max_age;
48mod util;
49mod vary;
50
51pub use allow_credentials::AllowCredentials;
52pub use allow_headers::AllowHeaders;
53pub use allow_methods::AllowMethods;
54pub use allow_origin::{AllowOrigin, Any};
55pub use config::{ConfigError, CorsBuilder};
56pub use cors::{builder, Cors};
57pub use expose_headers::ExposeHeaders;
58pub use max_age::MaxAge;
59pub use vary::Vary;