Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc cookies #602

Open
timotheecour opened this issue Feb 20, 2021 · 6 comments
Open

misc cookies #602

timotheecour opened this issue Feb 20, 2021 · 6 comments

Comments

@timotheecour
Copy link
Owner Author

  • set cookie expiration date

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
Copy link
Owner Author

  • addCookie to set multiple cookies in a single web request

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
Copy link
Owner Author

  • removeCookie

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
Copy link
Owner Author

  • using 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

@timotheecour
Copy link
Owner Author

@timotheecour
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant