On this page:
Cookie
Cookie.name
Cookie.value
Cookie.max_  age
Cookie.domain
Cookie.path
Cookie.extension
Cookie.is_  secure
Cookie.is_  http_  only
Cookie.clear_  string
Cookie.header_  to_  map
Cookie.handle
9.0.0.5

2 Cookies for Servers🔗ℹ

 import: net/cookie/server package: rhombus-net-cookie-lib

The net/cookie/server library provides cookie support for servers, which need to create cookies and encode them for response headers.

class

class server.Cookie(

  name :: cookie.Name,

  value :: cookie.Value,

  ~exp_date: exp_date :: maybe(date.ZonedDateTime) = #false,

  ~max_age: max_age :: maybe(PosInt) = #false,

  ~domain: domain :: maybe(cookie.Domain) = #false,

  ~path: path :: maybe(cookie.PathOrExtension) = #false,

  ~extension: extension :: maybe(cookie.PathOrExtension) = #false,

  ~is_secure: is_secure :: Boolean = #false,

  ~is_http_only: is_http_only :: Boolean = #false

)

Constructs a cookie for sending to a user agent (i.e., client). The #'text-mode printed form of the cookie, which can be obtained via to_string, is a values for a Set-Cookie HTTP response header suitable for sending the cookie to a user agent.

> to_string(server.Cookie("rememberUser", "bob", ~path: "/main"))

"rememberUser=bob; Path=/main"

Both exp_date and max_age are for specifying a time at which the user agent should remove the cookie from its cookie store. The exp_date argument is for specifying this expiration time as a date, while max_age is for specifying it as a number of seconds in the future. If both exp_date and max_age are given as non-#false, an RFC 6265-compliant user agent will disregard the exp_date and use the max_age.

A non-#false domain argument indicates that the recipient should send the cookie back to the server only if the hostname in the request URI is either domain itself or a host within domain.

A non-#false path argument indicates that the recipient should send the cookie back to the server only if path is a prefix of the request URI’s path.

When is_secure is #true, a flag tells the recipient that the cookie may only be sent if the request URI’s scheme specifies a “secure” protocol (presumably HTTPS).

When is_http_only is #true, a flag tells the recipient that the cookie may be communicated only to a server and only via HTTP or HTTPS.

The flag is_http_only and is_secure flags are important for security. Browsers provide JavaScript access to cookies (for example, via document.cookie), and consequently, when cookies contain sensitive data such as user session info, malicious JavaScript can compromise that data. The HttpOnly cookie flag, set by is_http_only argument, instructs the browser not to make this cookie available to JavaScript code. If a cookie is intended to be confidential, both is_secure and is_http_only should be #true, and all connections should use HTTPS. Some older browsers do not support this flag; see the OWASP page on HttpOnly for more info.

function

fun server.Cookie.clear_string(

  name :: cookie.Name,

  ~domain: domain :: maybe(cookie.Domain) = #false,

  ~path: path :: maybe(cookie.PathOrExtension) = #false

) :: String

Produces a string containing a Set-Cookie header value suitable for telling a user agent to clear the cookie with the given name. This is done, as per RFC 6265, by sending a cookie with an expiration date in the past.

> server.Cookie.clear_string("rememberUser", ~path: "/main")

"rememberUser=; Expires=Thu, 01 Jan 2015 00:00:00 GMT; Path=/main"

function

fun server.Cookie.header_to_map(

  header :: Bytes,

) :: Map.of(Bytes, Bytes)

 

function

fun server.Cookie.header_to_map(

  header :~ Bytes,

  ~decode: decode :: (Bytes -> Any)

) :: Map

Given the value part of a Cookie header, produces a map of all cookie-name-to-value mappings in the header. If a decode function is given, it is applied to each key and each value before inserting a map. If a key in the header has no value, then #"", or decode(#"") is used as the value. Invalid cookies will not be present in the result.

> server.Cookie.header_to_map(#"SID=31d4d96e407aad42; lang=en-US")

{

  Bytes.copy(#"SID"): Bytes.copy(#"31d4d96e407aad42"),

  Bytes.copy(#"lang"): Bytes.copy(#"en-US")

}

> server.Cookie.header_to_map(#"SID=31d4d96e407aad42; lang=en-US",

                              ~decode: Bytes.utf8_string)

{"SID": "31d4d96e407aad42", "lang": "en-US"}

> server.Cookie.header_to_map(

    #"seenIntro=; logins=3",

    ~decode: (fun (s): String.maybe_to_number(s) || s)

                Bytes.utf8_string

  )

{"logins": 3, "seenIntro": ""}

property

property (c :: server.Cookie).handle

Returns the cookie’s internal representation as recognized by Racket libraries.