#actix #cors #web #security #crossorigin

actix-cors

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

13 unstable releases (4 breaking)

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

#7 in HTTP server

Download history 9079/week @ 2020-11-14 8000/week @ 2020-11-21 8600/week @ 2020-11-28 8709/week @ 2020-12-05 8355/week @ 2020-12-12 5271/week @ 2020-12-19 3916/week @ 2020-12-26 7184/week @ 2021-01-02 10540/week @ 2021-01-09 9621/week @ 2021-01-16 9709/week @ 2021-01-23 9916/week @ 2021-01-30 8386/week @ 2021-02-06 8712/week @ 2021-02-13 9812/week @ 2021-02-20 12496/week @ 2021-02-27

33,943 downloads per month
Used in 37 crates (24 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
~480K SLoC