move header parsing utils into -http

This commit is contained in:
ibraheemdev 2021-03-25 14:39:07 -04:00
parent 3ccc28611d
commit aa6deb1909
4 changed files with 13 additions and 13 deletions

View File

@ -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 {

View File

@ -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<T: HttpMessage>(msg: &T) -> Result<Self, ParseError> {
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()))
}
}

View File

@ -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
/// <https://tools.ietf.org/html/rfc5987#section-3.2>
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)
}

View File

@ -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;