iyi

Standard library, 0.18.0

std/cookie

Cookies (RFC 6265), for both ends of std/http: Cookie reads a Set-Cookie line as a user agent reads one and writes one as a server writes one, and CookieJar is the client's store, handed to HTTP.request(..., jar: jar) (it is std/http's CookieStore).

import std/cookie to use it. Source: src/std/cookie.iyi at v0.18.0.

A line is read as §5.2 has it: a name and value, then attributes in any case, an unknown one passed over, Expires in any of RFC 9110's three date forms, Max-Age before Expires (§5.3, step 3), a Domain's leading dot dropped. A line without a name-value pair is no cookie. A server's line is refused, with the byte that would break it, when its name is not a token or its value holds what a cookie-octet cannot (§4.1.1): a space, a comma, a semicolon, a backslash, a quote.

The jar matches a request as §5.4 does - domain, path at a slash, not lapsed - sends the longer paths first, and sends a Secure cookie over https:// alone, taking one only from an answer that came over it (RFC 6265bis: an http page cannot overwrite a secure cookie). There is no public-suffix list: a Domain of one label is refused, and one like co.uk is taken, which a browser would refuse. This module is apart from std/http because it reads the clock: std/time is what knows what an Expires date means, and std/http reaches the network alone.

CookieJarclass

The cookies a client keeps between requests (RFC 6265 §5.3, §5.4): what each answer's Set-Cookie lines set, by domain and path, and what a request to a URL sends back. A cookie without Domain goes back to its own host alone; one with a Domain the host is not within is refused, and so is a Domain of one label, com - there is no public-suffix list here, so a cookie for co.uk would be taken, which a browser would refuse. A cookie that has lapsed, or is set lapsed, is gone. A Secure cookie is taken from an https:// answer only, and goes back over https:// only (§5.4 step 1). A jar may be shared by tasks on one thread, not across threads.

std/cookieiyi doc src/std/cookie.iyi
pub class CookieJar
  # *cookie*, as set by an answer from *url*: it replaces one of the same
  # name, domain and path, and one set already lapsed removes it.
  def add(url : String, cookie : Cookie) : Nil
  # The cookies a request to *url* carries: domain and path matching,
  # unexpired, `Secure` only over `https://`; longer paths first, then
  # older (§5.4, step 2).
  def cookies_for(url : String) : Array(Cookie)
  # §5.1.4: the request path up to its last `/`, or `/`.
  protected def self.default_path(path : String) : String
  # §5.1.3: the host is the domain, or ends with it after a dot, and is
  # not an IP address.
  protected def self.domain_match?(host : String, domain : String) : Bool
  # The `Cookie` header for a request to *url*, nil when nothing goes.
  def header_for(url : String) : String | ::Nil
  def initialize
  # §5.1.4.
  protected def self.path_match?(request : String, cookie : String) : Bool
  # Whether *uri* is over a secure protocol: `https://` (and `wss://`).
  protected def self.secure?(uri : URI) : Bool
  def size : Int32
  # The `Set-Cookie` lines of *response*, from *url*, taken in.
  def update(url : String, response : Response) : Nil
end

Std::Http::CookieStore for CookieJarimpl

std/cookieiyi doc src/std/cookie.iyi
impl Std::Http::CookieStore for CookieJar
end