From 59244b203c1948b9ff8986b066b74ceaa83826a6 Mon Sep 17 00:00:00 2001 From: Brandur Date: Sat, 21 Apr 2018 08:41:06 -0700 Subject: [PATCH] Let CSRF's `allowed_origin()` be specified as a type supporting `Into` A very minor addition: I'm using this middleware on specific resources, and given a non-static string, I often have to `clone()` already to get a string into a closure. Take this code for example: ``` rust let server = actix_web::server::new(move || { let csrf_origin_graphql = csrf_origin.clone(); ... .resource("/graphql", move |r| { r.middleware( csrf::CsrfFilter::new().allowed_origin(csrf_origin_graphql.as_str()), ); r.method(Method::POST).a(graphql::handlers::graphql_post); }) ``` Letting `allowed_origin()` take an `Into` instead of `&str` would prevent a second `clone()` in the code above, and also make the code a little nicer to read (you eliminate the `.as_str()` above). This is a pattern that seems to be common throughout actix-web already anyway, so it should also be fine to have here. --- src/middleware/csrf.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/middleware/csrf.rs b/src/middleware/csrf.rs index b0eb4a3d..9ff23b53 100644 --- a/src/middleware/csrf.rs +++ b/src/middleware/csrf.rs @@ -150,8 +150,8 @@ impl CsrfFilter { /// Add an origin that is allowed to make requests. Will be verified /// against the `Origin` request header. - pub fn allowed_origin(mut self, origin: &str) -> CsrfFilter { - self.origins.insert(origin.to_owned()); + pub fn allowed_origin>(mut self, origin: T) -> CsrfFilter { + self.origins.insert(origin.into()); self }