///|
pub fn Cookie::new(
name : String,
value : String,
path? : String,
expires_raw? : String,
max_age? : Int64,
domain? : String,
secure? : Bool = false,
http_only? : Bool = false,
extensions? : Array[String] = [],
) -> Cookie {
{
name,
value,
path,
expires_raw,
max_age,
domain,
secure,
http_only,
extensions,
}
}
///|
#cfg(target="native")
fn Cookie::parse(line : StringView) -> Cookie raise {
guard line.find("=") is Some(name_end) else { raise BadRequest }
let name = line[:name_end].to_owned()
guard line[name_end + 1:].find(";") is Some(value_end) else {
Cookie(name, line[name_end + 1:].to_owned())
}
let value_end = name_end + 1 + value_end
let value = line[name_end + 1:value_end].to_owned()
let mut path = None
let mut expires_raw = None
let mut max_age = None
let mut domain = None
let mut secure = false
let mut http_only = false
let extensions = []
for parsed = value_end + 1 {
let attr_end = if line[parsed:].find(";") is Some(attr_end) {
parsed + attr_end
} else {
line.length()
}
let attr = line[parsed:attr_end].trim()
if attr.find("=") is Some(attr_name_end) {
let name = attr[:attr_name_end]
let value = attr[attr_name_end + 1:]
match name.to_lower() {
"path" => path = Some(value.to_owned())
"expires" => expires_raw = Some(value.to_owned())
"max-age" => {
let value = @string.parse_int64(value) catch { _ => raise BadRequest }
max_age = Some(value)
}
"domain" => domain = Some(value.to_owned())
_ => extensions.push(attr.to_owned())
}
} else {
match attr.to_lower() {
"secure" => secure = true
"httponly" => http_only = true
_ => extensions.push(attr.to_owned())
}
}
if attr_end == line.length() {
break
} else {
continue attr_end + 1
}
}
{
name,
value,
path,
expires_raw,
max_age,
domain,
secure,
http_only,
extensions,
}
}