refactor redirect facade to use pub(crate)
This commit is contained in:
@@ -16,7 +16,7 @@ use super::request::{self, Request, RequestBuilder};
|
|||||||
use super::response::{self, Response};
|
use super::response::{self, Response};
|
||||||
use connect::Connector;
|
use connect::Connector;
|
||||||
use into_url::to_uri;
|
use into_url::to_uri;
|
||||||
use redirect::{self, RedirectPolicy, check_redirect, remove_sensitive_headers};
|
use redirect::{self, RedirectPolicy, remove_sensitive_headers};
|
||||||
use {Certificate, Identity, IntoUrl, Method, Proxy, StatusCode, Url};
|
use {Certificate, Identity, IntoUrl, Method, Proxy, StatusCode, Url};
|
||||||
|
|
||||||
static DEFAULT_USER_AGENT: &'static str =
|
static DEFAULT_USER_AGENT: &'static str =
|
||||||
@@ -475,8 +475,7 @@ impl Future for PendingRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.urls.push(self.url.clone());
|
self.urls.push(self.url.clone());
|
||||||
let action = check_redirect(
|
let action = self.client.redirect_policy.check(
|
||||||
&self.client.redirect_policy,
|
|
||||||
res.status(),
|
res.status(),
|
||||||
&loc,
|
&loc,
|
||||||
&self.urls,
|
&self.urls,
|
||||||
|
|||||||
@@ -128,6 +128,21 @@ impl RedirectPolicy {
|
|||||||
Policy::None => attempt.stop(),
|
Policy::None => attempt.stop(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn check(
|
||||||
|
&self,
|
||||||
|
status: StatusCode,
|
||||||
|
next: &Url,
|
||||||
|
previous: &[Url],
|
||||||
|
) -> Action {
|
||||||
|
self
|
||||||
|
.redirect(RedirectAttempt {
|
||||||
|
status: status,
|
||||||
|
next: next,
|
||||||
|
previous: previous,
|
||||||
|
})
|
||||||
|
.inner
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RedirectPolicy {
|
impl Default for RedirectPolicy {
|
||||||
@@ -205,30 +220,15 @@ impl fmt::Debug for Policy {
|
|||||||
// pub(crate)
|
// pub(crate)
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Action {
|
pub(crate) enum Action {
|
||||||
Follow,
|
Follow,
|
||||||
Stop,
|
Stop,
|
||||||
LoopDetected,
|
LoopDetected,
|
||||||
TooManyRedirects,
|
TooManyRedirects,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn check_redirect(
|
|
||||||
policy: &RedirectPolicy,
|
|
||||||
status: StatusCode,
|
|
||||||
next: &Url,
|
|
||||||
previous: &[Url],
|
|
||||||
) -> Action {
|
|
||||||
policy
|
|
||||||
.redirect(RedirectAttempt {
|
|
||||||
status: status,
|
|
||||||
next: next,
|
|
||||||
previous: previous,
|
|
||||||
})
|
|
||||||
.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_sensitive_headers(headers: &mut HeaderMap, next: &Url, previous: &[Url]) {
|
pub(crate) fn remove_sensitive_headers(headers: &mut HeaderMap, next: &Url, previous: &[Url]) {
|
||||||
if let Some(previous) = previous.last() {
|
if let Some(previous) = previous.last() {
|
||||||
let cross_host = next.host_str() != previous.host_str() ||
|
let cross_host = next.host_str() != previous.host_str() ||
|
||||||
next.port_or_known_default() != previous.port_or_known_default();
|
next.port_or_known_default() != previous.port_or_known_default();
|
||||||
@@ -268,14 +268,14 @@ fn test_redirect_policy_limit() {
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_redirect(&policy, StatusCode::FOUND, &next, &previous),
|
policy.check(StatusCode::FOUND, &next, &previous),
|
||||||
Action::Follow
|
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!(
|
assert_eq!(
|
||||||
check_redirect(&policy, StatusCode::FOUND, &next, &previous),
|
policy.check(StatusCode::FOUND, &next, &previous),
|
||||||
Action::TooManyRedirects
|
Action::TooManyRedirects
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -292,13 +292,13 @@ fn test_redirect_policy_custom() {
|
|||||||
|
|
||||||
let next = Url::parse("http://bar/baz").unwrap();
|
let next = Url::parse("http://bar/baz").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_redirect(&policy, StatusCode::FOUND, &next, &[]),
|
policy.check(StatusCode::FOUND, &next, &[]),
|
||||||
Action::Follow
|
Action::Follow
|
||||||
);
|
);
|
||||||
|
|
||||||
let next = Url::parse("http://foo/baz").unwrap();
|
let next = Url::parse("http://foo/baz").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
check_redirect(&policy, StatusCode::FOUND, &next, &[]),
|
policy.check(StatusCode::FOUND, &next, &[]),
|
||||||
Action::Stop
|
Action::Stop
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user