timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

misc cookies #602

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

links

timotheecour commented 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'")
timotheecour commented 3 years ago

IIRC 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)
timotheecour commented 3 years ago

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)
timotheecour commented 3 years ago

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

timotheecour commented 3 years ago
timotheecour commented 3 years ago