refactor redirect facade to use pub(crate)

This commit is contained in:
Sean McArthur
2018-08-23 10:03:52 -07:00
parent ef529df3f2
commit 13cfc4df85
2 changed files with 23 additions and 24 deletions

View File

@@ -16,7 +16,7 @@ use super::request::{self, Request, RequestBuilder};
use super::response::{self, Response};
use connect::Connector;
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};
static DEFAULT_USER_AGENT: &'static str =
@@ -475,8 +475,7 @@ impl Future for PendingRequest {
}
}
self.urls.push(self.url.clone());
let action = check_redirect(
&self.client.redirect_policy,
let action = self.client.redirect_policy.check(
res.status(),
&loc,
&self.urls,

View File

@@ -128,6 +128,21 @@ impl RedirectPolicy {
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 {
@@ -205,30 +220,15 @@ impl fmt::Debug for Policy {
// pub(crate)
#[derive(Debug, PartialEq)]
pub enum Action {
pub(crate) enum Action {
Follow,
Stop,
LoopDetected,
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() {
let cross_host = next.host_str() != previous.host_str() ||
next.port_or_known_default() != previous.port_or_known_default();
@@ -268,14 +268,14 @@ fn test_redirect_policy_limit() {
.collect::<Vec<_>>();
assert_eq!(
check_redirect(&policy, StatusCode::FOUND, &next, &previous),
policy.check(StatusCode::FOUND, &next, &previous),
Action::Follow
);
previous.push(Url::parse("http://a.b.d/e/33").unwrap());
assert_eq!(
check_redirect(&policy, StatusCode::FOUND, &next, &previous),
policy.check(StatusCode::FOUND, &next, &previous),
Action::TooManyRedirects
);
}
@@ -292,13 +292,13 @@ fn test_redirect_policy_custom() {
let next = Url::parse("http://bar/baz").unwrap();
assert_eq!(
check_redirect(&policy, StatusCode::FOUND, &next, &[]),
policy.check(StatusCode::FOUND, &next, &[]),
Action::Follow
);
let next = Url::parse("http://foo/baz").unwrap();
assert_eq!(
check_redirect(&policy, StatusCode::FOUND, &next, &[]),
policy.check(StatusCode::FOUND, &next, &[]),
Action::Stop
);
}