hyper-cors
CORS middleware for hyper 1.x.
Cors is a hyper::service::Service
that wraps an inner service and applies the Fetch
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 ;
use Bytes;
use ;
use Full;
use Service;
use ;
async
let svc = builder
.allow_origin
.allow_methods
.max_age
.build;
Configuration
All knobs are chainable on CorsBuilder. Defaults:
| Knob | Default |
|---|---|
allow_origin |
empty list (non-CORS requests still pass through) |
allow_credentials |
false |
allow_methods |
mirror request (Access-Control-Request-Method) |
allow_headers |
mirror request (Access-Control-Request-Headers) |
expose_headers |
empty |
max_age |
not emitted |
vary |
Origin, Access-Control-Request-Method, Access-Control-Request-Headers |
deliver_preflight |
false (preflight is short-circuited) |
deliver_non_allowed_origin |
true |
deliver_non_allowed_origin_websocket_upgrade |
false |
rejection_status |
400 Bad Request |
Per-tenant origins can be served from a cached sync predicate
(AllowOrigin::predicate) or, when the lookup is not in memory, an async predicate
(AllowOrigin::async_predicate).
CorsBuilder::build panics on Fetch-mandated incompatibilities: allow_credentials: true
combined with * in allow_origin, allow_methods, allow_headers, or expose_headers.
Use CorsBuilder::try_build to receive the same diagnostics as a Result and surface
them as a startup error instead.
Examples
Runnable examples live in the examples/ directory:
server.rs— minimal hyper server bound to127.0.0.1:3000, exercising both simple and preflight requests withcurl.multitenant.rs— per-tenant origin allow-list backed by anArc<RwLock<HashSet<_>>>and resolved viaAllowOrigin::async_predicate.
Run with cargo run --example <name>.
License
BSD 2-Clause