use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn merge_headers_to_map(headers: &[(String, String)]) -> HashMap<String, String> {
let mut map = HashMap::new();
for (k, v) in headers {
map.entry(k.clone())
.and_modify(|existing: &mut String| {
existing.push_str(", ");
existing.push_str(v);
})
.or_insert_with(|| v.clone());
}
map
}
pub fn is_hop_by_hop_header(name: &str) -> bool {
let lower = name.to_lowercase();
lower == "transfer-encoding" || lower == "connection" || lower == "keep-alive"
}
pub fn encode_body_for_stub(body: &[u8]) -> (Option<serde_json::Value>, bool) {
if body.is_empty() {
return (None, false);
}
match std::str::from_utf8(body) {
Ok(text) => {
let val = if let Ok(json_val) = serde_json::from_str::<serde_json::Value>(text) {
json_val
} else {
serde_json::Value::String(text.to_string())
};
(Some(val), false)
}
Err(_) => {
use base64::Engine;
let encoded = base64::engine::general_purpose::STANDARD.encode(body);
(Some(serde_json::Value::String(encoded)), true)
}
}
}
pub fn unix_timestamp() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}