Available on crate feature
cors only.Expand description
Middleware which adds headers for CORS.
§Example
use http::{Request, Response, Method, header}; use http_body_util::Full; use bytes::Bytes; use tower::{ServiceBuilder, ServiceExt, Service}; use tower_http::cors::{Any, CorsLayer}; use std::convert::Infallible; async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> { Ok(Response::new(Full::default())) } let cors = CorsLayer::new() // allow `GET` and `POST` when accessing the resource .allow_methods([Method::GET, Method::POST]) // allow requests from any origin .allow_origin(Any); let mut service = ServiceBuilder::new() .layer(cors) .service_fn(handle); let request = Request::builder() .header(header::ORIGIN, "https://example.com") .body(Full::default()) .unwrap(); let response = service .ready() .await? .call(request) .await?; assert_eq!( response.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN).unwrap(), "*", );Structs§
- Allow
Credentials - Holds configuration for how to set the
Access-Control-Allow-Credentialsheader. - Allow
Headers - Holds configuration for how to set the
Access-Control-Allow-Headersheader. - Allow
Methods - Holds configuration for how to set the
Access-Control-Allow-Methodsheader. - Allow
Origin - Holds configuration for how to set the
Access-Control-Allow-Originheader. - Allow
Private Network - Holds configuration for how to set the
Access-Control-Allow-Private-Networkheader. - Any
- Represents a wildcard value (
*) used with some CORS headers such asCorsLayer::allow_methods. - Cors
- Middleware which adds headers for CORS.
- Cors
Layer - Layer that applies the
Corsmiddleware which adds headers for CORS. - Expose
Headers - Holds configuration for how to set the
Access-Control-Expose-Headersheader. - MaxAge
- Holds configuration for how to set the
Access-Control-Max-Ageheader. - Response
Future - Response future for
Cors. - Vary
- Holds configuration for how to set the
Varyheader.
Functions§
- any
Deprecated - Represents a wildcard value (
*) used with some CORS headers such asCorsLayer::allow_methods. - preflight_
request_ headers - Returns an iterator over the three request headers that may be involved in a CORS preflight request.