diff --git a/actix-http/src/header/mod.rs b/actix-http/src/header/mod.rs index 9017f15aa..2b8678edc 100644 --- a/actix-http/src/header/mod.rs +++ b/actix-http/src/header/mod.rs @@ -14,6 +14,7 @@ use crate::HttpMessage; mod as_name; mod into_pair; mod into_value; +mod utils; pub(crate) mod map; mod shared; @@ -27,6 +28,7 @@ pub use self::into_value::IntoHeaderValue; #[doc(hidden)] pub use self::map::GetAll; pub use self::map::HeaderMap; +pub use self::utils::*; /// A trait for any object that already represents a valid header field and value. pub trait Header: IntoHeaderValue { diff --git a/actix-http/src/header/shared/content_encoding.rs b/actix-http/src/header/shared/content_encoding.rs index 51e65b5cf..b93d66101 100644 --- a/actix-http/src/header/shared/content_encoding.rs +++ b/actix-http/src/header/shared/content_encoding.rs @@ -4,7 +4,7 @@ use http::header::InvalidHeaderValue; use crate::{ error::ParseError, - header::{self, Header, HeaderName, HeaderValue, IntoHeaderValue}, + header::{self, from_one_raw_str, Header, HeaderName, HeaderValue, IntoHeaderValue}, HttpMessage, }; @@ -101,14 +101,6 @@ impl Header for ContentEncoding { } fn parse(msg: &T) -> Result { - let val = msg.headers().get(Self::name()); - - if let Some(line) = val { - let line = line.to_str().map_err(|_| ParseError::Header)?; - if !line.is_empty() { - return Self::from_str(line).or(Err(ParseError::Header)); - } - } - Err(ParseError::Header) + from_one_raw_str(msg.headers().get(Self::name())) } } diff --git a/src/http/header/utils.rs b/actix-http/src/header/utils.rs similarity index 79% rename from src/http/header/utils.rs rename to actix-http/src/header/utils.rs index a1258368c..5e9652380 100644 --- a/src/http/header/utils.rs +++ b/actix-http/src/header/utils.rs @@ -1,7 +1,7 @@ use std::{fmt, str::FromStr}; use super::HeaderValue; -use crate::error::ParseError; +use crate::{error::ParseError, header::HTTP_VALUE}; /// Reads a comma-delimited raw header into a Vec. #[inline] @@ -53,3 +53,10 @@ where } Ok(()) } + +/// Percent encode a sequence of bytes with a character set defined in +/// +pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result { + let encoded = percent_encoding::percent_encode(bytes, HTTP_VALUE); + fmt::Display::fmt(&encoded, f) +} diff --git a/src/http/header/mod.rs b/src/http/header/mod.rs index a77f0c352..1a2cca451 100644 --- a/src/http/header/mod.rs +++ b/src/http/header/mod.rs @@ -30,7 +30,7 @@ pub use self::if_range::IfRange; pub use self::if_unmodified_since::IfUnmodifiedSince; pub use self::last_modified::LastModified; //pub use self::range::{Range, ByteRangeSpec}; -pub(crate) use self::utils::{fmt_comma_delimited, from_comma_delimited, from_one_raw_str}; +pub(crate) use actix_http::http::header::{fmt_comma_delimited, from_comma_delimited, from_one_raw_str}; #[doc(hidden)] #[macro_export] @@ -353,4 +353,3 @@ mod if_none_match; mod if_range; mod if_unmodified_since; mod last_modified; -mod utils;