29 releases

0.7.0 Jan 6, 2024
0.6.5 Dec 6, 2023
0.6.4 Oct 28, 2022
0.6.1 Mar 7, 2022
0.1.0 Jun 15, 2019

#3 in HTTP server

Download history 64400/week @ 2024-03-31 54210/week @ 2024-04-07 58411/week @ 2024-04-14 66121/week @ 2024-04-21 66465/week @ 2024-04-28 61032/week @ 2024-05-05 69042/week @ 2024-05-12 74806/week @ 2024-05-19 67151/week @ 2024-05-26 70191/week @ 2024-06-02 69922/week @ 2024-06-09 67356/week @ 2024-06-16 71596/week @ 2024-06-23 61579/week @ 2024-06-30 61483/week @ 2024-07-07 53503/week @ 2024-07-14

252,091 downloads per month
Used in 136 crates (103 directly)

MIT/Apache

59KB
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

~14–28MB
~463K SLoC