From 88b47f98ceb197cc1f1884b8f5736b31b1cdf50c Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Mon, 18 Aug 2025 15:13:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91http2=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 From 205a994ebf954923144631c7d42ff232cb048611 Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Tue, 19 Aug 2025 16:40:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95invalid=20domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiga Ultraman --- ylong_http_client/src/async_impl/dns/default.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ylong_http_client/src/async_impl/dns/default.rs b/ylong_http_client/src/async_impl/dns/default.rs index ab4997c..32d41fe 100644 --- a/ylong_http_client/src/async_impl/dns/default.rs +++ b/ylong_http_client/src/async_impl/dns/default.rs @@ -140,6 +140,9 @@ impl Resolver for DefaultDnsResolver { } match connector.get_socket_addrs(&authority) { Ok(addrs) => { + if addrs.is_empty() { + return Err(io::Error::new(io::ErrorKind::Other, "host dns addr is empty")); + } let dns_result = DnsResult::new(addrs.clone(), Instant::now() + ttl); map_lock.insert(authority, dns_result); Ok(ResolvedAddrs::new(addrs.into_iter())) -- Gitee