Source Edit

This module implements helper procs for parsing Cookies.

Imports

strtabs, times, options

Types

  1. SameSite {.pure.} = enum
  2. Default, None, Lax, Strict

The SameSite cookie attribute. Default means that setCookie proc will not set SameSite attribute. Source Edit

Procs

  1. proc parseCookies(s: string): StringTableRef {....raises: [], tags: [], forbids: [].}

Parses cookies into a string table.

The proc is meant to parse the Cookie header set by a client, not the “Set-Cookie” header set by servers.

Example:

  1. import std/strtabs
  2. let cookieJar = parseCookies("a=1; foo=bar")
  3. assert cookieJar["a"] == "1"
  4. assert cookieJar["foo"] == "bar"

Source Edit

  1. proc setCookie(key, value: string; domain = ""; path = ""; expires = "";
  2. noName = false; secure = false; httpOnly = false;
  3. maxAge = none(int); sameSite = SameSite.Default): string {.
  4. ...raises: [], tags: [], forbids: [].}

Creates a command in the format of Set-Cookie: key=value; Domain=…; …

Tip: Cookies can be vulnerable. Consider setting secure=true, httpOnly=true and sameSite=Strict.

Source Edit

  1. proc setCookie(key, value: string; expires: DateTime | Time; domain = "";
  2. path = ""; noName = false; secure = false; httpOnly = false;
  3. maxAge = none(int); sameSite = SameSite.Default): string

Creates a command in the format of Set-Cookie: key=value; Domain=…; … Source Edit