JFLib

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 SameSite the SameSite attribute
    • Unset -- none given (reads as Lax)
    • Strict
    • Lax
    • None

impl SameSite

  • fn name(self) -> &'static str as a header writes it ("" for none)

  • struct SetCookie what a Set-Cookie header says (see the module header)

    • name: String
    • value: String
    • domain: Option<String> -- in lower case without a leading dot
    • path: Option<String> -- only one that starts with "/"
    • expires: Option<i64> -- seconds since 1970
    • max_age: Option<i64> -- seconds from now; <= 0 means at once
    • secure: bool
    • http_only: bool
    • same_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 one

  • fn canonical_host(host: &str) -> String a host name in lower case, without the dot at the end

  • fn domain_match(host: &str, domain: &str) -> bool a host matches a domain (RFC 6265 5.1.3): the same, or a name that ends in ".domain"; an address matches only itself

  • fn path_match(path: &str, cookie: &str) -> bool a request's path matches a cookie's (RFC 6265 5.1.4)

  • fn default_path(path: &str) -> String the path a cookie without one gets from its request's (RFC 6265 5.1.4)

  • fn site_of(host: &str) -> String what "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 site

  • struct Cookie a stored cookie

    • name: String
    • value: String
    • domain: String
    • host_only: bool -- only the very host gets it (no Domain attribute)
    • path: String
    • expires: Option<i64> -- seconds since 1970; None: until the session ends
    • secure: bool
    • http_only: bool
    • same_site: SameSite
    • order: u64 -- its place among the cookies: the older first (5.4), the oldest go first

impl Cookie

  • fn expired(&self, now: i64) -> bool

  • struct Origin<'a> who stores a cookie

    • host: &'a str
    • path: &'a str
    • secure: bool -- the request went over https
    • http: bool -- a Set-Cookie header; false: a script of the page
  • struct Asks<'a> who reads the cookies

    • host: &'a str
    • path: &'a str
    • secure: bool
    • http: bool -- a request; false: a script of the page
    • site: 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 request
    • safe: bool -- GET or HEAD
  • enum Refused why a jar does not take a cookie

    • Syntax -- no "name=value" with a name
    • TooLarge -- name and value hold more than MAX_COOKIE bytes
    • Domain -- the Domain does not fit the host, or is a public suffix
    • HttpOnly -- a script may not set HttpOnly nor overwrite such a cookie
    • Secure -- Secure from a page without https, or overwriting a Secure cookie from there
    • SameSiteNone -- SameSite=None without Secure
  • struct Jar the cookies of a browser (see the module header)

impl Jar

  • fn new() -> Jar
  • fn cookies(&self) -> &[Cookie] all of them in order of creation, expired ones too until purge
  • fn is_empty(&self) -> bool
  • fn bytes(&self) -> usize the 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 time now
  • fn store_parsed(&mut self, c: SetCookie, o: Origin, now: i64) -> Result<(), Refused> the same with what the header says, read already
  • fn 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 older
  • fn header(&self, a: &Asks, now: i64) -> String the value of a Cookie header ("" for none)
  • fn script_text(&self, host: &str, path: &str, secure: bool, now: i64) -> String what a script of the page at this address reads (no HttpOnly cookies)
  • fn purge(&mut self, now: i64) the expired ones go
  • fn drop_session(&mut self) the browser ends: what lasts only for the session goes
  • fn remove(&mut self, name: &str, domain: &str, path: &str) -> bool one cookie goes -> there was one
  • fn clear(&mut self)

Source: docs/API.md in the JFLib repository.