From c13562a20047225b556bb5146ec458f24da056ec Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Mon, 18 Aug 2025 15:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=AE=9A=E5=90=91ht?= =?UTF-8?q?tp2=20BodyDecode=20Error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiga Ultraman --- .../src/async_impl/conn/http2.rs | 5 +- ylong_http_client/src/async_impl/request.rs | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/ylong_http_client/src/async_impl/conn/http2.rs b/ylong_http_client/src/async_impl/conn/http2.rs index e9ecb28..6d614e9 100644 --- a/ylong_http_client/src/async_impl/conn/http2.rs +++ b/ylong_http_client/src/async_impl/conn/http2.rs @@ -55,8 +55,9 @@ where let part = message.request.ref_mut().part().clone(); // TODO Implement trailer. - let is_end_stream = message.request.ref_mut().body().is_empty(); - let (flag, payload) = build_headers_payload(part, is_end_stream) + let is_body_empty = message.request.ref_mut().body().is_empty(); + let no_length = message.request.ref_mut().headers().get("content-length").is_none(); + let (flag, payload) = build_headers_payload(part, is_body_empty && no_length) .map_err(|e| HttpClientError::from_error(ErrorKind::Request, e))?; let data = BodyDataRef::new(message.request.clone(), conn.speed_controller.clone()); let stream = RequestWrapper { diff --git a/ylong_http_client/src/async_impl/request.rs b/ylong_http_client/src/async_impl/request.rs index 79eeb59..5d99dca 100644 --- a/ylong_http_client/src/async_impl/request.rs +++ b/ylong_http_client/src/async_impl/request.rs @@ -352,7 +352,11 @@ impl Body { #[cfg(feature = "http2")] pub(crate) fn is_empty(&self) -> bool { - matches!(self.inner, BodyKind::Empty) + match self.inner { + BodyKind::Empty => true, + BodyKind::Slice(ref text) => text.get_ref().len() as u64 == text.position(), + _ => false, + } } pub(crate) async fn reuse(&mut self) -> std::io::Result<()> { @@ -524,4 +528,46 @@ mod ut_client_request { "https://www.example.com/data/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.txt" ); } + + /// UT test cases for `Body::is_empty`. + /// + /// # Brief + /// 1. Creates a `Body`. + /// 2. Checks if result is correct. + #[test] + #[cfg(feature = "http2")] + fn ut_body_empty() { + use std::pin::Pin; + use std::task::Poll; + use ylong_runtime::io::AsyncRead; + + let empty = Body::empty(); + assert!(empty.is_empty()); + let empty_slice = Body::slice(""); + assert!(empty_slice.is_empty()); + let mut data_slice = Body::slice("hello"); + assert!(!data_slice.is_empty()); + ylong_runtime::block_on(async move { + let mut container = [0u8; 5]; + let mut curr = 0; + loop { + let mut buf = ylong_runtime::io::ReadBuf::new(&mut container[curr..]); + let size = ylong_runtime::futures::poll_fn(|cx| { + match Pin::new(&mut data_slice).poll_read(cx, &mut buf) { + Poll::Ready(_) => { + let size = buf.filled().len(); + Poll::Ready(size) + } + Poll::Pending => Poll::Pending, + } + }) + .await; + curr += size; + if curr == 5 { + break; + } + } + assert!(data_slice.is_empty()); + }); + } } -- Gitee