Actix-web: Cookie auth without cloning HttpRequest

Created on 27 Sep 2018  路  5Comments  路  Source: actix/actix-web

I tried a cookie auth using request body by IdentityService middleware, following the example.

The compilation fails.

error[E0621]: explicit lifetime required in the type of `req`
  --> src/main.rs:41:5
   |
39 |   fn login(req: &HttpRequest) -> FutureResponse<HttpResponse> {
   |            --- consider changing the type of `req` to `&'static actix_web::HttpRequest`
40 |       // https://actix.rs/api/actix-web/stable/actix_web/trait.HttpMessage.html#method.urlencoded
41 | /     Box::new(
42 | |         req.urlencoded::<LoginParameters>()
43 | |             .from_err()
44 | |             .and_then(move |params| {
...  |
48 | |             }),
49 | |     )
   | |_____^ lifetime `'static` required

That's because the request cannot be accessed in the closure of a Future. This problem can be avoided by cloning the request, but I don't want to make an unnecessary clone.

Is there a good way to make a cookie auth using request body, without cloning the request?

Most helpful comment

@yoshrc this is limitation of current way futures do things, you need to avoid tying it to lifetimes as handler requires 'static lifetime

I'm surprised though about size of HttpRequest, wonder if it is with alignment/padding

All 5 comments

you don't have choice, you have to clone request if you use it inside .and_then()

Just to add that cloning request is relatively cheap as you're mostly cloning Rc pointers

@fafhrd91 @DoumanAsh

Thank you. But I don't think cloning HttpRequest is ignorable cost. std::mem::size_of::<HttpRequest>() returns 248. This size seems to come from ResourceInfo of HttpRequest.

Is it inevitable to copy 248 bytes whenever I use IdentityService?

you need to copy request if you want to use it with future combinators

@yoshrc this is limitation of current way futures do things, you need to avoid tying it to lifetimes as handler requires 'static lifetime

I'm surprised though about size of HttpRequest, wonder if it is with alignment/padding

Was this page helpful?
0 / 5 - 0 ratings