1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

Let CSRF's allowed_origin() be specified as a type supporting Into<String>

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<String>` 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.
This commit is contained in:
Brandur 2018-04-21 08:41:06 -07:00
parent 2adf8a3a48
commit 59244b203c

View File

@ -150,8 +150,8 @@ impl CsrfFilter {
/// Add an origin that is allowed to make requests. Will be verified /// Add an origin that is allowed to make requests. Will be verified
/// against the `Origin` request header. /// against the `Origin` request header.
pub fn allowed_origin(mut self, origin: &str) -> CsrfFilter { pub fn allowed_origin<T: Into<String>>(mut self, origin: T) -> CsrfFilter {
self.origins.insert(origin.to_owned()); self.origins.insert(origin.into());
self self
} }