#actix #cors #web #security #crossorigin

actix-cors

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

12 unstable releases (4 breaking)

0.5.3 Nov 19, 2020
0.5.0 Oct 19, 2020
0.3.0-alpha.1 Mar 11, 2020
0.2.0 Dec 20, 2019
0.1.0 Jun 15, 2019

#15 in HTTP server

Download history 7624/week @ 2020-08-13 7565/week @ 2020-08-20 7200/week @ 2020-08-27 7453/week @ 2020-09-03 8669/week @ 2020-09-10 7021/week @ 2020-09-17 8667/week @ 2020-09-24 8087/week @ 2020-10-01 7793/week @ 2020-10-08 8005/week @ 2020-10-15 8886/week @ 2020-10-22 8991/week @ 2020-10-29 8906/week @ 2020-11-05 8351/week @ 2020-11-12 8880/week @ 2020-11-19 7696/week @ 2020-11-26

34,213 downloads per month
Used in 28 crates (17 directly)

MIT/Apache

48KB
902 lines

actix-cors

Cross-origin resource sharing (CORS) for Actix Web.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status Join the chat at https://gitter.im/actix/actix-web

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

~20MB
~479K SLoC