Open timotheecour opened 3 years ago
TODO: max-age http://promincproductions.com/blog/set-cookie-expiration-date-browser-compatiability/ using maybe:
proc formatExpires*(time: Time): string =
result = format(time.utc, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'")
addCookie
to set multiple cookies in a single web requestIIRC I needed something addCookie
when using jester, and jester's setCookie
wasn't flexible enough, probably because of Unable to set multiple cookies in a single web request · Issue #224 · dom96/jester
template setCookie*(name, value: string, expires="",
sameSite: SameSite=Lax, secure = false,
httpOnly = false, domain = "", path = "") =
here's what I wrote instead:
proc addCookie*(headers: var Option[RawHeaders], cookie: string) =
# note: requires makeCookie eg: (key, value, expires: string, domain = "", path = "", secure = false, httpOnly = false, sameSite = Lax): string =
# EG: addCookie(result[2], cookie)
if isSome(headers) and
(let headers2 = headers.get(); headers2.toTable.hasKey("Set-Cookie")):
headers = some(headers2 & @({"Set-Cookie": cookie}))
else:
# Note: requires modif to jester
setHeader(headers, "Set-Cookie", cookie)
I use something like this (adapted from jester API/code)
proc removeCookie*(headers: var Option[RawHeaders], cookieKey: string, path = "") =
## https://stackoverflow.com/questions/5285940/correct-way-to-delete-cookies-server-side
let expires = "Thu, 01 Jan 1970 00:00:00 GMT"
# consider using fixed key for cookie value
let cookie = makeCookie(cookieKey, "", expires = expires, path = path)
addCookie(headers = headers, cookie = cookie)
StringTableRef
doesn't seem correct for parseCookies
because IIUC we can use duplicate keys (eg with different paths etc):nim r --eval:'import std/[cookies,strtabs]; echo parseCookies("a=1;Path=/; a=2;Path=/example;")'
prints {a: 1, Path: /}
which seems wrong according to https://stackoverflow.com/questions/4056306/how-to-handle-multiple-cookies-with-the-same-name
links