Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interest in body_peek filter ( access to body without consume it ) #452

Closed
AtsukiTak opened this issue Feb 19, 2020 · 4 comments
Closed

Comments

@AtsukiTak
Copy link
Contributor

I'm interesting in body_peek filter because it will make it easy to implement JSON RPC filters.

What I'd like to implement

let add_rpc_filter = json_rpc::method("add")  // matches "add" rpc method
    .and(json_rpc::params::<(u8, u8)>())  // matches rpc parameter
    .and(authenticate())    // some user defined filter
    .and_then(|a, b| future::ok(a + b))
    .and_then(json_rpc::reply());

let another_rpc_filter = ...;

let rpc_filter = filters::path::("rpc")
    .and(add_rpc_filter.or(another_rpc_filter));

The hard part

json_rpc::method and json_rpc::params filters need to access Body. But it will panic when Body is accessed more than once.

Ideas to solve it

  1. To access Body using filters::body::body_peek or filters::body::json_peek filter.
    Such filter allow us to access Body more than once.

  2. Add extension to store extracted Body data.
    Perhaps, this is better than above but I'm not sure how to do it without direct use of hyper.
    According to Consider removal of warp::ext::set #222 , I should use Service directory and if my understand is correct, I should provide the mechanism almost same with warp's server module by myself. Is it right?

  3. Do you have any ideas?
    I probably don't understand warp enough. So if you let me know another idea, I'm very very happy!

Thank you!

@AtsukiTak
Copy link
Contributor Author

I'm working on warp::ext::with_mut(impl Fn(&mut Extensions)) .
#222 (comment)

@AtsukiTak
Copy link
Contributor Author

I have created PR #457 but it might have some issues...
So for now, I personally use custom Service which wraps FilteredService. It seems to work well :)

@haarts
Copy link

haarts commented May 12, 2020

@AtsukiTak could you give me a nudge in the right direction? Perhaps a code snippet? I'm struggling.

@AtsukiTak
Copy link
Contributor Author

@haarts Hi. I chose solution 2 listed above.

  1. Add extension to store extracted Body data

To do that, what I did are

  1. convert Filter into Service using warp::serivce.
  2. prepare a space for extracted body. sample code

After that, when I want to touch request body,

  1. get from that space if it is filled.
  2. extract from request.

sample code

I hope this would help you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants