std/cookie
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.
Cookieclass
A cookie (RFC 6265): what a Set-Cookie line says, and what a jar keeps. Cookie.parse(line) reads a Set-Cookie value as RFC 6265 §5.2 has a user agent read one - a name and value, then attributes in any case, an unknown attribute ignored - and answers nil for a line that has no name-value pair. to_set_cookie writes one, refusing a name or value that would break the line. expires is the instant it lapses, from Max-Age before Expires (§5.3, step 3); nil is a session cookie.
pub class Cookie
def domain : String | ::Nil
# Whether the cookie has lapsed at *now*.
def expired?(now : Time) : Bool
def expires : Time | ::Nil
# The cookies an answer's `Set-Cookie` lines set, a line each.
def self.from_response(response : Response) : Array(Cookie)
def http_only : Bool
def initialize(name : String, value : String, path : String | ::Nil = nil, domain : String | ::Nil = nil, expires : Time | ::Nil = nil, max_age : Int64 | ::Nil = nil, secure : Bool = false, http_only : Bool = false, same_site : String | ::Nil = nil)
def max_age : Int64 | ::Nil
def name : String
def self.parse(line : String) : Cookie | ::Nil
# The pairs of a request's `Cookie` header: `a=1; b=2`.
def self.parse_header(value : String) : Hash(String, String)
def path : String | ::Nil
def same_site : String | ::Nil
# `Max-Age`'s digits, with a sign (§5.2.2); nil for anything else.
protected def self.seconds(text : String) : Int64 | ::Nil
def secure : Bool
# The `Set-Cookie` value a server writes (§4.1). A name is a token, and a
# value the cookie-octets: a space, a comma, a semicolon, a backslash or
# a quote inside one would end it or another field early.
def to_set_cookie : String
def value : String
endCookieJarclass
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.
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
endStd::Http::CookieStore for CookieJarimpl
impl Std::Http::CookieStore for CookieJar
end
Cookies (RFC 6265), for both ends of std/http:
Cookiereads aSet-Cookieline as a user agent reads one and writes one as a server writes one, andCookieJaris the client's store, handed toHTTP.request(..., jar: jar)(it is std/http'sCookieStore).