Skip to main content

Crate hyper_cors

Crate hyper_cors 

Source
Expand description

CORS middleware for hyper 1.x.

Cors is a hyper::service::Service that wraps an inner service and applies the Cross-Origin Resource Sharing protocol to every request.

Preflight requests (OPTIONS with Access-Control-Request-Method) are short-circuited by default, so the inner service never sees them.

§Example

use std::{convert::Infallible, time::Duration};
use http::Method;
use hyper::{Request, Response, service::Service, body::Incoming};
use http_body_util::Full;
use bytes::Bytes;
use hyper_cors::{Any, Cors};

async fn handle(_req: Request<Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
    Ok(Response::new(Full::new(Bytes::from("ok"))))
}

let svc = hyper_cors::builder()
    .allow_origin(Any)
    .allow_methods([Method::GET, Method::POST])
    .max_age(Duration::from_secs(600))
    .build(handle);

Structs§

AllowCredentials
Configuration for the Access-Control-Allow-Credentials header.
AllowHeaders
Configuration for the Access-Control-Allow-Headers header.
AllowMethods
Configuration for the Access-Control-Allow-Methods header.
AllowOrigin
Configuration for the Access-Control-Allow-Origin header.
Any
Sentinel for the wildcard * origin.
ConfigError
Error returned by CorsBuilder::try_build when the configuration violates a Fetch-mandated incompatibility.
Cors
CORS middleware.
CorsBuilder
Builder for Cors.
ExposeHeaders
Configuration for the Access-Control-Expose-Headers header.
MaxAge
Configuration for the Access-Control-Max-Age header.
Vary
Configuration for the Vary header.

Functions§

builder
Returns a new CorsBuilder with default settings.