netpkt/cookie.rs#
Source: lib/netpkt/cookie.rs
cookie -- HTTP cookies as RFC 6265 reads them: what a Set-Cookie header says, what a user agent
stores and what its Cookie header sends. No I/O and no clock: the caller passes the time in, in
seconds since 1970, so a jar on a simulated clock works like one on a real one
SetCookie::parse(text) the header's value (5.2): name, value and the attributes, what
cannot be read left out; None without a name-value pair
parse_date(text) the tolerant date of an Expires attribute (5.1.1)
domain_match(host, domain) 5.1.3; path_match(request, cookie) 5.1.4; default_path(path) 5.1.4
canonical_host(host) lower case, without the dot at the end (5.1.2, no IDNA here)
site_of(host) what "same site" compares: an address itself, else the name's
registrable domain by the public suffix list (netpkt::psl)
Origin who stores: host, path, secure (https), http (false: a script)
Asks who reads: host, path, secure, http, the site that asks (None:
the user), a top-level navigation, a safe method
Cookie a stored cookie: name, value, domain, host_only, path, expires
(None: until the session ends), secure, http_only, same_site
Jar store(text, origin, now) -> Result<(), Refused> (5.3),
header(asks, now) -> String (5.4: longer paths first, then the
older ones; "" for none), cookies() in order of creation,
matching(asks, now) -> the same ones as Cookies, script_text(..),
purge(now), drop_session(), remove(name, domain, path), clear(),
bytes()
From RFC 6265bis, as far as a small browser needs it: SameSite (Strict is sent only within the
site, Lax and no attribute also on a top-level navigation with a safe method, None always but
only with Secure), a page without https cannot set Secure nor overwrite a Secure cookie, name and
value together hold at most MAX_COOKIE bytes. A Domain that is a public suffix (netpkt::psl, the
Public Suffix List: "com", "co.uk", "github.io") makes a host-only cookie when it is the host's
own name and is refused otherwise (6265bis 5.6 step 9), so a site cannot set cookies for the
sites beside it. A jar holds at most MAX_PER_DOMAIN cookies of a domain and MAX_COOKIES in all;
beyond that the oldest go.
/// bytes of a cookie's name and value together at most (RFC 6265bis 5.6)
MAX_COOKIE: usize = 4096
/// cookies of a domain at most (RFC 6265 6.1 asks for at least 50)
MAX_PER_DOMAIN: usize = 50
/// cookies of a jar at most
MAX_COOKIES: usize = 500
enum SameSitethe SameSite attributeUnset-- none given (reads as Lax)StrictLaxNone
impl SameSite
fn name(self) -> &'static stras a header writes it ("" for none)struct SetCookiewhat a Set-Cookie header says (see the module header)name: Stringvalue: Stringdomain: Option<String>-- in lower case without a leading dotpath: Option<String>-- only one that starts with "/"expires: Option<i64>-- seconds since 1970max_age: Option<i64>-- seconds from now; <= 0 means at oncesecure: boolhttp_only: boolsame_site: SameSite
impl SetCookie
fn parse(text: &str) -> Option<SetCookie>a header's value (RFC 6265 5.2); None: no "name=value" with a name (the user agent ignores the whole header)fn parse_date(text: &str) -> Option<i64>the date of an Expires attribute (RFC 6265 5.1.1) -> seconds since 1970; None: not onefn canonical_host(host: &str) -> Stringa host name in lower case, without the dot at the endfn domain_match(host: &str, domain: &str) -> boola host matches a domain (RFC 6265 5.1.3): the same, or a name that ends in ".domain"; an address matches only itselffn path_match(path: &str, cookie: &str) -> boola request's path matches a cookie's (RFC 6265 5.1.4)fn default_path(path: &str) -> Stringthe path a cookie without one gets from its request's (RFC 6265 5.1.4)fn site_of(host: &str) -> Stringwhat "same site" compares: an address itself, else the name's registrable domain (the public suffix and one label: "example.co.uk"); a public suffix itself is its own sitestruct Cookiea stored cookiename: Stringvalue: Stringdomain: Stringhost_only: bool-- only the very host gets it (no Domain attribute)path: Stringexpires: Option<i64>-- seconds since 1970; None: until the session endssecure: boolhttp_only: boolsame_site: SameSiteorder: u64-- its place among the cookies: the older first (5.4), the oldest go first
impl Cookie
fn expired(&self, now: i64) -> boolstruct Origin<'a>who stores a cookiehost: &'a strpath: &'a strsecure: bool-- the request went over httpshttp: bool-- a Set-Cookie header; false: a script of the page
struct Asks<'a>who reads the cookieshost: &'a strpath: &'a strsecure: boolhttp: bool-- a request; false: a script of the pagesite: Option<&'a str>-- the site of the page that asks (None: the user, by the address line or a reload)top: bool-- a navigation of the window (a link, a form); false: an image, a sheet, a script's requestsafe: bool-- GET or HEAD
enum Refusedwhy a jar does not take a cookieSyntax-- no "name=value" with a nameTooLarge-- name and value hold more than MAX_COOKIE bytesDomain-- the Domain does not fit the host, or is a public suffixHttpOnly-- a script may not set HttpOnly nor overwrite such a cookieSecure-- Secure from a page without https, or overwriting a Secure cookie from thereSameSiteNone-- SameSite=None without Secure
struct Jarthe cookies of a browser (see the module header)
impl Jar
fn new() -> Jarfn is_empty(&self) -> boolfn bytes(&self) -> usizethe bytes its texts hold (an estimate for a budget)fn store(&mut self, text: &str, o: Origin, now: i64) -> Result<(), Refused>a Set-Cookie value (RFC 6265 5.3) at timenowfn store_parsed(&mut self, c: SetCookie, o: Origin, now: i64) -> Result<(), Refused>the same with what the header says, read alreadyfn matching(&self, a: &Asks, now: i64) -> Vec<&Cookie>the cookies of a request or a script (RFC 6265 5.4): longer paths first, then the olderfn header(&self, a: &Asks, now: i64) -> Stringthe value of a Cookie header ("" for none)fn script_text(&self, host: &str, path: &str, secure: bool, now: i64) -> Stringwhat a script of the page at this address reads (no HttpOnly cookies)fn purge(&mut self, now: i64)the expired ones gofn drop_session(&mut self)the browser ends: what lasts only for the session goesfn remove(&mut self, name: &str, domain: &str, path: &str) -> boolone cookie goes -> there was onefn clear(&mut self)