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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/**
* Cookie according to RFC 9110
*/
class Cookie {
Str name;
Str value;
Str path = ""; // Change to URL?
Str domain ="";
Str expires =""; // Maybe add a time class? There is one in Storm already but too basic
Nat maxAge = 0;
Bool secure = false;
Bool httpOnly = false;
Str sameSite ="";
Bool cookieValid = true;
void toS(StrBuf to) : override {
to << name << "=" << value;
if (path != "")
to << "; Path=" << path;
if (domain != "")
to << "; Domain=" << domain;
if (expires != "")
to << "; Expires=" << expires;
if (maxAge != 0)
to << "; Max-Age: " << maxAge;
if (secure)
to << "; Secure";
if (httpOnly)
to << "; HttpOnly";
if (sameSite != "")
to << "; SamSite=" << sameSite;
}
}
// TODO: It does not seem like this is used.
Bool responseCookieParser(Cookie cookie) on Compiler {
initMonth(); //Call in server
initDays(); //Call in server
Str:Iter iter;
if (cookie.name != "") {
cookie.name = cookie.name;
} else {
return false;
}
if (cookie.value != "") {
//parse here if string in ascii US
iter = cookie.value.find(' ', cookie.value.begin());
if (iter != cookie.value.end())
return false;
iter = cookie.value.find(',' , cookie.value.begin());
if (iter != cookie.value.end())
return false;
iter = cookie.value.find(';' , cookie.value.begin());
if (iter != cookie.value.end())
return false;
} else {
return false;
}
if (cookie.path != "") {
iter = cookie.path.find(';', cookie.path.begin());
if (iter != cookie.path.end())
return false;
//parse here
}
if (cookie.domain != "") {
//Should be subdomain ?
// parse here
// cookie.domain = trimWhitespace(cookie.domain);
}
if (cookie.expires != "") {
StrBuf buf;
iter = cookie.expires.find(' ', cookie.expires.begin());
buf << cookie.expires.cut(cookie.expires.begin(), iter);
Str:Iter iter2;
for(Int i = 0; i < 4; i++) {
if (iter == cookie.expires.end())
return false;
iter2 = iter + 1;
iter = cookie.expires.find(' ', iter2);
buf << cookie.expires.cut(iter2, iter);
}
Str cookiestr = buf.toS();
// print("\n\n"+cookiestr+"\n\n");
if (cookiestr.count() != 24) {
return false;
}
if ("," != cookiestr.cut(cookiestr.begin()+3, cookiestr.begin()+4))
return false;
iter = cookiestr.begin()+3;
if (!dayFormatIMF.has(cookiestr.cut(cookiestr.begin(), iter))) {
return false;
}
Str month = cookiestr.cut(iter+3, iter+6);
if (!monthFormatIMF.has(month)) {
return false;
}
Str day = cookiestr.cut(iter+1, iter+3);
if (val = day.int) {
if (val < 1 | val > monthFormatIMF.get(month)) //Check if day is correct in month
return false;
} else {
return false;
}
Str year = cookiestr.cut(iter+6, iter+10);
if (val = year.int) {
if (val < 1970)
return false;
} else {
return false;
}
Str hour = cookiestr.cut(iter+10, iter+12);
if (val = hour.int) {
if (val < 0 | val > 23)
return false;
} else {
return false;
}
if (cookiestr.cut(iter+12, iter+13) != ":" | cookiestr.cut(iter+15, iter+16) != ":" )
return false;
Str minute = cookiestr.cut(iter+13, iter+15);
if (val = minute.int) {
if (val < 0 | val > 59)
return false;
} else {
return false;
}
Str second = cookiestr.cut(iter+16, iter+18);
if (val = second.int) {
if (val < 0 | val > 60)
return false;
} else {
return false;
}
if (cookiestr.cut(iter+18, cookiestr.end()) != "GMT")
return false;
//parse here
}
if (cookie.sameSite != "") {
if (cookie.sameSite != "None")
if (cookie.sameSite != "Lax")
if (cookie.sameSite != "Strict")
return false;
}
return true;
}
|