Skip to content

feature: expose redirect policy through extension #583

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

unusual-thoughts
Copy link

This adds a FollowedPolicy extension for the FollowRedirect middleware that exposes the policy struct to the response.

Motivation

Currently there is no way to inspect the URLs (and status codes) that a redirected response took, as there is no way to "get back" the redirect policy struct from follow_redirect::ResponseFuture.

Custom redirect policies might have useful state that the user wants to access once the request completes. A typical use case would be to log the urls and status codes that have been traversed, which is already necessary for example to avoid loops.

This implements part of the modifications suggested in #404 as I understand it. It is also required to implement seanmonstar/reqwest#2606

Solution

Implement a http::Extensions::Extension that stores a copy of the follow_redirect::policy::Policy struct that was run on the last request of the redirect chain.

Downsides

Currently this does another clone() of the policy struct for each new request

@unusual-thoughts unusual-thoughts force-pushed the redirect-policy-extension branch 3 times, most recently from c29a069 to 44b3401 Compare June 25, 2025 20:28
@unusual-thoughts
Copy link
Author

Rewrote to avoid breaking changes. Did my best to avoid code duplication, but some is unavoidable.

@unusual-thoughts unusual-thoughts force-pushed the redirect-policy-extension branch 2 times, most recently from 536bde7 to f348c2b Compare June 25, 2025 20:39
@@ -3,7 +3,7 @@ use super::{Action, Attempt, Policy};
/// A redirection [`Policy`] that limits the number of successive redirections.
#[derive(Clone, Copy, Debug)]
pub struct Limited {
remaining: usize,
pub(crate) remaining: usize,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be useful to make this fully public

@unusual-thoughts unusual-thoughts force-pushed the redirect-policy-extension branch 5 times, most recently from ac346c1 to 4a5cec9 Compare June 27, 2025 18:15
@seanmonstar
Copy link
Collaborator

Here's another possible option, which I welcome feedback on from others: what if all that was added was a new method Policy::on_response(&mut Response)?

reqwest could then implement that method to insert an extension.

@unusual-thoughts
Copy link
Author

Here's another possible option, which I welcome feedback on from others: what if all that was added was a new method Policy::on_response(&mut Response)?

reqwest could then implement that method to insert an extension.

I suppose that could work too.

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

Successfully merging this pull request may close these issues.

3 participants