Skip to content

Commit

Permalink
feat(redirect): Expose method for accessing the previous and next req…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 authored Dec 11, 2024
1 parent cfedb58 commit bdbc7f1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,9 @@ impl Future for PendingRequest {
}
}
}

let previous_method = self.method.clone();

let should_redirect = match res.status() {
StatusCode::MOVED_PERMANENTLY | StatusCode::FOUND | StatusCode::SEE_OTHER => {
self.body = None;
Expand Down Expand Up @@ -2037,6 +2040,7 @@ impl Future for PendingRequest {
}
_ => false,
};

if should_redirect {
let loc = res.headers().get(LOCATION).and_then(|val| {
let loc = (|| -> Option<Url> {
Expand Down Expand Up @@ -2074,7 +2078,13 @@ impl Future for PendingRequest {
.redirect
.as_ref()
.unwrap_or(&self.client.redirect)
.check(res.status(), &loc, &self.urls);
.check(
res.status(),
&self.method,
&loc,
&previous_method,
&self.urls,
);

match action {
redirect::ActionKind::Follow => {
Expand Down
71 changes: 65 additions & 6 deletions src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::error::Error as StdError;
use std::fmt;

use crate::header::{HeaderMap, AUTHORIZATION, COOKIE, PROXY_AUTHORIZATION, WWW_AUTHENTICATE};
use http::Method;
use hyper::StatusCode;

use crate::Url;
Expand All @@ -30,7 +31,9 @@ pub struct Policy {
#[derive(Debug)]
pub struct Attempt<'a> {
status: StatusCode,
next_method: &'a Method,
next: &'a Url,
previous_method: &'a Method,
previous: &'a [Url],
}

Expand Down Expand Up @@ -138,10 +141,19 @@ impl Policy {
}
}

pub(crate) fn check(&self, status: StatusCode, next: &Url, previous: &[Url]) -> ActionKind {
pub(crate) fn check(
&self,
status: StatusCode,
next_method: &Method,
next: &Url,
previous_method: &Method,
previous: &[Url],
) -> ActionKind {
self.redirect(Attempt {
status,
next_method,
next,
previous_method,
previous,
})
.inner
Expand All @@ -165,11 +177,21 @@ impl<'a> Attempt<'a> {
self.status
}

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

/// Get the next URL to redirect to.
pub fn url(&self) -> &Url {
self.next
}

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

/// Get the list of previous URLs that have already been requested in this chain.
pub fn previous(&self) -> &[Url] {
self.previous
Expand Down Expand Up @@ -264,14 +286,26 @@ fn test_redirect_policy_limit() {
.map(|i| Url::parse(&format!("http://a.b/c/{}", i)).unwrap())
.collect::<Vec<_>>();

match policy.check(StatusCode::FOUND, &next, &previous) {
match policy.check(
StatusCode::FOUND,
&Method::GET,
&next,
&Method::GET,
&previous,
) {
ActionKind::Follow => (),
other => panic!("unexpected {:?}", other),
}

previous.push(Url::parse("http://a.b.d/e/33").unwrap());

match policy.check(StatusCode::FOUND, &next, &previous) {
match policy.check(
StatusCode::FOUND,
&Method::GET,
&next,
&Method::GET,
&previous,
) {
ActionKind::Error(err) if err.is::<TooManyRedirects>() => (),
other => panic!("unexpected {:?}", other),
}
Expand All @@ -283,7 +317,13 @@ fn test_redirect_policy_limit_to_0() {
let next = Url::parse("http://x.y/z").unwrap();
let previous = vec![Url::parse("http://a.b/c").unwrap()];

match policy.check(StatusCode::FOUND, &next, &previous) {
match policy.check(
StatusCode::FOUND,
&Method::GET,
&next,
&Method::GET,
&previous,
) {
ActionKind::Error(err) if err.is::<TooManyRedirects>() => (),
other => panic!("unexpected {:?}", other),
}
Expand All @@ -300,18 +340,37 @@ fn test_redirect_policy_custom() {
});

let next = Url::parse("http://bar/baz").unwrap();
match policy.check(StatusCode::FOUND, &next, &[]) {
match policy.check(StatusCode::FOUND, &Method::GET, &next, &Method::GET, &[]) {
ActionKind::Follow => (),
other => panic!("unexpected {:?}", other),
}

let next = Url::parse("http://foo/baz").unwrap();
match policy.check(StatusCode::FOUND, &next, &[]) {
match policy.check(StatusCode::FOUND, &Method::GET, &next, &Method::GET, &[]) {
ActionKind::Stop => (),
other => panic!("unexpected {:?}", other),
}
}

#[test]
fn test_redirect_custom_policy_methods() {
let policy = Policy::custom(|attempt| {
let next = attempt.next_method();
if next != Method::HEAD {
panic!("unexpected next method {:?}", next);
}
let prev = attempt.previous_method();
if prev != Method::PUT {
panic!("unexpected previous method {:?}", prev);
}
attempt.stop()
});

let next = Url::parse("http://bar/baz").unwrap();
let res = policy.check(StatusCode::FOUND, &Method::HEAD, &next, &Method::PUT, &[]);
assert!(matches!(res, ActionKind::Stop));
}

#[test]
fn test_remove_sensitive_headers() {
use hyper::header::{HeaderValue, ACCEPT, AUTHORIZATION, COOKIE};
Expand Down

0 comments on commit bdbc7f1

Please sign in to comment.