HeaderName parse function implementation.

This commit is contained in:
Mauro D
2022-10-21 07:15:42 +00:00
parent fbd53cb094
commit 58b1bc8966
2 changed files with 26 additions and 24 deletions
-10
View File
@@ -509,16 +509,6 @@ impl<'x> Hash for HeaderName<'x> {
impl Eq for HeaderName<'_> {}
impl<'x> HeaderName<'x> {
/// Parse a header name
pub fn parse(header_name: impl Into<Cow<'x, str>>) -> Self {
let header_name = header_name.into();
if let Some(rfc_header) = RfcHeader::parse(header_name.as_ref()) {
HeaderName::Rfc(rfc_header)
} else {
HeaderName::Other(header_name)
}
}
pub fn as_str(&self) -> &str {
match self {
HeaderName::Rfc(header) => header.as_str(),
+26 -14
View File
@@ -9,6 +9,8 @@
* except according to those terms.
*/
use std::borrow::Cow;
use crate::{Header, HeaderName, RfcHeader};
use super::MessageStream;
@@ -166,25 +168,31 @@ impl<'x> MessageStream<'x> {
}
}
impl RfcHeader {
pub fn parse(data: &str) -> Option<RfcHeader> {
impl<'x> HeaderName<'x> {
/// Parse a header name
pub fn parse(data: impl Into<Cow<'x, str>>) -> Option<HeaderName<'x>> {
let mut token_hash: usize = 0;
let mut last_ch: u8 = 0;
let data = data.into();
let data_ = data.as_bytes();
for (pos, &ch) in data_.iter().enumerate() {
if let 0 | 9 = pos {
token_hash += {
#[cfg(feature = "ludicrous_mode")]
unsafe {
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
}
if ch.is_ascii_alphanumeric() || [b'_', b'-'].contains(&ch) {
if let 0 | 9 = pos {
token_hash += {
#[cfg(feature = "ludicrous_mode")]
unsafe {
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
}
#[cfg(not(feature = "ludicrous_mode"))]
HDR_HASH[ch.to_ascii_lowercase() as usize]
} as usize;
#[cfg(not(feature = "ludicrous_mode"))]
HDR_HASH[ch.to_ascii_lowercase() as usize]
} as usize;
}
last_ch = ch;
} else {
return None;
}
last_ch = ch;
}
if (2..=25).contains(&data.len()) {
@@ -202,12 +210,16 @@ impl RfcHeader {
let token_hash = token_hash - 4;
if data_.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
return HDR_MAP[token_hash].into();
return HeaderName::Rfc(HDR_MAP[token_hash]).into();
}
}
}
None
if !data.is_empty() {
HeaderName::Other(data).into()
} else {
None
}
}
}