-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcookie.c3
96 lines (89 loc) · 2.34 KB
/
cookie.c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module cookie;
import std::collections::map;
struct Cookie
{
String name;
String value;
String domain; // if starting with . for all subdomains valid
String path;
String expires;
String max_age;
String same_site;
// flags
bool secure;
bool http_only;
}
// Cookie Jar
struct CookieJar
{
HashMap(<String, Cookie>) cookies;
}
fn void CookieJar.set_cookie(&self, Cookie cookie)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", cookie.name, cookie.domain, cookie.path);
self.cookies[key.str_view()] = cookie;
}
fn Cookie! CookieJar.get_cookie(&self, String name , String domain, String path)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", name, domain, path);
return self.cookies[key.str_view()];
}
///
fn String Cookie.to_string(&self)
{
DString result;
result.new_init();
result.appendf(
"<Cookie %s=%s; Domain=%s; Path=%s; Expires=%s; Max-Age=%s; SameSite=%s; Secure=%b; HttpOnly=%b>",
self.name,
self.value,
self.domain,
self.path,
self.expires,
self.max_age,
self.same_site,
self.secure,
self.http_only
);
return result.str_view();
}
fn void Cookie.parse(&self, String value)
{
// iterate over every set-cookie attribute
foreach(ix, attribute: value.trim().split(";"))
{
// parse key value attributes
if(attribute.contains("="))
{
String[] kv = attribute.trim().split("=");
// the first key value is always the cookie name and cookie value
if(ix == 0)
{
self.name = kv[0];
self.value = kv[1];
}
else
{
switch(attribute.temp_ascii_to_lower())
{
case "domain": self.domain = kv[1];
case "path": self.path = kv[1];
case "expires": self.expires = kv[1];
case "max-age": self.max_age = kv[1];
case "same-site": self.same_site = kv[1];
}
}
continue;
}
// parse flag attributes
switch(attribute.temp_ascii_to_lower())
{
case "secure": self.secure = true;
case "httponly": self.http_only = true;
}
}
}