reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

adding CookieService as a middleware to handle cookies

Open Lori-Shu opened this issue 7 months ago • 7 comments

tried my best to make methods async,Im not so good at handling the lifetimes from the cookie crate,so some of the methods in the service are sync fn

Lori-Shu avatar May 28 '25 16:05 Lori-Shu

Hey there, thanks for starting! Here's some points:

We want to keep the public API the same, so the builder methods and CookieStore trait say the same.

The goal is to make an internal type, and impl<S: Service<Req>> Service<Req> for CookiesService<S>, which adds cookies to the request, and looks at the response and adds new cookies back to the store.

Then we can wrap the inner service (is it HyperService?), and the H3Service, and then each one will no longer need the duplicated cookie management code. Does that make sense?

thank you for giving me points.I tried to hit them but ran into some problems: the new type should be able to wrap a hyperservice or h3service,however I dont know the concrete type of the wrapped, the problem lead to unable to know the response type,which is defined different in those two Services, because of that,I cant call function like Response::headers(),I point to the Service trait defined in hyper project. I figured one solution is to change the Service trait===>type Response->type ResponseBody and make the response the concrete http::Response,but it may be a big change and it is in hyper not reqwest. If you get other solutions,please hit me again!thank you.

Lori-Shu avatar May 30 '25 17:05 Lori-Shu

the new type should be able to wrap a hyperservice or h3service,however I dont know the concrete type of the wrapped,

That new type can be referenced to tower_http::follow_redirect::FollowRedirect in tower-http. Hmm, that's say we need a new Service type to unify HyperService and H3Client, then this new Service is wrappered in tower_http::follow_redirect::FollowRedirect.

linyihai avatar Jun 01 '25 09:06 linyihai

the new type should be able to wrap a hyperservice or h3service,however I dont know the concrete type of the wrapped,

That new type can be referenced to tower_http::follow_redirect::FollowRedirect in tower-http. Hmm, that's say we need a new Service type to unify HyperService and H3Client, then this new Service is wrappered in tower_http::follow_redirect::FollowRedirect.

yeah,except now the h3client response === http::Response crate::async_impl::body::ResponseBody and the hyperservice response === http::Response hyper::body::Incoming,if I store a var in the new type ,which is S:Service<Req<Body>>,its abstractive and response is to be defined,not http::Response I must use to unwrap headers. And I just find out I used the wrong Service trait in hyper they look so alike.

Lori-Shu avatar Jun 01 '25 12:06 Lori-Shu

I just find out that I could use 'where' to restrict the Response,I have made some changes,somebody please review my code. by the way,I dont like the fact that there are many undefined types in a trait.

Lori-Shu avatar Jun 02 '25 07:06 Lori-Shu

@linyihai@seanmonstar do you guys consider this is done by other contributors?If so,please close my pull request.Still,I will keep trying contribute to reqwest.I am curious about this networking thing.

Lori-Shu avatar Jun 19 '25 19:06 Lori-Shu

Thanks for you keep doing this thing that I hadn't finished.

do you guys consider this is done by other contributors?

Do you mean https://github.com/seanmonstar/reqwest/pull/2727 ? IMO, I lean to its solution which add the tower-layer to avoid the abstraction of the Response.

linyihai avatar Jun 20 '25 01:06 linyihai

Do you mean https://github.com/seanmonstar/reqwest/pull/2727 ? IMO, I lean to its solution which add the tower-layer to avoid the abstraction of the Response.

yeah,I think so,we are probably doing the same issue of #2693 @seanmonstar

Lori-Shu avatar Jun 20 '25 03:06 Lori-Shu