use super::*;
#[inline]
pub(crate) fn idempotency_now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|dur: Duration| dur.as_millis() as u64)
.unwrap_or(0)
}
#[inline]
pub(crate) fn idempotency_shard_index(key: &str) -> usize {
let mut hasher: XxHash3_64 = XxHash3_64::default();
hasher.write(key.as_bytes());
(hasher.finish() as usize) & (IDEMPOTENCY_SHARD_COUNT - 1)
}
#[inline]
pub fn compute_request_fingerprint(method: &Method, path: &str, body: &[u8]) -> u64 {
let mut hasher: XxHash3_64 = XxHash3_64::default();
let method_tag: u8 = match method {
Method::Get => 1,
Method::Post => 2,
Method::Put => 3,
Method::Delete => 4,
Method::Patch => 5,
Method::Head => 6,
Method::Options => 7,
Method::Connect => 8,
Method::Trace => 9,
Method::Unknown(_) => 10,
};
hasher.write(&[method_tag]);
if let Method::Unknown(raw) = method {
hasher.write(raw.as_bytes());
}
hasher.write(FINGERPRINT_SEPARATOR);
hasher.write(path.as_bytes());
hasher.write(FINGERPRINT_SEPARATOR);
hasher.write(body);
hasher.finish()
}
pub(crate) fn build_storage_key(method: &Method, path: &str, client_key: &str) -> String {
let method_str: String = method.to_string();
let mut out: String =
String::with_capacity(method_str.len() + path.len() + client_key.len() + 2);
out.push_str(&method_str);
out.push('|');
out.push_str(path);
out.push('|');
out.push_str(client_key);
out
}
#[inline]
pub(crate) fn find_header_back_ref<'a>(
headers: &'a RequestHeaders,
lower_name: &str,
) -> Option<&'a str> {
if let Some(vals) = headers.get(lower_name) {
return vals.back().map(String::as_str);
}
headers
.iter()
.find(|(k, _): &(&String, &VecDeque<String>)| k.eq_ignore_ascii_case(lower_name))
.and_then(|(_, v): (&String, &VecDeque<String>)| v.back().map(String::as_str))
}
#[inline]
pub(crate) fn is_valid_idempotency_key(key: &str, max_len: usize) -> bool {
!key.is_empty() && key.len() <= max_len && key.bytes().all(|b: u8| b >= 0x20 && b != 0x7F)
}
pub(crate) fn normalize_prefix(prefix: &str) -> String {
let trimmed: &str = prefix.trim();
if trimmed == "/" {
return "/".to_owned();
}
let stripped: &str = trimmed.trim_end_matches('/');
if stripped.is_empty() {
"/".to_owned()
} else {
stripped.to_owned()
}
}
#[inline]
pub(crate) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
if prefix == "/" {
return true;
}
let Some(rest) = path.strip_prefix(prefix) else {
return false;
};
rest.is_empty() || rest.starts_with('/')
}
pub(crate) fn estimate_response_bytes(key: &str, resp: &StoredIdempotentResponse) -> usize {
let header_bytes: usize = resp
.get_headers()
.iter()
.map(|(k, v): &(String, String)| k.len() + v.len())
.sum();
key.len()
+ header_bytes
+ resp.get_body().len()
+ resp.get_replayed_wire().len()
+ ENTRY_OVERHEAD_BYTES
}
pub(crate) fn build_static_error_wire(
status: HttpStatus,
body: &[u8],
retry_after: Option<&str>,
) -> ResponseData {
let mut resp: Response = Response::default();
resp.set_status_code(status.code())
.set_header(CONTENT_TYPE, ERROR_CONTENT_TYPE)
.set_body(body);
if let Some(retry) = retry_after {
resp.set_header(RETRY_AFTER, retry);
}
resp.build()
}