#actix #cors #web #security #crossorigin

actix-cors

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

24 releases

0.6.1 Mar 7, 2022
0.6.0-beta.8 Dec 29, 2021
0.6.0-beta.4 Nov 22, 2021
0.6.0-beta.2 Jun 27, 2021
0.1.0 Jun 15, 2019

#102 in HTTP server

Download history 14779/week @ 2022-01-30 15587/week @ 2022-02-06 14795/week @ 2022-02-13 15262/week @ 2022-02-20 16160/week @ 2022-02-27 15416/week @ 2022-03-06 15857/week @ 2022-03-13 14851/week @ 2022-03-20 15364/week @ 2022-03-27 16434/week @ 2022-04-03 14610/week @ 2022-04-10 15214/week @ 2022-04-17 15428/week @ 2022-04-24 15303/week @ 2022-05-01 16423/week @ 2022-05-08 16844/week @ 2022-05-15

65,024 downloads per month
Used in 49 crates (36 directly)

MIT/Apache

52KB
963 lines

actix-cors

Cross-origin resource sharing (CORS) 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.

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

~15–23MB
~515K SLoC