Use Url (instead of String) in internal history var

This commit is contained in:
Martin Algesten
2021-12-11 08:47:41 +01:00
parent 2a5ab43c8c
commit c59632bd97
2 changed files with 15 additions and 15 deletions

View File

@@ -53,7 +53,7 @@ const MAX_HEADER_COUNT: usize = 100;
/// # } /// # }
/// ``` /// ```
pub struct Response { pub struct Response {
url: Option<Url>, pub(crate) url: Option<Url>,
status_line: String, status_line: String,
index: ResponseStatusIndex, index: ResponseStatusIndex,
status: u16, status: u16,
@@ -67,7 +67,7 @@ pub struct Response {
/// previous to this one. /// previous to this one.
/// ///
/// If this response was not redirected, the history is empty. /// If this response was not redirected, the history is empty.
pub(crate) history: Vec<String>, pub(crate) history: Vec<Url>,
} }
/// index into status_line where we split: HTTP/1.1 200 OK /// index into status_line where we split: HTTP/1.1 200 OK
@@ -502,7 +502,7 @@ impl Response {
#[cfg(test)] #[cfg(test)]
pub fn history_from_previous(&mut self, previous: Response) { pub fn history_from_previous(&mut self, previous: Response) {
let previous_url = previous.get_url().to_string(); let previous_url = previous.url.expect("previous url");
self.history = previous.history; self.history = previous.history;
self.history.push(previous_url); self.history.push(previous_url);
} }
@@ -941,7 +941,7 @@ mod tests {
response2.set_url("http://2.example.com/".parse().unwrap()); response2.set_url("http://2.example.com/".parse().unwrap());
response2.history_from_previous(response1); response2.history_from_previous(response1);
let hist: Vec<&str> = response2.history.iter().map(|r| &**r).collect(); let hist: Vec<String> = response2.history.iter().map(|r| r.to_string()).collect();
assert_eq!(hist, ["http://1.example.com/", "http://2.example.com/"]) assert_eq!(hist, ["http://1.example.com/", "http://2.example.com/"])
} }

View File

@@ -206,7 +206,7 @@ pub(crate) fn connect(
_ => break resp, _ => break resp,
}; };
debug!("redirect {} {} -> {}", resp.status(), url, new_url); debug!("redirect {} {} -> {}", resp.status(), url, new_url);
history.push(unit.url.to_string()); history.push(unit.url);
body = Payload::Empty.into_read(); body = Payload::Empty.into_read();
unit.headers.retain(|h| h.name() != "Content-Length"); unit.headers.retain(|h| h.name() != "Content-Length");
@@ -229,7 +229,7 @@ fn connect_inner(
unit: &Unit, unit: &Unit,
use_pooled: bool, use_pooled: bool,
body: SizedReader, body: SizedReader,
previous: &[String], history: &[Url],
) -> Result<Response, Error> { ) -> Result<Response, Error> {
let host = unit let host = unit
.url .url
@@ -247,7 +247,7 @@ fn connect_inner(
debug!("sending request {} {}", method, url); debug!("sending request {} {}", method, url);
} }
let send_result = send_prelude(unit, &mut stream, previous); let send_result = send_prelude(unit, &mut stream, history);
if let Err(err) = send_result { if let Err(err) = send_result {
if is_recycled { if is_recycled {
@@ -255,7 +255,7 @@ fn connect_inner(
// we try open a new connection, this time there will be // we try open a new connection, this time there will be
// no connection in the pool. don't use it. // no connection in the pool. don't use it.
// NOTE: this recurses at most once because `use_pooled` is `false`. // NOTE: this recurses at most once because `use_pooled` is `false`.
return connect_inner(unit, false, body, previous); return connect_inner(unit, false, body, history);
} else { } else {
// not a pooled connection, propagate the error. // not a pooled connection, propagate the error.
return Err(err.into()); return Err(err.into());
@@ -284,7 +284,7 @@ fn connect_inner(
debug!("retrying request {} {}: {}", method, url, err); debug!("retrying request {} {}: {}", method, url, err);
let empty = Payload::Empty.into_read(); let empty = Payload::Empty.into_read();
// NOTE: this recurses at most once because `use_pooled` is `false`. // NOTE: this recurses at most once because `use_pooled` is `false`.
return connect_inner(unit, false, empty, previous); return connect_inner(unit, false, empty, history);
} }
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(resp) => resp, Ok(resp) => resp,
@@ -354,11 +354,11 @@ fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stre
Ok((stream?, false)) Ok((stream?, false))
} }
fn can_propagate_authorization_on_redirect(unit: &Unit, previous: &[String]) -> bool { fn can_propagate_authorization_on_redirect(unit: &Unit, history: &[Url]) -> bool {
if let RedirectAuthHeaders::SameHost = unit.agent.config.redirect_auth_headers { if let RedirectAuthHeaders::SameHost = unit.agent.config.redirect_auth_headers {
let host_s = unit.url.host_str().unwrap(); let host_s = unit.url.host_str().unwrap();
let prev_url = Url::parse(&previous[0]).unwrap(); let prev_url = &history[0];
let prev_host = prev_url.host_str().unwrap(); let prev_host = prev_url.host_str().expect("Host in previous Url");
host_s == prev_host && prev_url.scheme() == "https" && unit.url.scheme() == "https" host_s == prev_host && prev_url.scheme() == "https" && unit.url.scheme() == "https"
} else { } else {
false false
@@ -367,7 +367,7 @@ fn can_propagate_authorization_on_redirect(unit: &Unit, previous: &[String]) ->
/// Send request line + headers (all up until the body). /// Send request line + headers (all up until the body).
#[allow(clippy::write_with_newline)] #[allow(clippy::write_with_newline)]
fn send_prelude(unit: &Unit, stream: &mut Stream, previous: &[String]) -> io::Result<()> { fn send_prelude(unit: &Unit, stream: &mut Stream, history: &[Url]) -> io::Result<()> {
// build into a buffer and send in one go. // build into a buffer and send in one go.
let mut prelude = PreludeBuilder::new(); let mut prelude = PreludeBuilder::new();
@@ -406,8 +406,8 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, previous: &[String]) -> io::Re
prelude.write_header("Accept", "*/*")?; prelude.write_header("Accept", "*/*")?;
} }
let preserve_auth = if !previous.is_empty() { let preserve_auth = if !history.is_empty() {
can_propagate_authorization_on_redirect(unit, previous) can_propagate_authorization_on_redirect(unit, history)
} else { } else {
true //Not in redirection true //Not in redirection
}; };