From 37e1e91e22d1bec33173b1bb8bf7bf463a63aec2 Mon Sep 17 00:00:00 2001 From: Gus Power Date: Sat, 19 Jun 2021 00:12:31 +0100 Subject: [PATCH] Clear Content-Length header from redirected requests (#394) This PR removes the Content-Length header from subsequent redirect requests if set. A test verifies the new behaviour. --- src/test/redirect.rs | 17 +++++++++++++++++ src/unit.rs | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/test/redirect.rs b/src/test/redirect.rs index b6f1fa0..067e736 100644 --- a/src/test/redirect.rs +++ b/src/test/redirect.rs @@ -146,6 +146,23 @@ fn redirect_post() { assert_eq!(resp.header("x-foo").unwrap(), "bar"); } +#[test] +fn redirect_post_with_data() { + test::set_handler("/redirect_post1", |unit| { + assert_eq!(unit.header("Content-Length").unwrap(), "4"); + test::make_response(302, "Go here", vec!["Location: /redirect_post2"], vec![]) + }); + test::set_handler("/redirect_post2", |unit| { + assert_eq!(unit.header("Content-Length"), None); + assert_eq!(unit.method, "GET"); + test::make_response(200, "OK", vec![], vec![]) + }); + let resp = post("test://host/redirect_post1") + .send_string("data") + .unwrap(); + assert_eq!(resp.status(), 200); +} + #[test] fn redirect_308() { test::set_handler("/redirect_get3", |_| { diff --git a/src/unit.rs b/src/unit.rs index a50d346..063cca4 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -205,6 +205,8 @@ pub(crate) fn connect( debug!("redirect {} {} -> {}", resp.status(), url, new_url); history.push(unit.url.to_string()); body = Payload::Empty.into_read(); + unit.headers.retain(|h| h.name() != "Content-Length"); + // recreate the unit to get a new hostname and cookies for the new host. unit = Unit::new( &unit.agent,