#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetCookieHeader {
pub name: String,
pub value: String,
pub attributes: Vec<SetCookieAttribute>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetCookieAttribute {
pub name: String,
pub value: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CookiePair {
pub name: String,
pub value: String,
}
pub fn parse_cookie_header(header: &str) -> Vec<CookiePair> {
header
.split(';')
.filter_map(|pair| {
let (name, value) = pair.trim().split_once('=')?;
let name = name.trim();
(!name.is_empty()).then(|| CookiePair {
name: name.to_owned(),
value: value.trim().to_owned(),
})
})
.collect()
}
pub fn format_cookie_header<'a>(cookies: impl IntoIterator<Item = (&'a str, &'a str)>) -> String {
let mut header = String::new();
for (name, value) in cookies {
if !header.is_empty() {
header.push_str("; ");
}
header.push_str(name);
header.push('=');
header.push_str(value);
}
header
}
pub fn format_set_cookie_header<'a>(
name: &str,
value: &str,
attributes: &[(&'a str, Option<&'a str>)],
) -> String {
let mut header = format!("{name}={value}");
for (attribute_name, attribute_value) in attributes {
header.push_str("; ");
header.push_str(attribute_name);
if let Some(attribute_value) = attribute_value {
header.push('=');
header.push_str(attribute_value);
}
}
header
}
pub fn parse_set_cookie_header(header: &str) -> Option<SetCookieHeader> {
let header = header.trim();
if header.is_empty() {
return None;
}
let (name_value, attributes) = match header.split_once(';') {
Some((name_value, attributes)) => (name_value, Some(attributes)),
None => (header, None),
};
let name_value = name_value.trim();
let (name, value) = name_value.split_once('=')?;
let name = name.trim();
if name.is_empty() {
return None;
}
let mut value = value.trim();
if value.len() >= 2 && value.starts_with('"') && value.ends_with('"') {
value = &value[1..value.len() - 1];
}
let attributes = attributes
.into_iter()
.flat_map(|attributes| attributes.split(';'))
.filter_map(|attribute| {
let attribute = attribute.trim();
if attribute.is_empty() {
return None;
}
let (name, value) = match attribute.split_once('=') {
Some((name, value)) => (name.trim(), Some(value.trim().to_owned())),
None => (attribute, None),
};
(!name.is_empty()).then(|| SetCookieAttribute {
name: name.to_ascii_lowercase(),
value,
})
})
.collect();
Some(SetCookieHeader {
name: name.to_owned(),
value: value.to_owned(),
attributes,
})
}
pub 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 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 fn default_path(request_uri_path: &str) -> String {
if request_uri_path.is_empty() || !request_uri_path.starts_with('/') {
return "/".to_owned();
}
let Some(last_slash) = request_uri_path.rfind('/') else {
return "/".to_owned();
};
if last_slash == 0 {
"/".to_owned()
} else {
request_uri_path[..last_slash].to_owned()
}
}
pub 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 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 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)
}
#[cfg(test)]
mod tests {
use super::{
default_path, domain_matches, format_cookie_header, format_http_date,
format_set_cookie_header, parse_cookie_header, parse_http_date, parse_set_cookie_header,
path_matches,
};
#[test]
fn parses_set_cookie_wire_fields_and_preserves_attribute_order() {
let cookie = parse_set_cookie_header(
" SID = \"abc=123\" ; Domain=.Example.test; Secure; SameSite=Lax; Extension=one=two ",
)
.unwrap();
assert_eq!(cookie.name, "SID");
assert_eq!(cookie.value, "abc=123");
assert_eq!(
cookie.attributes,
vec![
super::SetCookieAttribute {
name: "domain".to_owned(),
value: Some(".Example.test".to_owned()),
},
super::SetCookieAttribute {
name: "secure".to_owned(),
value: None,
},
super::SetCookieAttribute {
name: "samesite".to_owned(),
value: Some("Lax".to_owned()),
},
super::SetCookieAttribute {
name: "extension".to_owned(),
value: Some("one=two".to_owned()),
},
]
);
}
#[test]
fn parses_bare_set_cookie_and_rejects_missing_name_or_value_separator() {
assert_eq!(
parse_set_cookie_header("SID=abc"),
Some(super::SetCookieHeader {
name: "SID".to_owned(),
value: "abc".to_owned(),
attributes: vec![],
})
);
assert!(parse_set_cookie_header("").is_none());
assert!(parse_set_cookie_header("missing-separator").is_none());
assert!(parse_set_cookie_header("=value").is_none());
}
#[test]
fn parses_request_cookie_pairs_and_skips_malformed_entries() {
assert_eq!(
parse_cookie_header(" a=1; malformed; token = value=tail; =empty; b=2 "),
vec![
super::CookiePair {
name: "a".to_owned(),
value: "1".to_owned(),
},
super::CookiePair {
name: "token".to_owned(),
value: "value=tail".to_owned(),
},
super::CookiePair {
name: "b".to_owned(),
value: "2".to_owned(),
},
]
);
}
#[test]
fn formats_request_cookie_pairs() {
assert_eq!(format_cookie_header([]), "");
assert_eq!(
format_cookie_header([("sid", "abc"), ("theme", "dark")]),
"sid=abc; theme=dark"
);
}
#[test]
fn formats_set_cookie_attributes_in_order() {
assert_eq!(
format_set_cookie_header(
"sid",
"abc",
&[("Path", Some("/")), ("Secure", None), ("HttpOnly", None)],
),
"sid=abc; Path=/; Secure; HttpOnly"
);
}
#[test]
fn parses_cookie_and_http_date_forms() {
assert_eq!(
parse_http_date("Wed, 09 Jun 2021 10:18:14 GMT"),
Some(1_623_233_894)
);
assert_eq!(parse_http_date("06-Nov-94 08:49:37 GMT"), Some(784_111_777));
assert_eq!(parse_http_date("not a date"), None);
}
#[test]
fn formats_http_date_and_matches_cookie_scopes() {
assert_eq!(
format_http_date(1_623_233_894),
"Wed, 09 Jun 2021 10:18:14 GMT"
);
assert!(domain_matches("download.example.test", "example.test"));
assert!(path_matches("/files", "/files/item.bin"));
assert!(!path_matches("/files", "/files-other/item.bin"));
}
#[test]
fn computes_default_cookie_paths() {
assert_eq!(default_path(""), "/");
assert_eq!(default_path("relative/path"), "/");
assert_eq!(default_path("/"), "/");
assert_eq!(default_path("/file.bin"), "/");
assert_eq!(default_path("/a/b"), "/a");
assert_eq!(default_path("/a/b/"), "/a/b");
}
}