Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ fn format_host_port(host: &str, port: u16) -> String {
}
}

/// A redirect may reuse the current connection only if both the scheme and the
/// authority are unchanged (e.g. `http://B` -> `https://B` needs a new connection).
fn can_reuse_connection(from: &Url, to: &Url) -> bool {
from.scheme() == to.scheme() && from.authority() == to.authority()
}

#[derive(Debug, Clone, Copy)]
pub struct ConnectionTime {
pub dns_lookup: std::time::Duration,
Expand Down Expand Up @@ -940,7 +946,7 @@ impl Client {
};

let (mut send_request, send_request_base) =
if base_url.authority() == url.authority() && !self.disable_keepalive {
if can_reuse_connection(&base_url, &url) && !self.disable_keepalive {
// reuse connection
(send_request, None)
} else {
Expand All @@ -953,8 +959,8 @@ impl Client {
send_request = stream;
}

let mut request = self.generate_request(&mut seed.clone())?.1;
if url.authority() != base_url.authority() {
let (request_url, mut request, _) = self.generate_request(&mut seed.clone())?;
if url.authority() != request_url.authority() {
request.headers_mut().insert(
http::header::HOST,
http::HeaderValue::from_str(url.authority())?,
Expand Down Expand Up @@ -994,9 +1000,15 @@ impl Client {
}

if let Some(location) = parts.headers.get("Location") {
let (send_request_redirect, new_status, len) =
Box::pin(self.redirect(base_url, seed, send_request, location, limit - 1, rng))
.await?;
let (send_request_redirect, new_status, len) = Box::pin(self.redirect(
Cow::Owned(url),
seed,
send_request,
location,
limit - 1,
rng,
))
.await?;
send_request = send_request_redirect;
status = new_status;
len_bytes = len;
Expand Down Expand Up @@ -2653,3 +2665,16 @@ pub mod fast {
});
}
}

#[cfg(test)]
mod redirect_reuse_tests {
use super::*;

#[test]
fn connection_is_reused_only_for_same_scheme_and_authority() {
let u = |s: &str| Url::parse(s).unwrap();
assert!(can_reuse_connection(&u("http://b/x"), &u("http://b/y")));
assert!(!can_reuse_connection(&u("http://b/x"), &u("https://b/y")));
assert!(!can_reuse_connection(&u("http://a/x"), &u("http://b/y")));
}
}
38 changes: 38 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,44 @@ async fn test_redirect() {
}
}

#[rstest]
#[case(false)]
#[case(true)]
#[tokio::test]
async fn test_chained_relative_redirects(#[case] cross_authority: bool) {
let (listener, port) = bind_port().await;
let (tx, rx) = kanal::unbounded();
let target = format!("http://127.0.0.1:{port}");
let app = Router::new()
.route("/start", get(|| async { Redirect::temporary("/dir/next") }))
.route("/dir/next", get(|| async { Redirect::temporary("leaf") }))
.route(
"/dir/leaf",
get(move |headers: HeaderMap| async move {
tx.send(headers["host"].to_str().unwrap().to_string())
.unwrap();
"done"
}),
);
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });

let start = if cross_authority {
let (listener, _) = bind_port().await;
let address = listener.local_addr().unwrap();
let location = format!("{target}/start");
let app = Router::new().route(
"/start",
get(move || async move { Redirect::temporary(&location) }),
);
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
format!("http://{address}/start")
} else {
format!("{target}/start")
};
run(["-n", "1", "-c", "1", "--redirect", "5", &start]).await;
assert_eq!(rx.try_recv().unwrap(), Some(format!("127.0.0.1:{port}")));
}

#[tokio::test]
async fn test_connect_to() {
assert_eq!(
Expand Down
Loading