#actix #cors #web #security #crossorigin

actix-cors

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

17 releases

0.6.0-beta.4 Nov 22, 2021
0.6.0-beta.2 Jun 27, 2021
0.5.4 Dec 31, 2020
0.5.3 Nov 19, 2020
0.1.0 Jun 15, 2019

#18 in HTTP server

Download history 13694/week @ 2021-08-13 13304/week @ 2021-08-20 12074/week @ 2021-08-27 10494/week @ 2021-09-03 13955/week @ 2021-09-10 15858/week @ 2021-09-17 15953/week @ 2021-09-24 17510/week @ 2021-10-01 17609/week @ 2021-10-08 16145/week @ 2021-10-15 15113/week @ 2021-10-22 13009/week @ 2021-10-29 14522/week @ 2021-11-05 15763/week @ 2021-11-12 12791/week @ 2021-11-19 12681/week @ 2021-11-26

58,451 downloads per month
Used in 44 crates (31 directly)

MIT/Apache

50KB
923 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

~22MB
~515K SLoC