Repository: algesten/ureq
Pull request #394 (2021-06-18 14:58:59)

Clear Content-Length header from redirected requests
This PR removes the `Content-Length` header from subsequent redirect requests if set. A test verifies the new behaviour.

This issue causes `hyper` to attempt to read a non-existent body from the redirect GET request, as seen when trying to use `wiremock-rs` to simulate redirect-on-post behaviour where the POST contains a payload. The issue has be replicated in [an example project](https://github.com/guspower/redirect-on-post).

jsha: Hi @guspower ! I thought I'd save you some time by forking your PR into #395, and then I realize I can't self-approve it. Want to merge in my c948d43, then I'll reapprove this and merge?
guspower: Yep for sure let me give it a go

On Fri, 18 Jun 2021 at 21:05, Jacob Hoffman-Andrews <
***@***.***> wrote:

> Hi @guspower <https://github.com/guspower> ! I thought I'd save you some
> time by forking your PR into #395
> <https://github.com/algesten/ureq/pull/395>, and then I realize I can't
> self-approve it. Want to merge in my c948d43
> <https://github.com/algesten/ureq/commit/c948d439829973b4ec1d8ef6ab9d70e64ebb3b89>,
> then I'll reapprove this and merge?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/algesten/ureq/pull/394#issuecomment-864249139>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AABSPBE2OZJBEUMXMIQR7CLTTORJ7ANCNFSM4652TWIQ>
> .
>

guspower: I pushed the changes to satisfy the linter.

On Fri, 18 Jun 2021, 21:12 Gus Power, ***@***.***> wrote:

> Yep for sure let me give it a go
>
> On Fri, 18 Jun 2021 at 21:05, Jacob Hoffman-Andrews <
> ***@***.***> wrote:
>
>> Hi @guspower <https://github.com/guspower> ! I thought I'd save you some
>> time by forking your PR into #395
>> <https://github.com/algesten/ureq/pull/395>, and then I realize I can't
>> self-approve it. Want to merge in my c948d43
>> <https://github.com/algesten/ureq/commit/c948d439829973b4ec1d8ef6ab9d70e64ebb3b89>,
>> then I'll reapprove this and merge?
>>
>> —
>> You are receiving this because you were mentioned.
>> Reply to this email directly, view it on GitHub
>> <https://github.com/algesten/ureq/pull/394#issuecomment-864249139>, or
>> unsubscribe
>> <https://github.com/notifications/unsubscribe-auth/AABSPBE2OZJBEUMXMIQR7CLTTORJ7ANCNFSM4652TWIQ>
>> .
>>
>


Patch:
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,


Test patch:
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", |_| {