From 612e983576bafb44f616d866ed8430bc3d8ecb2b Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Wed, 30 Jul 2025 20:53:18 -0700 Subject: [PATCH 1/3] fix(awc): some methods incorrectly send body & body-headers --- awc/CHANGES.md | 2 ++ awc/src/any_body.rs | 14 +++++++++++--- awc/src/sender.rs | 18 ++++++++---------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 092239719..9cf01ccd4 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- `GET/HEAD/OPTIONS/TRACE` methods no longer send a request body on request. + ## 3.7.0 - Update `brotli` dependency to `8`. diff --git a/awc/src/any_body.rs b/awc/src/any_body.rs index ef0edfb9e..d38533ea0 100644 --- a/awc/src/any_body.rs +++ b/awc/src/any_body.rs @@ -4,8 +4,12 @@ use std::{ task::{Context, Poll}, }; -use actix_http::body::{BodySize, BoxBody, MessageBody}; +use actix_http::{ + body::{BodySize, BoxBody, MessageBody}, + RequestHead, +}; use bytes::Bytes; +use http::Method; use pin_project_lite::pin_project; pin_project! { @@ -75,11 +79,15 @@ where /// Converts a [`MessageBody`] type into the best possible representation. /// /// Checks size for `None` and tries to convert to `Bytes`. Otherwise, uses the `Body` variant. - pub fn from_message_body(body: B) -> Self + pub fn from_message_body(head: &RequestHead, body: B) -> Self where B: MessageBody, { - if matches!(body.size(), BodySize::None) { + if matches!( + head.method, + Method::GET | Method::HEAD | Method::OPTIONS | Method::TRACE + ) || matches!(body.size(), BodySize::None) + { return Self::None; } diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 0015743bd..de563a798 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -189,16 +189,14 @@ impl RequestSender { body: impl MessageBody + 'static, ) -> SendClientRequest { let req = match self { - RequestSender::Owned(head) => ConnectRequest::Client( - RequestHeadType::Owned(head), - AnyBody::from_message_body(body).into_boxed(), - addr, - ), - RequestSender::Rc(head, extra_headers) => ConnectRequest::Client( - RequestHeadType::Rc(head, extra_headers), - AnyBody::from_message_body(body).into_boxed(), - addr, - ), + RequestSender::Owned(head) => { + let body = AnyBody::from_message_body(&head, body).into_boxed(); + ConnectRequest::Client(RequestHeadType::Owned(head), body, addr) + } + RequestSender::Rc(head, extra_headers) => { + let body = AnyBody::from_message_body(&head, body).into_boxed(); + ConnectRequest::Client(RequestHeadType::Rc(head, extra_headers), body, addr) + } }; let fut = config.connector.call(req); From 605bcd18c6ea36fe43d70cc401721c983b3ca1f0 Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Wed, 30 Jul 2025 21:08:46 -0700 Subject: [PATCH 2/3] chore(awc): json test should use proper method --- awc/tests/test_client.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 43c83891a..e3c477015 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -62,10 +62,11 @@ async fn json() { }); let request = srv - .get("/") + .post("/") .insert_header(("x-test", "111")) .send_json(&"TEST".to_string()); let response = request.await.unwrap(); + println!("{response:?}"); assert!(response.status().is_success()); } From 845534fc81d78d5f8af6269ef7e91a5c04001fd6 Mon Sep 17 00:00:00 2001 From: imgurbot12 Date: Wed, 30 Jul 2025 21:20:07 -0700 Subject: [PATCH 3/3] chore(actix-http): more tests should use proper method --- actix-http/tests/test_client.rs | 4 ++-- actix-http/tests/test_openssl.rs | 2 +- actix-http/tests/test_rustls.rs | 2 +- awc/tests/test_client.rs | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/actix-http/tests/test_client.rs b/actix-http/tests/test_client.rs index 2d940984d..3e0f2a8e4 100644 --- a/actix-http/tests/test_client.rs +++ b/actix-http/tests/test_client.rs @@ -139,7 +139,7 @@ async fn h1_expect() { // test expect would fail to continue let request = srv - .request(http::Method::GET, srv.url("/")) + .request(http::Method::POST, srv.url("/")) .insert_header(("Expect", "100-continue")); let response = request.send_body("expect body").await.unwrap(); @@ -147,7 +147,7 @@ async fn h1_expect() { // test expect would continue let request = srv - .request(http::Method::GET, srv.url("/")) + .request(http::Method::POST, srv.url("/")) .insert_header(("Expect", "100-continue")) .insert_header(("AUTH", "996")); diff --git a/actix-http/tests/test_openssl.rs b/actix-http/tests/test_openssl.rs index 83456b0cb..4a520a366 100644 --- a/actix-http/tests/test_openssl.rs +++ b/actix-http/tests/test_openssl.rs @@ -118,7 +118,7 @@ async fn h2_body() -> io::Result<()> { }) .await; - let response = srv.sget("/").send_body(data.clone()).await.unwrap(); + let response = srv.spost("/").send_body(data.clone()).await.unwrap(); assert!(response.status().is_success()); let body = srv.load_body(response).await.unwrap(); diff --git a/actix-http/tests/test_rustls.rs b/actix-http/tests/test_rustls.rs index 43e47c0a4..3060cec22 100644 --- a/actix-http/tests/test_rustls.rs +++ b/actix-http/tests/test_rustls.rs @@ -184,7 +184,7 @@ async fn h2_body1() -> io::Result<()> { }) .await; - let response = srv.sget("/").send_body(data.clone()).await.unwrap(); + let response = srv.spost("/").send_body(data.clone()).await.unwrap(); assert!(response.status().is_success()); let body = srv.load_body(response).await.unwrap(); diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index e3c477015..7cd1ea8ee 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -66,7 +66,6 @@ async fn json() { .insert_header(("x-test", "111")) .send_json(&"TEST".to_string()); let response = request.await.unwrap(); - println!("{response:?}"); assert!(response.status().is_success()); }