Skip to content

follow_redirect: expose previous and next request methods #559

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tower-http/src/follow_redirect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ where
let mut res = ready!(this.future.as_mut().poll(cx)?);
res.extensions_mut().insert(RequestUri(this.uri.clone()));

let previous_method = &this.method.clone();
match res.status() {
StatusCode::MOVED_PERMANENTLY | StatusCode::FOUND => {
// User agents MAY change the request method from POST to GET
Expand Down Expand Up @@ -295,7 +296,9 @@ where

let attempt = Attempt {
status: res.status(),
next_method: &this.method,
location: &location,
previous_method,
previous: this.uri,
};
match this.policy.redirect(&attempt)? {
Expand Down
4 changes: 3 additions & 1 deletion tower-http/src/follow_redirect/policy/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use http::Uri;
use http::{Method, Uri};

struct Taint<P> {
policy: P,
Expand Down Expand Up @@ -75,7 +75,9 @@ mod tests {
fn redirect() {
let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &Uri::from_static("*"),
previous_method: &Method::GET,
previous: &Uri::from_static("*"),
};

Expand Down
6 changes: 5 additions & 1 deletion tower-http/src/follow_redirect/policy/filter_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<B, E> Policy<B, E> for FilterCredentials {
#[cfg(test)]
mod tests {
use super::*;
use http::Uri;
use http::{Method, Uri};

#[test]
fn works() {
Expand All @@ -126,7 +126,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &same_origin,
previous_method: &Method::GET,
previous: request.uri(),
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand All @@ -143,7 +145,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &cross_origin,
previous_method: &Method::GET,
previous: request.uri(),
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand Down
6 changes: 5 additions & 1 deletion tower-http/src/follow_redirect/policy/limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<B, E> Policy<B, E> for Limited {

#[cfg(test)]
mod tests {
use http::{Request, Uri};
use http::{Method, Request, Uri};

use super::*;

Expand All @@ -51,7 +51,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &uri,
previous_method: &Method::GET,
previous: &uri,
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand All @@ -64,7 +66,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &uri,
previous_method: &Method::GET,
previous: &uri,
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand Down
14 changes: 13 additions & 1 deletion tower-http/src/follow_redirect/policy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::{
same_origin::SameOrigin,
};

use http::{uri::Scheme, Request, StatusCode, Uri};
use http::{uri::Scheme, Method, Request, StatusCode, Uri};

/// Trait for the policy on handling redirection responses.
///
Expand Down Expand Up @@ -198,7 +198,9 @@ pub type Standard = And<Limited, FilterCredentials>;
/// A type that holds information on a redirection attempt.
pub struct Attempt<'a> {
pub(crate) status: StatusCode,
pub(crate) next_method: &'a Method,
pub(crate) location: &'a Uri,
pub(crate) previous_method: &'a Method,
pub(crate) previous: &'a Uri,
}

Expand All @@ -208,11 +210,21 @@ impl<'a> Attempt<'a> {
self.status
}

/// Returns the method for the next request, after applying redirection logic.
pub fn next_method(&self) -> &Method {
self.next_method
}

/// Returns the destination URI of the redirection.
pub fn location(&self) -> &'a Uri {
self.location
}

/// Returns the method for the previous request, before redirection.
pub fn previous_method(&self) -> &Method {
self.previous_method
}

/// Returns the URI of the original request.
pub fn previous(&self) -> &'a Uri {
self.previous
Expand Down
4 changes: 3 additions & 1 deletion tower-http/src/follow_redirect/policy/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use http::Uri;
use http::{Method, Uri};

struct Taint<P> {
policy: P,
Expand Down Expand Up @@ -75,7 +75,9 @@ mod tests {
fn redirect() {
let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &Uri::from_static("*"),
previous_method: &Method::GET,
previous: &Uri::from_static("*"),
};

Expand Down
6 changes: 5 additions & 1 deletion tower-http/src/follow_redirect/policy/same_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<B, E> Policy<B, E> for SameOrigin {
#[cfg(test)]
mod tests {
use super::*;
use http::{Request, Uri};
use http::{Method, Request, Uri};

#[test]
fn works() {
Expand All @@ -48,7 +48,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &same_origin,
previous_method: &Method::GET,
previous: request.uri(),
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand All @@ -60,7 +62,9 @@ mod tests {

let attempt = Attempt {
status: Default::default(),
next_method: &Method::GET,
location: &cross_origin,
previous_method: &Method::GET,
previous: request.uri(),
};
assert!(Policy::<(), ()>::redirect(&mut policy, &attempt)
Expand Down
Loading