#actix #cors #web #security #crossorigin

actix-cors

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

27 releases

0.6.4 Oct 28, 2022
0.6.2 Aug 7, 2022
0.6.1 Mar 7, 2022
0.6.0-beta.8 Dec 29, 2021
0.1.0 Jun 15, 2019

#20 in HTTP server

Download history 22413/week @ 2022-12-02 23596/week @ 2022-12-09 21368/week @ 2022-12-16 15146/week @ 2022-12-23 15299/week @ 2022-12-30 22612/week @ 2023-01-06 24051/week @ 2023-01-13 25233/week @ 2023-01-20 26917/week @ 2023-01-27 30371/week @ 2023-02-03 28559/week @ 2023-02-10 29548/week @ 2023-02-17 32579/week @ 2023-02-24 34885/week @ 2023-03-03 36706/week @ 2023-03-10 31071/week @ 2023-03-17

140,792 downloads per month
Used in 88 crates (65 directly)

MIT/Apache

57KB
1K SLoC

actix-cors

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

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Documentation & Resources


lib.rs:

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 any 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(())
}

Dependencies

~16–23MB
~514K SLoC