-
Notifications
You must be signed in to change notification settings - Fork 168
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
Add FollowRedirect
middleware
#79
Conversation
This allows following of a redirection in some cases even if `ReqBody::default().size_hint()` would not return zero.
Commit 04cc691 added a new type parameter to `Policy` trait to represent the error type and, as a side effect, complicated type inference when calling the trait methods. To mitigate the issue, this commit moves the `Policy` combinators from trait methods to freestanding functions. This also adds `select` combinator function, which is useful in combination with `Action::error`.
05fffc4
to
0e6a949
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really great work on this!
I left some comments but overall I think this setup is nice.
I would also like to hear what @hawkw thinks. Adding her as reviewer.
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
This is to catch bugs in handling of `poll_ready`, as suggested in a review comment: tower-rs#79 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks good! I'm gonna try and integrate it with our code at embark as the final test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its working quite well! I ended up with something like this:
// automatically follow redirects
.option_layer(self.max_redirects.map(|limit| {
FollowRedirectLayer::with_policy(
Limited::new(limit).and::<_, _, HttpError>(clone_body_fn(Body::try_clone)),
)
}))
Hey really cool! |
Think I'm just going to merge this. We can always improve things later. |
This is a middleware that retries requests to follow HTTP redirections.
This includes
Policy
trait to customize behavior of the middleware, which is heavily inspired byreqwest::redirect::Policy
.Policy
also has an optional methodclone_body
that tries to clone a request body, liketower::retry::Policy::clone_request
. But even if the body cannot be cloned,FollowRedirect
uses<ReqBody as Default>::default
when<ReqBody as http_body::Body>::size_hint
returns zero, so that it can handle commonGET
requests withoutclone_body
.The API proposed here is a proof of concept to some extent, and it is fine if this is not fully merged at once (
2fe3fea...22212ba3f8deda...22212ba3f8deda...0e6a949 is optional in particular).Fixes #78.