CookieJar

stores cookies and replays them on matching requests.

Create a jar with CookieJar.create, then pass it to Client.get-with-jar and similar functions:

(let-do [jar (CookieJar.create)]
  (match (Client.get-with-jar "https://example.com/" &jar)
    (Result.Success r) (println* (Response.body &r))
    (Result.Error e) (IO.errorln &e))
  ; jar now has cookies; they are sent on the next request automatically
  (match (Client.get-with-jar "https://example.com/page" &jar)
    (Result.Success r) (println* (Response.body &r))
    (Result.Error e) (IO.errorln &e)))

apply-to-headers

defn

(Fn [(Ref CookieJar a), (Ref String b), (Ref (Map String (Array String)) c)] (Map String (Array String)))

                        (apply-to-headers jar url headers)
                    

adds a Cookie header for matching cookies to the headers map. Returns headers unchanged if no cookies match.

clear!

defn

(Fn [(Ref CookieJar a)] ())

                        (clear! jar)
                    

removes all cookies.

defn

(Fn [(Ref CookieJar a), (Ref String b)] (Maybe String))

                        (cookie-header jar url)
                    

builds a Cookie header value for the URL, or Nothing if no cookies match. Cookies are serialized longest path first.

cookies

instantiate

(Fn [(Ref CookieJar a)] (Ref (Array JarCookie) a))

gets the cookies property of a CookieJar.

copy

instantiate

(Fn [(Ref CookieJar a)] CookieJar)

copies a CookieJar.

create

defn

(Fn [] CookieJar)

                        (create)
                    

creates an empty cookie jar.

delete

instantiate

(Fn [CookieJar] ())

deletes a CookieJar. Should usually not be called manually.

init

instantiate

(Fn [(Array JarCookie)] CookieJar)

creates a CookieJar.

matching

defn

(Fn [(Ref CookieJar a), (Ref String b)] (Array Cookie))

                        (matching jar url)
                    

returns cookies matching the given URL by domain, path, security, and expiry, longest path first (RFC 6265 §5.4 step 2). Cookies of equal path length keep their insertion order; the creation time §5.4 asks for as the second sort key is not recorded.

A cookie stored without a Domain attribute is host-only and matches its origin host alone; one stored with a Domain attribute also matches that domain’s subdomains, unless the request host is an IP literal.

prn

instantiate

(Fn [(Ref CookieJar a)] String)

converts a CookieJar to a string.

set-cookies

instantiate

(Fn [CookieJar, (Array JarCookie)] CookieJar)

sets the cookies property of a CookieJar.

set-cookies!

instantiate

(Fn [(Ref CookieJar a), (Array JarCookie)] ())

sets the cookies property of a CookieJar in place.

size

defn

(Fn [(Ref CookieJar a)] Int)

                        (size jar)
                    

returns the number of cookies stored.

store!

defn

(Fn [(Ref CookieJar a), (Ref Cookie b)] ())

                        (store! jar c)
                    

stores a cookie as a domain cookie, replacing any with the same name, domain, and path. The cookie is trusted as given: there is no request origin to check it against, so prefer store-response! for anything that came off the wire.

store-response!

defn

(Fn [(Ref CookieJar a), (Ref Response b), (Ref String c)] ())

                        (store-response! jar resp url)
                    

stores cookies from a response, applying RFC 6265 §5.3.

A cookie with no Domain attribute becomes host-only: it is replayed to the URL’s host and to no other. A Domain attribute the URL’s host does not domain-match is rejected outright. So is one with no embedded dot — the pre-public-suffix-list approximation of §5.3 step 5, which lets through registrable multi-label suffixes such as co.uk — unless it is identical to the host, in which case the cookie is kept host-only, so Domain=localhost from http://localhost/ still works.

str

instantiate

(Fn [(Ref CookieJar a)] String)

converts a CookieJar to a string.

update-cookies

instantiate

(Fn [CookieJar, (Ref (Fn [(Array JarCookie)] (Array JarCookie) a) b)] CookieJar)

updates the cookies property of a CookieJar using a function f.