diff --git a/ylong_http/src/body/mime/common/headers.rs b/ylong_http/src/body/mime/common/headers.rs index db37a026b1a817305a6540a5649b77494bc08ed7..e5cfddea9d4869b8271c8901ebaf438d6a6921de 100644 --- a/ylong_http/src/body/mime/common/headers.rs +++ b/ylong_http/src/body/mime/common/headers.rs @@ -20,8 +20,8 @@ use crate::body::mime::common::{ use crate::body::mime::{CR, LF}; use crate::body::TokenStatus; use crate::error::{ErrorKind, HttpError}; -use crate::h1::response::decoder::{HEADER_NAME_BYTES, HEADER_VALUE_BYTES}; use crate::headers::{HeaderName, HeaderValue, Headers}; +use crate::util::header_bytes::{HEADER_NAME_BYTES, HEADER_VALUE_BYTES}; #[derive(Debug, PartialEq)] pub(crate) enum HeaderStatus { diff --git a/ylong_http/src/h1/request/encoder.rs b/ylong_http/src/h1/request/encoder.rs index 38f2af9f16ba50bd9aac80fa50d255d530bd4433..9efcf154103700de47dc0405b8598bc5d66b22b7 100644 --- a/ylong_http/src/h1/request/encoder.rs +++ b/ylong_http/src/h1/request/encoder.rs @@ -33,6 +33,7 @@ //! // Gets `RequestPart`. //! let (part, _) = request.into_parts(); //! let mut encoder = RequestEncoder::new(part); +//! encoder.absolute_uri(true); //! //! // We use `message` to store all the body data. //! let mut message = Vec::new(); @@ -90,6 +91,7 @@ use crate::version::Version; /// // Gets `RequestPart`. /// let (part, _) = request.into_parts(); /// let mut encoder = RequestEncoder::new(part); +/// encoder.absolute_uri(true); /// /// // We use `message` to store all the body data. /// let mut message = Vec::new(); @@ -230,6 +232,7 @@ impl RequestEncoder { /// /// let (part, _) = request.into_parts(); /// let mut encoder = RequestEncoder::new(part); + /// encoder.absolute_uri(true); /// /// let mut buf = [0_u8; 10]; /// let mut message = Vec::new(); @@ -704,6 +707,7 @@ mod ut_request_encoder { let (part, _) = request.into_parts(); let mut encoder = RequestEncoder::new(part); + encoder.absolute_uri(true); let mut buf = [0u8; 5]; let mut res = Vec::new(); loop { diff --git a/ylong_http/src/h1/response/decoder.rs b/ylong_http/src/h1/response/decoder.rs index 9ed61f05bbb708238f5c28dee26ef2d3eba55c7b..881d5f54a2b7c140caea766b4c90f0c65f719e5e 100644 --- a/ylong_http/src/h1/response/decoder.rs +++ b/ylong_http/src/h1/response/decoder.rs @@ -18,6 +18,7 @@ use crate::h1::H1Error; use crate::headers::Headers; use crate::response::status::StatusCode; use crate::response::ResponsePart; +use crate::util::header_bytes::{HEADER_NAME_BYTES, HEADER_VALUE_BYTES}; use crate::version::Version; /// `HTTP/1` response decoder, which support decoding multi-segment byte @@ -636,83 +637,6 @@ fn is_legal_reason_byte(byte: u8) -> bool { byte == 0x09 || byte == 0x20 || (0x21..=0x7E).contains(&byte) || (0x80..=0xFF).contains(&byte) } -// token = 1*tchar -// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" -// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" -// / DIGIT / ALPHA -// ; any VCHAR, except delimiters -// delimitersd = DQUOTE and "(),/:;<=>?@[\]{}" -#[rustfmt::skip] -pub(crate) static HEADER_NAME_BYTES: [bool; 256] = { - const __: bool = false; - const TT: bool = true; - [ -// \0 HT LF CR - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, -// \w ! " # $ % & ' ( ) * + , - . / - __, TT, __, TT, TT, TT, TT, TT, __, __, TT, TT, __, TT, TT, __, -// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, __, __, __, __, __, -// @ A B C D E F G H I J K L M N O - __, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// P Q R S T U V W X Y Z [ \ ] ^ _ - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, __, __, TT, TT, -// ` a b c d e f g h i j k l m n o - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// p q r s t u v w x y z { | } ~ del - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, TT, __, TT, __, -// Expand ascii - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, - ] -}; - -// field-value = *( field-content / obs-fold ) -// field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] -// field-vchar = VCHAR / obs-text -// -// obs-fold = CRLF 1*( SP / HTAB ) -// ; obsolete line folding -// ; see Section 3.2.4 -#[rustfmt::skip] -pub(crate) static HEADER_VALUE_BYTES: [bool; 256] = { - const __: bool = false; - const TT: bool = true; - [ -// \0 HT LF CR - __, __, __, __, __, __, __, __, __, TT, __, __, __, __, __, __, - __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, -// \w ! " # $ % & ' ( ) * + , - . / - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// @ A B C D E F G H I J K L M N O - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// P Q R S T U V W X Y Z [ \ ] ^ _ - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// ` a b c d e f g h i j k l m n o - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, -// p q r s t u v w x y z { | } ~ del - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, -// Expand ascii - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, - ] -}; - // TODO: Add more test cases. #[cfg(test)] mod ut_decoder { diff --git a/ylong_http/src/h2/decoder.rs b/ylong_http/src/h2/decoder.rs index fdde469c7694f4cf7ebf87640e4ce743beaf9ede..64f164370280fa07421e2508557ef5e971a8148e 100644 --- a/ylong_http/src/h2/decoder.rs +++ b/ylong_http/src/h2/decoder.rs @@ -883,7 +883,7 @@ mod ut_frame_decoder { use crate::h2::decoder::{get_setting, FrameDecoder, FrameHeader, FrameKind}; use crate::h2::frame::{Payload, Ping, Setting}; use crate::h2::{ErrorCode, H2Error, PseudoHeaders}; - use crate::test_util::decode; + use crate::util::test_util::decode; macro_rules! check_complete_frame { ( diff --git a/ylong_http/src/h2/hpack/decoder.rs b/ylong_http/src/h2/hpack/decoder.rs index 8022c9b224f33fda5add229fba3abf8926da5513..15d7f6e93d5998a0fde402987b433591458adb6c 100644 --- a/ylong_http/src/h2/hpack/decoder.rs +++ b/ylong_http/src/h2/hpack/decoder.rs @@ -208,7 +208,7 @@ fn header_line_length(key_size: usize, value_size: usize) -> usize { mod ut_hpack_decoder { use crate::h2::hpack::table::Header; use crate::h2::hpack::HpackDecoder; - use crate::test_util::decode; + use crate::util::test_util::decode; const MAX_HEADER_LIST_SIZE: usize = 16 << 20; diff --git a/ylong_http/src/h2/hpack/encoder.rs b/ylong_http/src/h2/hpack/encoder.rs index f1eccd91ea7a814a38aa0d86afa147dd0bbaae1b..3b2a71dec1264bb2151b60a0835c6cdd8b043209 100644 --- a/ylong_http/src/h2/hpack/encoder.rs +++ b/ylong_http/src/h2/hpack/encoder.rs @@ -61,7 +61,7 @@ mod ut_hpack_encoder { use crate::h2::hpack::table::Header; use crate::h2::hpack::HpackEncoder; use crate::h2::Parts; - use crate::test_util::decode; + use crate::util::test_util::decode; #[test] fn ut_hpack_encoder() { diff --git a/ylong_http/src/h2/hpack/representation/decoder.rs b/ylong_http/src/h2/hpack/representation/decoder.rs index 1ac595fe0117df7f5c7160212d926fb4efda219f..952b18c5d339ee295347e949996a09981c054bc9 100644 --- a/ylong_http/src/h2/hpack/representation/decoder.rs +++ b/ylong_http/src/h2/hpack/representation/decoder.rs @@ -568,7 +568,7 @@ impl From for DecResult { #[cfg(test)] mod ut_repr_decoder { use super::*; - use crate::test_util::decode; + use crate::util::test_util::decode; /// UT test cases for `ReprDecoder`. /// diff --git a/ylong_http/src/h3/qpack/decoder.rs b/ylong_http/src/h3/qpack/decoder.rs index bf33bb463cf2ddc22f9a4986fb62acfd84133c42..c2ec9061a0e23b0716d670a8c7f1b36515cd46d4 100644 --- a/ylong_http/src/h3/qpack/decoder.rs +++ b/ylong_http/src/h3/qpack/decoder.rs @@ -589,7 +589,7 @@ mod ut_qpack_decoder { use crate::h3::qpack::format::decoder::ReprDecodeState; use crate::h3::qpack::table::{DynamicTable, Field}; use crate::h3::qpack::QpackDecoder; - use crate::test_util::decode; + use crate::util::test_util::decode; const MAX_HEADER_LIST_SIZE: usize = 16 << 20; diff --git a/ylong_http/src/h3/qpack/encoder.rs b/ylong_http/src/h3/qpack/encoder.rs index 5f01bc3310656dc9236a6d141c1b298588101ccd..f1e2a23827befa1cd266baa4e021c5a59446c7c6 100644 --- a/ylong_http/src/h3/qpack/encoder.rs +++ b/ylong_http/src/h3/qpack/encoder.rs @@ -304,7 +304,7 @@ mod ut_qpack_encoder { use crate::h3::qpack::encoder; use crate::h3::qpack::encoder::QpackEncoder; use crate::h3::qpack::table::{DynamicTable, Field}; - use crate::test_util::decode; + use crate::util::test_util::decode; macro_rules! qpack_test_cases { ($enc: expr,$encoder_buf:expr,$encoder_cur:expr, $len: expr, $res: literal,$encoder_res: literal, $size: expr, { $($h: expr, $v: expr $(,)?)*} $(,)?) => { let mut _encoder = $enc; diff --git a/ylong_http/src/huffman/mod.rs b/ylong_http/src/huffman/mod.rs index faa5d961f2f4a8babca059c0017d5b426a7442bc..903eca028245d01c1bcc4145bd51f0eee81c36c4 100644 --- a/ylong_http/src/huffman/mod.rs +++ b/ylong_http/src/huffman/mod.rs @@ -246,7 +246,7 @@ pub(crate) enum HuffmanDecodeError { #[cfg(test)] mod ut_huffman { use super::{huffman_decode, huffman_encode, HuffmanDecoder}; - use crate::test_util::decode; + use crate::util::test_util::decode; /// UT test cases for `huffman_encode`. /// diff --git a/ylong_http/src/lib.rs b/ylong_http/src/lib.rs index 8a6f7337648917400505a09ba6c9dc7e7541b02e..b99a3b36e594bf0ccc678f9d4f35b016e8933298 100644 --- a/ylong_http/src/lib.rs +++ b/ylong_http/src/lib.rs @@ -42,8 +42,7 @@ pub mod request; pub mod response; pub mod version; -#[cfg(test)] -pub(crate) mod test_util; +pub(crate) mod util; #[cfg(feature = "tokio_base")] pub(crate) use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; diff --git a/ylong_http/src/util/header_bytes.rs b/ylong_http/src/util/header_bytes.rs new file mode 100644 index 0000000000000000000000000000000000000000..75aa7fafb214ad9fd6ae89f6023b6ee70beabb64 --- /dev/null +++ b/ylong_http/src/util/header_bytes.rs @@ -0,0 +1,91 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! HTTP Legitimate Header characters. + +// token = 1*tchar +// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" +// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" +// / DIGIT / ALPHA +// ; any VCHAR, except delimiters +// delimitersd = DQUOTE and "(),/:;<=>?@[\]{}" +#[rustfmt::skip] +pub(crate) static HEADER_NAME_BYTES: [bool; 256] = { + const __: bool = false; + const TT: bool = true; + [ +// \0 HT LF CR + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, +// \w ! " # $ % & ' ( ) * + , - . / + __, TT, __, TT, TT, TT, TT, TT, __, __, TT, TT, __, TT, TT, __, +// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, __, __, __, __, __, +// @ A B C D E F G H I J K L M N O + __, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// P Q R S T U V W X Y Z [ \ ] ^ _ + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, __, __, TT, TT, +// ` a b c d e f g h i j k l m n o + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// p q r s t u v w x y z { | } ~ del + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, TT, __, TT, __, +// Expand ascii + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, + ] +}; + +// field-value = *( field-content / obs-fold ) +// field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] +// field-vchar = VCHAR / obs-text +// +// obs-fold = CRLF 1*( SP / HTAB ) +// ; obsolete line folding +// ; see Section 3.2.4 +#[rustfmt::skip] +pub(crate) static HEADER_VALUE_BYTES: [bool; 256] = { + const __: bool = false; + const TT: bool = true; + [ +// \0 HT LF CR + __, __, __, __, __, __, __, __, __, TT, __, __, __, __, __, __, + __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, +// \w ! " # $ % & ' ( ) * + , - . / + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// @ A B C D E F G H I J K L M N O + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// P Q R S T U V W X Y Z [ \ ] ^ _ + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// ` a b c d e f g h i j k l m n o + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, +// p q r s t u v w x y z { | } ~ del + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, __, +// Expand ascii + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, TT, + ] +}; diff --git a/ylong_http/src/util/mod.rs b/ylong_http/src/util/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..7beb0b66e74b5a01d5bc35a12479a8c557afbe48 --- /dev/null +++ b/ylong_http/src/util/mod.rs @@ -0,0 +1,18 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Common util module. + +pub(crate) mod header_bytes; +#[cfg(test)] +pub(crate) mod test_util; diff --git a/ylong_http/src/test_util.rs b/ylong_http/src/util/test_util.rs similarity index 96% rename from ylong_http/src/test_util.rs rename to ylong_http/src/util/test_util.rs index 0d554802b60f2524b93e701a72d2a2ab943e4e51..a45e2bc0e845391b63220cdac3e14b1bbabf47ed 100644 --- a/ylong_http/src/test_util.rs +++ b/ylong_http/src/util/test_util.rs @@ -11,6 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Convert a str slice to u8 vector. + pub fn decode(str: &str) -> Option> { if str.len() % 2 != 0 { return None; diff --git a/ylong_http_client/src/async_impl/conn/http1.rs b/ylong_http_client/src/async_impl/conn/http1.rs index d05403a08ebe690e8aec47916a17970cbb9b6626..2de4fe9b231a58b4055c0c065415ee0276312b70 100644 --- a/ylong_http_client/src/async_impl/conn/http1.rs +++ b/ylong_http_client/src/async_impl/conn/http1.rs @@ -120,13 +120,13 @@ where } }; + // The shutdown function only sets the current connection to the closed state + // and does not release the connection immediately. + // Instead, the connection will be completely closed + // when the body has finished reading or when the body is released. match part.headers.get("Connection") { None => { if part.version == Version::HTTP1_0 { - // The shutdown function only sets the current connection to the closed state - // and does not release the connection immediately. - // Instead, the connection will be completely closed - // when the body has finished reading or when the body is released. conn.shutdown() } } diff --git a/ylong_http_client/src/lib.rs b/ylong_http_client/src/lib.rs index 1cad613724b87eef9aa0b5498bd3076577bcb57c..c943878630c003064d960c89e7be15303ec61176 100644 --- a/ylong_http_client/src/lib.rs +++ b/ylong_http_client/src/lib.rs @@ -20,6 +20,7 @@ // TODO: Need doc. // ylong_http crate re-export. +#[cfg(any(feature = "ylong_base", feature = "tokio_base"))] pub use ylong_http::body::{EmptyBody, TextBody}; pub use ylong_http::request::method::Method; pub use ylong_http::request::uri::{Scheme, Uri}; diff --git a/ylong_http_client/src/util/proxy.rs b/ylong_http_client/src/util/proxy.rs index d38d80dd70b986b10955ec29b665387c400aeb60..3a7d5d9947adecf07fbe0e724cfd09e544578167 100644 --- a/ylong_http_client/src/util/proxy.rs +++ b/ylong_http_client/src/util/proxy.rs @@ -258,10 +258,7 @@ impl NoProxy { } else if domain.ends_with(&block_domain) { // .example.com and www. if block_domain.starts_with('.') - || domain - .as_bytes() - .get(domain.len() - block_domain.len() - 1) - == Some(&b'.') + || domain.as_bytes().get(domain.len() - block_domain.len() - 1) == Some(&b'.') { return true; }