From 6f8c3caabc92cbdecf01171efe705fb5a52523fd Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Wed, 29 Nov 2023 14:45:21 +0800 Subject: [PATCH] change content-length type Signed-off-by: Tiga Ultraman --- ylong_http/src/body/text.rs | 11 ++++++----- ylong_http_client/src/async_impl/conn/http2.rs | 2 +- ylong_http_client/src/async_impl/http_body.rs | 4 ++-- ylong_http_client/src/sync_impl/conn/http1.rs | 2 +- ylong_http_client/src/sync_impl/http_body.rs | 4 ++-- ylong_http_client/src/util/normalizer.rs | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ylong_http/src/body/text.rs b/ylong_http/src/body/text.rs index dc2e046..58010d6 100644 --- a/ylong_http/src/body/text.rs +++ b/ylong_http/src/body/text.rs @@ -306,7 +306,7 @@ impl async_impl::Body for TextBody> { /// assert_eq!(left, b"[REDUNDANT DATA]"); /// ``` pub struct TextBodyDecoder { - left: usize, + left: u64, } impl TextBodyDecoder { @@ -321,7 +321,7 @@ impl TextBodyDecoder { /// /// let decoder = TextBodyDecoder::new(10); /// ``` - pub fn new(length: usize) -> TextBodyDecoder { + pub fn new(length: u64) -> TextBodyDecoder { TextBodyDecoder { left: length } } @@ -378,12 +378,13 @@ impl TextBodyDecoder { return (Text::complete(&buf[..0]), buf); } - let size = min(self.left, buf.len()); + let size = min(self.left, buf.len() as u64); self.left -= size; + let end = size as usize; if self.left == 0 { - (Text::complete(&buf[..size]), &buf[size..]) + (Text::complete(&buf[..end]), &buf[end..]) } else { - (Text::partial(&buf[..size]), &buf[size..]) + (Text::partial(&buf[..end]), &buf[end..]) } } } diff --git a/ylong_http_client/src/async_impl/conn/http2.rs b/ylong_http_client/src/async_impl/conn/http2.rs index 94c755a..1d5bf33 100644 --- a/ylong_http_client/src/async_impl/conn/http2.rs +++ b/ylong_http_client/src/async_impl/conn/http2.rs @@ -144,7 +144,7 @@ where .headers .get("Content-Length") .map(|v| v.to_str().unwrap_or(String::new())) - .and_then(|s| s.parse::().ok()); + .and_then(|s| s.parse::().ok()); match content_length { None => HttpBody::empty(), Some(0) => HttpBody::empty(), diff --git a/ylong_http_client/src/async_impl/http_body.rs b/ylong_http_client/src/async_impl/http_body.rs index 0acbb18..669f737 100644 --- a/ylong_http_client/src/async_impl/http_body.rs +++ b/ylong_http_client/src/async_impl/http_body.rs @@ -99,7 +99,7 @@ impl HttpBody { } #[cfg(feature = "http2")] - pub(crate) fn text(len: usize, pre: &[u8], io: BoxStreamData) -> Self { + pub(crate) fn text(len: u64, pre: &[u8], io: BoxStreamData) -> Self { Self { kind: Kind::Text(Text::new(len, pre, io)), sleep: None, @@ -283,7 +283,7 @@ struct Text { } impl Text { - pub(crate) fn new(len: usize, pre: &[u8], io: BoxStreamData) -> Self { + pub(crate) fn new(len: u64, pre: &[u8], io: BoxStreamData) -> Self { Self { decoder: TextBodyDecoder::new(len), pre: (!pre.is_empty()).then_some(Cursor::new(pre.to_vec())), diff --git a/ylong_http_client/src/sync_impl/conn/http1.rs b/ylong_http_client/src/sync_impl/conn/http1.rs index bfa41f7..ef89318 100644 --- a/ylong_http_client/src/sync_impl/conn/http1.rs +++ b/ylong_http_client/src/sync_impl/conn/http1.rs @@ -107,7 +107,7 @@ where .headers .get("Content-Length") .map(|v| v.to_str().unwrap_or_default()) - .and_then(|s| s.parse::().ok()); + .and_then(|s| s.parse::().ok()); let is_trailer = part.headers.get("Trailer").is_some(); diff --git a/ylong_http_client/src/sync_impl/http_body.rs b/ylong_http_client/src/sync_impl/http_body.rs index 37ecaa7..c7b1317 100644 --- a/ylong_http_client/src/sync_impl/http_body.rs +++ b/ylong_http_client/src/sync_impl/http_body.rs @@ -57,7 +57,7 @@ impl HttpBody { Self { kind: Kind::Empty } } - pub(crate) fn text(len: usize, pre: &[u8], io: BoxStreamData) -> Self { + pub(crate) fn text(len: u64, pre: &[u8], io: BoxStreamData) -> Self { Self { kind: Kind::Text(Text::new(len, pre, io)), } @@ -84,7 +84,7 @@ struct Text { } impl Text { - pub(crate) fn new(len: usize, pre: &[u8], io: BoxStreamData) -> Self { + pub(crate) fn new(len: u64, pre: &[u8], io: BoxStreamData) -> Self { Self { decoder: TextBodyDecoder::new(len), pre: (!pre.is_empty()).then_some(Cursor::new(pre.to_vec())), diff --git a/ylong_http_client/src/util/normalizer.rs b/ylong_http_client/src/util/normalizer.rs index d187489..13e4320 100644 --- a/ylong_http_client/src/util/normalizer.rs +++ b/ylong_http_client/src/util/normalizer.rs @@ -158,7 +158,7 @@ impl<'a> BodyLengthParser<'a> { if content_length.is_some() { let content_length_valid = content_length .and_then(|v| v.to_str().ok()) - .and_then(|s| s.parse::().ok()); + .and_then(|s| s.parse::().ok()); return match content_length_valid { // If `content-length` is 0, the io stream cannot be read, @@ -179,7 +179,7 @@ impl<'a> BodyLengthParser<'a> { pub(crate) enum BodyLength { #[cfg(feature = "http1_1")] Chunk, - Length(usize), + Length(u64), Empty, UntilClose, } -- Gitee