Expand description
HTTP Cookie implementation in Rust
Implementation of the RFC 6265 Set-Cookie.
After receiving a HTTP response, a server cand send one or more Set-Cookie
headers. The user agent usually stores these
cookies and includes some of them (those which match cookie criteria) inside a Cookie
header.
Tutorial
Client-side
Receiving Set-Cookie
headers at HTTP responses
Servers may incluide one or more Set-Cookie
headers at HTTP responses, for example:
Set-Cookie: id=1213342
Set-Cookie: user=john@smith; Expires=Thu, 31 Oct 2021 07:28:00 GMT; Secure
The type SetCookie represents a server cookie. To parse a Set-Cookie
header value it has a from_string
function:
use wcookie::SetCookie;
use std::str::FromStr;
let cookie_value: &str = "user=john@smith; Expires=Thu, 31 Oct 2021 07:28:00 GMT; Secure";
let cookie = SetCookie::from_str(cookie_value);
assert!(cookie.is_ok());
Sending Cookie
headers in HTTP requests
The user agent can check if the cookie can be used in a request with function SetCookie::use_in_request passing as params:
domain
: request target domainpath
: request pathsecure
: if HTTPS is used
use wcookie::SetCookie;
use std::str::FromStr;
let cookie = SetCookie::from_str("cookie1=122343; Domain=b.a").unwrap();
assert!(cookie.use_in_request("c.b.a", "/", true));
Note, if the Set-Cookie
value does not include the Domain
directive, it should be set the request’s host domain:
use wcookie::SetCookie;
use std::str::FromStr;
let mut cookie = SetCookie::from_str("cookie1=122343").unwrap();
cookie.domain = Some(String::from("b.a"));
assert!(cookie.use_in_request("c.b.a", "/", true));
By default, the cookie path, if not set, is /
.
Note, use_in_request
makes use of next functions:
A SetCookie
can be converted into a Cookie to be incluided in a Cookie
header:
use wcookie::SetCookie;
use std::str::FromStr;
let set_cookie = SetCookie::from_str("cookie1=122343; Max-Age=12000; Domain=b.a").unwrap();
// Check the cookie can be used in request
assert!(set_cookie.use_in_request("c.b.a", "/", true));
let cookie = set_cookie.to_cookie();
assert_eq!(cookie.name.as_str(), "cookie1");
assert_eq!(cookie.value.as_str(), "122343");
assert_eq!(cookie.to_string(), "cookie1=122343");
Server-side: creating Set-Cookie
At server side, a cookie can be created using the new
constructor and member values can be set when it is mutable:
use wcookie::SetCookie;
use chrono::{Utc, TimeZone};
let mut cookie = SetCookie::new("cookie_name", "cookie_value");
cookie.domain = Some(String::from("myserver.com"));
cookie.expires = Some(Utc.ymd(2024, 7, 8).and_hms(9, 10, 11));
Structs
- Represents a cookie sent at a
Cookie
header at an HTTP Request. - Error type produced while parsing a
Cookie
- Represents a cookie created from
Set-Cookie
response header.
Enums
- Enum with
SameSite
possible values forSet-Cookie
attribute
Functions
- Parses Asct dates, as defined in RFC2616 Section 3.3.1.
- Parses RFC 850 dates, with extension, as defined in RFC2616 Section 3.3.1.
- Parses RFC 1123 dates, as defined in RFC2616 Section 3.3.1.