3 Cookies for User Agents (Clients)
| import: net/cookie/user_agent | |
| package: rhombus-net-cookie-lib | |
The net/cookie/user_agent library provides cookie support for user agents (i.e., clients).
3.1 Cookie Objects
class | |||||||||||||
|
All times are represented as the number of seconds since the epoch (midnight of January 1, 1970), like the values produced by system.seconds.
property | ||
| ||
function | ||
|
3.2 Cookie Jars
method | ||||
| ||||
| ||||
method | ||||
|
The via_http argument should be true if the cookie was received via an HTTP API. If it is #false, the cookie will be ignored if the its “HTTP only” flag is set or if the cookie is attempting to replace an “HTTP only” cookie already present in the jar.
method | |||||
|
The given headers may be provided either as a map from string keys to bytes values, such as returned in a Response from http.get, or as a list of unparsed header lines as byte strings. The decode function is used just for values in the case that headers is a map, and decode is used for both keys and values in the case that headers is a list of bytes.
> def site = URL.from_string("http://test.example.com/apps/main")
> example_cj.extract_and_save_cookies(
{ "X-Test-Header": #"isThisACookie=no",
"Set-Cookie": #"a=b; Max-Age=2000; Path=/",
"Set-Cookie": #"user=bob; Max-Age=86400; Path=/apps" },
~url: site
)
> example_cj.cookie_header(site)
Bytes.copy(#"user=bob")
> example_cj.extract_and_save_cookies(
[#"X-Ignore-This: thisIsStillNotACookie=yes",
#"Set-Cookie: p=q; Max-Age=2000; Path=/",
#"Set-Cookie: usersMom=alice; Max-Age=86400; Path=/apps"],
~url: site
)
> example_cj.cookie_header(site)
Bytes.copy(#"usersMom=alice; user=bob; p=q")
method | ||||
|
This method should produce its cookies in the order expected according to RFC 6265:
Cookies with longer paths are listed before cookies with shorter paths.
Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times.
If there are multiple cookies in the jar with the same name and different domains or paths, RFC 6265 does not specify which to send. The result listincludes all cookies that match the domain and path of the given URL, in the order specified above.
method | ||||||
|
Cookies with the “Secure” flag will be included in this header if and only if url.scheme is "https", unless removed using the keep or skip function.
See user_agent.CookieJar.extract_and_save_cookies for an example.
context parameter | ||
|
3.3 Parsing Utilities
These parsing utilities are normally not used directly, but they are used internally by user_agent.CookieJar.extract_and_save_cookies.
function | |||||
|
Parses headers in the same way as user_agent.CookieJar.extract_and_save_cookies. but returns a list of cookies.
> user_agent.parse.extract_cookies(
[#"X-Ignore-This: thisIsStillNotACookie=yes",
#"Set-Cookie: p=q; Max-Age=2000; Path=/",
#"Set-Cookie: usersMom=alice; Max-Age=86400; Path=/apps"],
~url: site
)
[
user_agent.Cookie(
String.copy("p"),
String.copy("q"),
~domain: "test.example.com",
~path: String.copy("/"),
~expiration_time: 1764121082,
~creation_time: 1764119082,
~access_time: 1764119082,
~is_persistent: #true,
~is_host_only: #true,
~is_secure_only: #false,
~is_http_only: #false
),
user_agent.Cookie(
String.copy("usersMom"),
String.copy("alice"),
~domain: "test.example.com",
~path: String.copy("/apps"),
~expiration_time: 1764205482,
~creation_time: 1764119082,
~access_time: 1764119082,
~is_persistent: #true,
~is_host_only: #true,
~is_secure_only: #false,
~is_http_only: #false
)
]
function | |||||
|
value | |
| |
| |
value | |
|