use std::time::{SystemTime, UNIX_EPOCH};
pub(crate) fn domain_matches(host: &str, domain: &str) -> bool {
let h = host.to_lowercase();
let d = domain.to_lowercase();
if h == d {
return true;
}
if is_numeric_host(host) {
return false;
}
h.ends_with(&format!(".{}", d))
}
pub(crate) fn path_matches(cookie_path: &str, request_path: &str) -> bool {
if cookie_path == request_path {
return true;
}
if !request_path.starts_with(cookie_path) {
return false;
}
if cookie_path.ends_with('/') {
return true;
}
let remaining = &request_path[cookie_path.len()..];
remaining.starts_with('/')
}
pub(crate) fn is_numeric_host(host: &str) -> bool {
let is_ipv4 = host.chars().all(|c| c.is_ascii_digit() || c == '.');
if is_ipv4 && host.contains('.') {
return true;
}
if host.contains(':') {
return true;
}
false
}
pub(crate) fn now_secs() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64
}
pub fn format_http_date(epoch: i64) -> String {
const DAYS: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let days_since_epoch = epoch.div_euclid(86_400);
let seconds = epoch.rem_euclid(86_400);
let (y, m, day) = civil_from_days(days_since_epoch);
let hour = seconds / 3_600;
let min = (seconds % 3_600) / 60;
let sec = seconds % 60;
let dow = (days_since_epoch + 4).rem_euclid(7) as usize;
format!(
"{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT",
DAYS[dow],
day,
MONTHS[(m - 1) as usize],
y,
hour,
min,
sec
)
}
pub(crate) fn parse_http_date(s: &str) -> Option<i64> {
const MONTH_NAMES: [&str; 12] = [
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
];
fn is_delimiter(c: u8) -> bool {
c == 0x09
|| (0x20..=0x2F).contains(&c)
|| (0x3B..=0x40).contains(&c)
|| (0x5B..=0x60).contains(&c)
|| (0x7B..=0x7E).contains(&c)
}
let bytes = s.trim().as_bytes();
let mut tokens: Vec<&str> = Vec::new();
let mut i = 0;
while i < bytes.len() {
if is_delimiter(bytes[i]) {
i += 1;
continue;
}
let start = i;
while i < bytes.len() && !is_delimiter(bytes[i]) {
i += 1;
}
tokens.push(std::str::from_utf8(&bytes[start..i]).ok()?);
}
let mut found_time = false;
let mut hour: u32 = 0;
let mut minute: u32 = 0;
let mut second: u32 = 0;
let mut found_day = false;
let mut day_of_month: u32 = 0;
let mut found_month = false;
let mut month: u32 = 0;
let mut found_year = false;
let mut year: u32 = 0;
for token in &tokens {
if !found_time && let Some((h, m, s)) = parse_time_token(token) {
hour = h;
minute = m;
second = s;
found_time = true;
continue;
}
if !found_day {
let digits = leading_digits(token);
if (1..=2).contains(&digits)
&& digits == token.len()
&& let Ok(d) = token.parse::<u32>()
{
day_of_month = d;
found_day = true;
continue;
}
}
if !found_month && token.len() >= 3 {
let lower = token.to_lowercase();
for (idx, &name) in MONTH_NAMES.iter().enumerate() {
if lower.starts_with(name) {
month = (idx + 1) as u32;
found_month = true;
break;
}
}
if found_month {
continue;
}
}
if !found_year {
let digits = leading_digits(token);
if (1..=4).contains(&digits)
&& digits == token.len()
&& let Ok(y) = token.parse::<u32>()
{
year = y;
found_year = true;
continue;
}
}
}
if (70..=99).contains(&year) {
year += 1900;
} else if year <= 69 {
year += 2000;
}
if !found_time || !found_day || !found_month || !found_year {
return None;
}
if !(1..=31).contains(&day_of_month) || year < 1601 || hour > 23 || minute > 59 || second > 59 {
return None;
}
let max_day = match month {
4 | 6 | 9 | 11 => 30,
2 => {
let is_leap =
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400);
if is_leap { 29 } else { 28 }
}
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
_ => return None,
};
if day_of_month > max_day {
return None;
}
let total_days = days_since_epoch(year, month, day_of_month);
Some(total_days * 86400 + hour as i64 * 3600 + minute as i64 * 60 + second as i64)
}
fn parse_time_token(token: &str) -> Option<(u32, u32, u32)> {
let parts: Vec<&str> = token.split(':').collect();
if parts.len() != 3 {
return None;
}
for p in &parts {
let digits = leading_digits(p);
if digits == 0 || digits > 2 || digits != p.len() {
return None;
}
}
let h: u32 = parts[0].parse().ok()?;
let m: u32 = parts[1].parse().ok()?;
let s: u32 = parts[2].parse().ok()?;
Some((h, m, s))
}
fn leading_digits(s: &str) -> usize {
s.bytes().take_while(|c| c.is_ascii_digit()).count()
}
fn days_since_epoch(year: u32, month: u32, day: u32) -> i64 {
let y = year as i64 - if month <= 2 { 1 } else { 0 };
let era = if y >= 0 { y / 400 } else { (y - 399) / 400 };
let year_of_era = y - era * 400;
let month_from_march = month as i64 + if month > 2 { -3 } else { 9 };
let day_of_year = (153 * month_from_march + 2) / 5 + day as i64 - 1;
let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
era * 146_097 + day_of_era - 719_468
}
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = if z >= 0 {
z / 146_097
} else {
(z - 146_096) / 146_097
};
let day_of_era = z - era * 146_097;
let year_of_era =
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let year = year_of_era + era * 400;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_from_march = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * month_from_march + 2) / 5 + 1;
let month = month_from_march + if month_from_march < 10 { 3 } else { -9 };
let year = year + if month <= 2 { 1 } else { 0 };
(year, month as u32, day as u32)
}