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(crate) 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 epoch = epoch as u64;
let days_since_epoch = epoch / 86400;
let mut y = 1970u32;
let mut remaining = days_since_epoch as u32;
loop {
let leap = y.is_multiple_of(4) && !y.is_multiple_of(100) || y.is_multiple_of(400);
let days_in_year = if leap { 366 } else { 365 };
if remaining < days_in_year {
break;
}
remaining -= days_in_year;
y += 1;
}
let mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let mut m = 0u32;
while m < 12 {
let dim =
if m == 1 && (y.is_multiple_of(4) && !y.is_multiple_of(100) || y.is_multiple_of(400)) {
29
} else {
mdays[m as usize]
};
if remaining < dim {
break;
}
remaining -= dim;
m += 1;
}
let day = remaining + 1;
let secs = epoch % 86400;
let hour = secs / 3600;
let min = (secs % 3600) / 60;
let sec = secs % 60;
let dow = ((y + (y / 4) - (y / 100) + (y / 400) + (13 * m + 1) / 5 + day + 308) % 7) as usize;
format!(
"{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT",
DAYS[dow], day, MONTHS[m 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 as i64 * 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) -> u32 {
let mut days: u32 = 0;
for y in 1970..year {
days += if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 {
366
} else {
365
};
}
let mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let is_leap = (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400);
for m in 1..month {
days += if m == 2 && is_leap {
29
} else {
mdays[(m - 1) as usize]
};
}
days + day - 1
}