Add status code accessor to RedirectAttempt (#285)

This commit is contained in:
Mattias Päivärinta
2018-04-16 00:11:50 +02:00
committed by Sean McArthur
parent c00950085c
commit 31504388d7
2 changed files with 44 additions and 14 deletions

View File

@@ -539,7 +539,12 @@ impl Future for PendingRequest {
} }
} }
self.urls.push(self.url.clone()); self.urls.push(self.url.clone());
let action = check_redirect(&self.client.redirect_policy, &loc, &self.urls); let action = check_redirect(
&self.client.redirect_policy,
res.status(),
&loc,
&self.urls,
);
match action { match action {
redirect::Action::Follow => { redirect::Action::Follow => {

View File

@@ -1,6 +1,7 @@
use std::fmt; use std::fmt;
use hyper::header::{Headers}; use hyper::header::Headers;
use hyper::StatusCode;
use Url; use Url;
@@ -17,6 +18,7 @@ pub struct RedirectPolicy {
/// in redirect chain. /// in redirect chain.
#[derive(Debug)] #[derive(Debug)]
pub struct RedirectAttempt<'a> { pub struct RedirectAttempt<'a> {
status: StatusCode,
next: &'a Url, next: &'a Url,
previous: &'a [Url], previous: &'a [Url],
} }
@@ -105,7 +107,7 @@ impl RedirectPolicy {
/// # /// #
/// # fn run() -> Result<(), Error> { /// # fn run() -> Result<(), Error> {
/// let custom = RedirectPolicy::custom(|attempt| { /// let custom = RedirectPolicy::custom(|attempt| {
/// eprintln!("Location: {:?}", attempt.url()); /// eprintln!("{}, Location: {:?}", attempt.status(), attempt.url());
/// RedirectPolicy::default().redirect(attempt) /// RedirectPolicy::default().redirect(attempt)
/// }); /// });
/// # Ok(()) /// # Ok(())
@@ -135,6 +137,11 @@ impl Default for RedirectPolicy {
} }
impl<'a> RedirectAttempt<'a> { impl<'a> RedirectAttempt<'a> {
/// Get the type of redirect.
pub fn status(&self) -> StatusCode {
self.status
}
/// Get the next URL to redirect to. /// Get the next URL to redirect to.
pub fn url(&self) -> &Url { pub fn url(&self) -> &Url {
self.next self.next
@@ -206,11 +213,19 @@ pub enum Action {
} }
#[inline] #[inline]
pub fn check_redirect(policy: &RedirectPolicy, next: &Url, previous: &[Url]) -> Action { pub fn check_redirect(
policy.redirect(RedirectAttempt { policy: &RedirectPolicy,
next: next, status: StatusCode,
previous: previous, next: &Url,
}).inner previous: &[Url],
) -> Action {
policy
.redirect(RedirectAttempt {
status: status,
next: next,
previous: previous,
})
.inner
} }
pub fn remove_sensitive_headers(headers: &mut Headers, next: &Url, previous: &[Url]) { pub fn remove_sensitive_headers(headers: &mut Headers, next: &Url, previous: &[Url]) {
@@ -252,13 +267,17 @@ fn test_redirect_policy_limit() {
.map(|i| Url::parse(&format!("http://a.b/c/{}", i)).unwrap()) .map(|i| Url::parse(&format!("http://a.b/c/{}", i)).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!(
assert_eq!(check_redirect(&policy, &next, &previous), Action::Follow); check_redirect(&policy, StatusCode::Found, &next, &previous),
Action::Follow
);
previous.push(Url::parse("http://a.b.d/e/33").unwrap()); previous.push(Url::parse("http://a.b.d/e/33").unwrap());
assert_eq!(check_redirect(&policy, &next, &previous), assert_eq!(
Action::TooManyRedirects); check_redirect(&policy, StatusCode::Found, &next, &previous),
Action::TooManyRedirects
);
} }
#[test] #[test]
@@ -272,10 +291,16 @@ fn test_redirect_policy_custom() {
}); });
let next = Url::parse("http://bar/baz").unwrap(); let next = Url::parse("http://bar/baz").unwrap();
assert_eq!(check_redirect(&policy, &next, &[]), Action::Follow); assert_eq!(
check_redirect(&policy, StatusCode::Found, &next, &[]),
Action::Follow
);
let next = Url::parse("http://foo/baz").unwrap(); let next = Url::parse("http://foo/baz").unwrap();
assert_eq!(check_redirect(&policy, &next, &[]), Action::Stop); assert_eq!(
check_redirect(&policy, StatusCode::Found, &next, &[]),
Action::Stop
);
} }
#[test] #[test]