use super::*;
#[inline(always)]
pub(crate) fn request_header_back<'a>(req: &'a Request, lower_key: &str) -> Option<&'a str> {
req.get_headers()
.get(lower_key)
.and_then(|values: &VecDeque<String>| values.back())
.map(String::as_str)
}
#[inline(always)]
pub(crate) fn ends_with_ignore_ascii_case(haystack: &str, suffix: &str) -> bool {
let h_len: usize = haystack.len();
let s_len: usize = suffix.len();
if h_len < s_len {
return false;
}
haystack.as_bytes()[h_len - s_len..].eq_ignore_ascii_case(suffix.as_bytes())
}
#[inline]
pub(crate) fn extract_origin_host(origin: &str) -> &str {
let trimmed: &str = origin.trim();
let without_scheme: &str = match trimmed.split_once(ORIGIN_SCHEME_SEP) {
Some((_, rest)) => rest,
None => return "",
};
let authority: &str = without_scheme.split('/').next().unwrap_or(without_scheme);
if authority.starts_with('[') {
match authority.find(']') {
Some(end) => &authority[..=end],
None => "",
}
} else {
authority.split(':').next().unwrap_or(authority)
}
}
#[inline]
pub(crate) fn merge_vary_header(response: &mut Response, token: &str) {
if let Some(values) = response.get_mut_headers().get_mut(VARY)
&& let Some(last) = values.back_mut()
{
let already: bool = last
.split(',')
.any(|part: &str| part.trim().eq_ignore_ascii_case(token));
if !already {
last.reserve(COMMA_SPACE.len() + token.len());
last.push_str(COMMA_SPACE);
last.push_str(token);
}
return;
}
response.set_header(VARY, token);
}
pub(crate) fn should_mirror_request_headers(headers: &[String]) -> bool {
headers.is_empty()
|| headers
.iter()
.any(|item: &String| item.trim() == CORS_WILDCARD)
}
#[inline(always)]
pub(crate) fn is_valid_cors_header_value(value: &str) -> bool {
value
.bytes()
.all(|b: u8| b == b'\t' || (b >= 0x20 && b != 0x7f))
}