use super::*;
#[inline(always)]
pub fn hash_entity_bytes(body: &[u8]) -> u64 {
let mut hasher: XxHash3_64 = XxHash3_64::default();
hasher.write(body);
hasher.finish()
}
#[inline(always)]
pub(crate) fn format_strong_etag_stack(digest: u64) -> [u8; 18] {
let mut out: [u8; 18] = [b'"'; 18];
let bytes: [u8; 8] = digest.to_be_bytes();
for i in 0..8usize {
out[1 + i * 2] = HEX_LOWER[(bytes[i] >> 4) as usize];
out[2 + i * 2] = HEX_LOWER[(bytes[i] & 0x0f) as usize];
}
out
}
#[inline(always)]
pub(crate) fn format_weak_etag_stack(digest: u64) -> [u8; 20] {
let mut out: [u8; 20] = [b'"'; 20];
out[0] = b'W';
out[1] = b'/';
let bytes: [u8; 8] = digest.to_be_bytes();
for i in 0..8usize {
out[3 + i * 2] = HEX_LOWER[(bytes[i] >> 4) as usize];
out[4 + i * 2] = HEX_LOWER[(bytes[i] & 0x0f) as usize];
}
out
}
pub fn compute_etag(body: &[u8], strength: EtagStrength) -> String {
let digest: u64 = hash_entity_bytes(body);
match strength {
EtagStrength::Strong => {
let buf: [u8; 18] = format_strong_etag_stack(digest);
from_utf8(&buf).unwrap_or_default().to_owned()
}
EtagStrength::Weak => {
let buf: [u8; 20] = format_weak_etag_stack(digest);
from_utf8(&buf).unwrap_or_default().to_owned()
}
}
}
pub fn matches_if_match_strong(header_value: &str, current_etag: Option<&str>) -> bool {
let trimmed: &str = header_value.trim();
if trimmed == "*" {
return current_etag.is_some();
}
let Some(current) = current_etag else {
return false;
};
if current.starts_with(WEAK_PREFIX) {
return false;
}
for token in trimmed.split(',') {
let cand: &str = token.trim();
if !cand.starts_with(WEAK_PREFIX) && cand == current {
return true;
}
}
false
}
#[inline(always)]
pub(crate) fn status_has_no_body(status: usize) -> bool {
(100..200).contains(&status) || status == 204 || status == 304
}
pub(crate) fn normalize_prefix(raw: &str) -> String {
let trimmed: &str = raw.trim().trim_end_matches('/');
if trimmed.is_empty() || trimmed == ROOT_SLASH {
return ROOT_SLASH.to_owned();
}
if trimmed.starts_with('/') {
trimmed.to_owned()
} else {
format!("/{trimmed}")
}
}
#[inline]
pub(crate) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
if prefix == ROOT_SLASH {
return true;
}
if let Some(rest) = path.strip_prefix(prefix) {
rest.is_empty() || rest.starts_with('/')
} else {
false
}
}