netkit/http/cookies

This module provides the Cookie type, which directly maps to Set-Cookie HTTP response headers, and the CookieJar type which contains many cookies.

Overview

Cookie type is used to generate Set-Cookie HTTP response headers. Server sends Set-Cookie HTTP response headers to the user agent. So the user agent can send them back to the server later.

CookieJar contains many cookies from the user agent.

Types

SameSite{...}{.pure.} = enum
  None,
  Lax,
  Strict
The SameSite cookie attribute.   Source Edit
Cookie = object
  name*, value*: string
  expires*: string
  maxAge*: Option[int]
  domain*: string
  path*: string
  secure*: bool
  httpOnly*: bool
  sameSite*: SameSite
Cookie type represents Set-Cookie HTTP response headers.   Source Edit
CookieJar = object
  data: StringTableRef
CookieJar type is a collection of cookies.   Source Edit
MissingValueError = object of ValueError
Indicates an error associated with Cookie.   Source Edit

Procs

proc initCookie(name, value: string; expires = ""; maxAge: Option[int] = none(int);
               domain = ""; path = ""; secure = false; httpOnly = false; sameSite = Lax): Cookie {...}{.
    inline, raises: [], tags: [].}
Initiates Cookie object.

Examples:

let
  username = "admin"
  message = "ok"
  cookie = initCookie(username, message)
doAssert cookie.name == username
doAssert cookie.value == message
  Source Edit
proc initCookie(name, value: string; expires: DateTime | Time;
               maxAge: Option[int] = none(int); domain = ""; path = ""; secure = false;
               httpOnly = false; sameSite = Lax): Cookie {...}{.inline.}
Initiates Cookie object.

Examples:

import
  times

let
  username = "admin"
  message = "ok"
  expires = now()
  cookie = initCookie(username, message, expires)
doAssert cookie.name == username
doAssert cookie.value == message
  Source Edit
proc initCookie(text: string): Cookie {...}{.inline, raises: [MissingValueError], tags: [].}
Initiates Cookie object from strings.

Examples:

doAssert initCookie("foo=bar=baz").name == "foo"
doAssert initCookie("foo=bar=baz").value == "bar=baz"
doAssert initCookie("foo=bar; HttpOnly").httpOnly
  Source Edit
proc setCookie(cookie: Cookie): string {...}{.raises: [UnpackError], tags: [].}
Stringifys Cookie object to get Set-Cookie HTTP response headers.

Examples:

import
  strformat

let
  username = "admin"
  message = "ok"
  cookie = initCookie(username, message)
doAssert setCookie(cookie) == fmt"{username}={message}; SameSite=Lax"
  Source Edit
proc `$`(cookie: Cookie): string {...}{.inline, raises: [UnpackError], tags: [].}
Stringifys Cookie object to get Set-Cookie HTTP response headers.

Examples:

import
  strformat

let
  username = "admin"
  message = "ok"
  cookie = initCookie(username, message)
doAssert $cookie == fmt"{username}={message}; SameSite=Lax"
  Source Edit
proc initCookieJar(): CookieJar {...}{.inline, raises: [], tags: [].}
Creates a new cookieJar that is empty.   Source Edit
proc len(cookieJar: CookieJar): int {...}{.inline, raises: [], tags: [].}
Returns the number of names in cookieJar.   Source Edit
proc `[]`(cookieJar: CookieJar; name: string): string {...}{.inline, raises: [KeyError],
    tags: [].}
  Source Edit
proc getOrDefault(cookieJar: CookieJar; name: string; default = ""): string {...}{.inline,
    raises: [], tags: [].}
Retrieves the value at cookieJar[name] if name is in cookieJar. Otherwise, the default value is returned(default is "").   Source Edit
proc hasKey(cookieJar: CookieJar; name: string): bool {...}{.inline, raises: [], tags: [].}
Returns true if name is in the cookieJar.   Source Edit
proc contains(cookieJar: CookieJar; name: string): bool {...}{.inline, raises: [], tags: [].}
Returns true if name is in the cookieJar. Alias of hasKey for use with the in operator.   Source Edit
proc `[]=`(cookieJar: var CookieJar; name: string; value: string) {...}{.inline, raises: [],
    tags: [].}
Inserts a (name, value) pair into cookieJar.   Source Edit
proc parse(cookieJar: var CookieJar; text: string) {...}{.inline, raises: [], tags: [].}
Parses CookieJar from strings.

Examples:

var cookieJar = initCookieJar()
cookieJar.parse("username=netkit; message=ok")
doAssert cookieJar["username"] == "netkit"
doAssert cookieJar["message"] == "ok"
  Source Edit

Iterators

iterator pairs(cookieJar: CookieJar): tuple[name, value: string] {...}{.raises: [], tags: [].}
Iterates over any (name, value) pair in the cookieJar.   Source Edit
iterator keys(cookieJar: CookieJar): string {...}{.raises: [], tags: [].}
Iterates over any name in the cookieJar.   Source Edit
iterator values(cookieJar: CookieJar): string {...}{.raises: [], tags: [].}
Iterates over any value in the cookieJar.   Source Edit