From b47148f793a6fc4e7618e4ff180a373be8442962 Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Wed, 27 Mar 2024 14:53:13 +0800 Subject: [PATCH] fix chunk and connection bug Signed-off-by: Tiga Ultraman --- ylong_http/src/body/chunk.rs | 14 ++++++++-- .../src/async_impl/conn/http1.rs | 2 +- ylong_http_client/src/async_impl/http_body.rs | 27 +++++++++++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ylong_http/src/body/chunk.rs b/ylong_http/src/body/chunk.rs index 923a918..4cbc2be 100644 --- a/ylong_http/src/body/chunk.rs +++ b/ylong_http/src/body/chunk.rs @@ -1020,7 +1020,7 @@ impl ChunkBodyDecoder { continue; } - if *b == b':' { + if *b == b':' && colon == 0 { colon = i; if lf == 0 { let trailer_name = &self.trailer[..colon]; @@ -1037,12 +1037,22 @@ impl ChunkBodyDecoder { break; } lf = i; - let trailer_value = &self.trailer[colon + 1..lf - 1]; + let mut trailer_value = &self.trailer[colon + 1..lf - 1]; + if let Some(start) = trailer_value.iter().position(|b| *b != b' ' && *b != b'\t') { + trailer_value = &trailer_value[start..]; + } + if let Some(end) = trailer_value + .iter() + .rposition(|b| *b != b' ' && *b != b'\t') + { + trailer_value = &trailer_value[..end + 1]; + } let trailer_header_value = HeaderValue::from_bytes(trailer_value)?; let _ = trailer_headers.insert::( trailer_header_name.clone(), trailer_header_value.clone(), )?; + colon = 0; } } diff --git a/ylong_http_client/src/async_impl/conn/http1.rs b/ylong_http_client/src/async_impl/conn/http1.rs index 15416e3..277f7be 100644 --- a/ylong_http_client/src/async_impl/conn/http1.rs +++ b/ylong_http_client/src/async_impl/conn/http1.rs @@ -145,7 +145,7 @@ where .to_string() .ok() .and_then(|v| v.find("close")) - .is_none() + .is_some() { conn.shutdown() } diff --git a/ylong_http_client/src/async_impl/http_body.rs b/ylong_http_client/src/async_impl/http_body.rs index 432058c..e2e3f2e 100644 --- a/ylong_http_client/src/async_impl/http_body.rs +++ b/ylong_http_client/src/async_impl/http_body.rs @@ -529,12 +529,13 @@ mod ut_async_http_body { #[test] fn ut_asnyc_chunk_trailer_1() { let handle = ylong_runtime::spawn(async move { - asnyc_chunk_trailer_1().await; + async_chunk_trailer_1().await; + async_chunk_trailer_2().await; }); ylong_runtime::block_on(handle).unwrap(); } - async fn asnyc_chunk_trailer_1() { + async fn async_chunk_trailer_1() { let box_stream = Box::new("".as_bytes()); let chunk_body_bytes = "\ 5\r\n\ @@ -586,6 +587,28 @@ mod ut_async_http_body { assert!(res.is_none()); } + async fn async_chunk_trailer_2() { + let box_stream = Box::new("".as_bytes()); + let chunk_body_bytes = "\ + 5\r\n\ + hello\r\n\ + C ; type = text ;end = !\r\n\ + hello world!\r\n\ + 000; message = last\r\n\ + Expires: Wed, 21 Oct 2015 07:27:00 GMT \r\n\r\n\ + "; + let mut chunk = + HttpBody::new(BodyLength::Chunk, box_stream, chunk_body_bytes.as_bytes()).unwrap(); + let res = async_impl::Body::trailer(&mut chunk) + .await + .unwrap() + .unwrap(); + assert_eq!( + res.get("expires").unwrap().to_string().unwrap(), + "Wed, 21 Oct 2015 07:27:00 GMT".to_string() + ); + } + /// UT test cases for `Body::data`. /// /// # Brief -- Gitee