30 releases

0.7.1 Mar 11, 2025
0.7.0 Jan 6, 2024
0.6.5 Dec 6, 2023
0.6.4 Oct 28, 2022
0.1.0 Jun 15, 2019

#5 in HTTP server

Download history 41706/week @ 2024-12-20 34156/week @ 2024-12-27 85305/week @ 2025-01-03 100112/week @ 2025-01-10 89294/week @ 2025-01-17 94507/week @ 2025-01-24 97483/week @ 2025-01-31 110034/week @ 2025-02-07 94744/week @ 2025-02-14 103943/week @ 2025-02-21 96923/week @ 2025-02-28 102127/week @ 2025-03-07 105601/week @ 2025-03-14 101574/week @ 2025-03-21 96951/week @ 2025-03-28 105766/week @ 2025-04-04

432,638 downloads per month
Used in 182 crates (135 directly)

MIT/Apache

60KB
1K SLoC

actix-cors

crates.io Documentation Version MIT or Apache 2.0 licensed
Dependency Status Download Chat on Discord

Cross-Origin Resource Sharing (CORS) controls for Actix Web.

This middleware can be applied to both applications and resources. Once built, a Cors builder can be used as an argument for Actix Web's App::wrap(), Scope::wrap(), or Resource::wrap() methods.

This CORS middleware automatically handles OPTIONS preflight requests.

Crate Features

  • draft-private-network-access: ⚠️ Unstable. Adds opt-in support for the Private Network Access spec extensions. This feature is unstable since it will follow breaking changes in the draft spec until it is finalized.

Example

use actix_cors::Cors;
use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};

#[get("/index.html")]
async fn index(req: HttpRequest) -> &'static str {
    "<p>Hello World!</p>"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let cors = Cors::default()
            .allowed_origin("https://www.rust-lang.org")
            .allowed_origin_fn(|origin, _req_head| {
                origin.as_bytes().ends_with(b".rust-lang.org")
            })
            .allowed_methods(vec!["GET", "POST"])
            .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
            .allowed_header(http::header::CONTENT_TYPE)
            .max_age(3600);

        App::new()
            .wrap(cors)
            .service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await;

    Ok(())
}

Documentation & Resources

Dependencies

~16–26MB
~448K SLoC