HttpCookie.Jar (http_cookie v0.7.0)

View Source

HTTP Cookie Jar

Handles storing cookies from response headers, preparing cookie headers for requests, etc.

Implemented according to RFC6265

Summary

Functions

Removes cookies which expired before the provided time from the jar.

Removes session cookies from the jar.

Formats the cookie for sending in a request header for the provided URL.

Gets all the cookies in the store which match the given request URL.

Creates a new empty cookie jar.

Stores the provided cookie in the jar.

Stores the provided cookies in the jar.

Processes the response header list for the given request URL. Parses set-cookie/set-cookie2 headers and stores valid cookies.

Types

t()

@type t() :: %HttpCookie.Jar{cookies: map(), opts: keyword()}

Functions

clear_expired_cookies(jar, now \\ DateTime.utc_now())

@spec clear_expired_cookies(
  jar :: %HttpCookie.Jar{cookies: term(), opts: term()},
  now :: DateTime.t()
) ::
  %HttpCookie.Jar{cookies: term(), opts: term()}

Removes cookies which expired before the provided time from the jar.

Uses the current time if no time is provided.

clear_session_cookies(jar)

@spec clear_session_cookies(jar :: %HttpCookie.Jar{cookies: term(), opts: term()}) ::
  %HttpCookie.Jar{
    cookies: term(),
    opts: term()
  }

Removes session cookies from the jar.

Cookies which don't have an explicit expiry time set are considered session cookies and they expire when a user session ends.

get_matching_cookies(jar, request_url)

@spec get_matching_cookies(
  jar :: %HttpCookie.Jar{cookies: term(), opts: term()},
  request_url :: URI.t()
) :: {[HttpCookie.t()], %HttpCookie.Jar{cookies: term(), opts: term()}}

Gets all the cookies in the store which match the given request URL.

new(opts \\ [])

@spec new(opts :: keyword()) :: %HttpCookie.Jar{cookies: term(), opts: term()}

Creates a new empty cookie jar.

Options

  • :max_cookies - maximum number of cookies stored, positive integer or :infinity, default: 5_000
  • :max_cookies_per_domain - maximum number of cookies stored per domain, positive integer or :infinity, default: 100
  • :cookie_opts - options passed to HttpCookie.from_cookie_string/3

put_cookie(jar, cookie, opts \\ [])

@spec put_cookie(
  jar :: %HttpCookie.Jar{cookies: term(), opts: term()},
  cookie :: HttpCookie.t(),
  opts :: list()
) :: %HttpCookie.Jar{cookies: term(), opts: term()}

Stores the provided cookie in the jar.

put_cookies(jar, cookies)

@spec put_cookies(
  jar :: %HttpCookie.Jar{cookies: term(), opts: term()},
  cookies :: [HttpCookie.t()]
) ::
  %HttpCookie.Jar{cookies: term(), opts: term()}

Stores the provided cookies in the jar.

put_cookies_from_headers(jar, request_url, headers)

@spec put_cookies_from_headers(jar :: t(), request_url :: URI.t(), headers :: list()) ::
  t()

Processes the response header list for the given request URL. Parses set-cookie/set-cookie2 headers and stores valid cookies.