Sign in

    gaato/http/cookie does not have a README file

    pub struct Cookie {
    name : String
    value : String
    domain : String
    host_only : Bool
    path : String
    expires_ms : Int64?
    secure : Bool
    http_only : Bool
    same_site : SameSite?
    creation_ms : Int64
    last_access_ms : Int64
    } derive(Eq,
    Debug
    )

    A stored cookie, with the fields of the RFC 6265 §5.3 storage model.

    domain is the request host for a host-only cookie and the Domain attribute otherwise. expires_ms is None for a session cookie. Times are Unix milliseconds from the jar's clock; last_access_ms is when the cookie was last stored or sent.

    Cookie::equal

    fn Cookie::equal(Cookie, Cookie) -> Bool

    Compares every field.

    Cookie::is_expired

    fn Cookie::is_expired(self : Cookie, now_ms : Int64) -> Bool

    Whether the cookie has expired at now_ms.

    Cookie::not_equal

    fn Cookie::not_equal(x : Cookie, y : Cookie) -> Bool

    Compares every field.

    Cookie::to_repr

    Debug representation of every field.

    CookieJar

    pub struct CookieJar {
    // private fields
    }

    An RFC 6265 cookie store for an HTTP client: store takes the Set-Cookie headers of a response, header_for builds the Cookie header of a request.

    It is a client's jar, not a browser's: HttpOnly cookies are sent like any other, and SameSite is kept but not enforced, since a client has no notion of a cross-site request. Without the Public Suffix List it rejects only a Domain of a single label (com, localhost); pass is_public_suffix to reject more. An IP-address host only gets host-only cookies. Hosts must arrive in ASCII form, so a Domain with other characters is ignored.

    test {
    let jar = @cookie.CookieJar::new(@mock.FakeClock::new())
    jar.store(
    @http.Headers::from_array([("Set-Cookie", "sid=1; Path=/; Secure")]),
    "https://example.com/login",
    )
    assert_eq(jar.header_for("https://example.com/"), Some("sid=1"))
    assert_eq(jar.header_for("http://example.com/"), None)
    }

    CookieJar::attach

    Returns request with the jar's Cookie header for its URL added, or request itself when no cookie matches or it already carries a Cookie header, which is left exactly as the caller set it. The headers are copied, so request is not changed.

    Middleware does not wrap Transport::send_stream; a streaming caller sends jar.attach(request) and then calls jar.store(head.headers, request.url) with the ResponseHead it gets.

    CookieJar::clear

    fn CookieJar::clear(self : CookieJar) -> Unit

    Removes every cookie.

    CookieJar::clear_domain

    fn CookieJar::clear_domain(self : CookieJar, domain : String) -> Unit

    Removes the cookies whose domain is domain or one of its subdomains.

    CookieJar::clear_session

    fn CookieJar::clear_session(self : CookieJar) -> Unit

    Removes the session cookies (those without Expires or Max-Age), as RFC 6265 §5.3 does when a session ends.

    CookieJar::header_for

    fn CookieJar::header_for(self : CookieJar, request_url : String) -> String?

    Returns the Cookie header value for a request to request_url, or None when no cookie matches, and marks the cookies sent as accessed. Longer paths come first, then earlier-created cookies (RFC 6265 §5.4).

    CookieJar::iter

    fn CookieJar::iter(self : CookieJar) -> Iter[Cookie]

    Iterates over a snapshot of the stored cookies, grouped by domain. Expired cookies not yet evicted are included; see remove_expired.

    CookieJar::length

    fn CookieJar::length(self : CookieJar) -> Int

    The number of cookies stored, expired ones not yet evicted included.

    CookieJar::new

    fn CookieJar::new(clock : &
    Clock
    , max_per_domain? : Int, max_total? : Int, is_public_suffix? : (String) -> Bool) -> CookieJar

    Creates an empty jar that reads the time from clock.

    max_per_domain (default 50) and max_total (default 3000) bound how many cookies it keeps; a cookie past either evicts expired cookies first, then the least recently accessed (RFC 6265 §5.3). Negative limits count as 0. is_public_suffix names further domains a cookie may not be set for, such as a Public Suffix List lookup; single-label domains are always refused.

    CookieJar::remove_expired

    fn CookieJar::remove_expired(self : CookieJar) -> Unit

    Removes the cookies that have expired by the clock's current time.

    CookieJar::store

    fn CookieJar::store(self : CookieJar, headers :
    Headers
    , request_url : String) -> Unit

    Stores every Set-Cookie of a response to request_url, in order. Values the RFC 6265 algorithms ignore, and every value when the URL has no host, are skipped.

    SameSite

    pub(all) enum SameSite {
    Strict
    Lax
    NoRestriction
    } derive(Eq,
    Debug
    )

    A cookie's SameSite attribute (RFC 6265bis). NoRestriction is SameSite=None.

    SameSite::equal

    fn SameSite::equal(SameSite, SameSite) -> Bool

    Compares attribute values.

    SameSite::not_equal

    fn SameSite::not_equal(x : SameSite, y : SameSite) -> Bool

    Compares attribute values.

    SameSite::to_repr

    Debug representation of the attribute value.

    cookies

    A middleware step that sends jar's cookies with each request (see CookieJar::attach) and stores the Set-Cookie headers of its response. Nothing is stored when the request fails.

    Place it inside the redirect step, between @http.redirects and the transport: @http.send_with(transport, [@http.redirects(),@cookie.cookies(jar)], request). The redirect step then sends every hop through it, so each hop's Set-Cookie is stored before the next hop is sent and each hop carries the cookies of its own URL. Placed outside, it would see only the first URL and the final response: cookies set by an intermediate response would be lost, and the first URL's cookies would look like a header the caller set, kept on same-origin hops.

    test {
    let jar = @cookie.CookieJar::new(@mock.FakeClock::new())
    let _ : @http.Middleware = @cookie.cookies(jar)
    }