pub struct CookieBuilder {
pub name: CookieKey,
pub value: CookieValue,
pub expires: Option<String>,
pub max_age: Option<i64>,
pub domain: Option<String>,
pub path: Option<String>,
pub secure: Option<bool>,
pub http_only: Option<bool>,
pub same_site: Option<String>,
}Expand description
Builder for constructing HTTP cookies.
Provides methods to set various cookie attributes like expiration, domain, path, and security flags before building the final cookie string.
Fields§
§name: CookieKeyCookie name identifier.
value: CookieValueCookie value content.
expires: Option<String>Optional expiration date/time string.
max_age: Option<i64>Optional maximum age in seconds.
domain: Option<String>Optional domain scope for the cookie.
path: Option<String>Optional path scope for the cookie.
secure: Option<bool>Optional flag indicating secure (HTTPS-only) transmission.
http_only: Option<bool>Optional flag preventing JavaScript access.
same_site: Option<String>Optional SameSite policy setting.
Implementations§
Source§impl CookieBuilder
Implementation for CookieBuilder.
impl CookieBuilder
Implementation for CookieBuilder.
Sourcepub fn parse<C>(cookie: C) -> Self
pub fn parse<C>(cookie: C) -> Self
Parses a Set-Cookie header string into a CookieBuilder.
This method takes a Set-Cookie header string and extracts the various
attributes of a cookie, populating a CookieBuilder instance.
A name that is not a token and a value that carries a control byte are refused, because RFC 6265 Section 4.1.1 admits neither in a cookie pair, and a name that carries a separator would turn the rest of the name into an attribute. An attribute whose value carries a control byte or a semicolon is skipped rather than kept, for the same reason.
§Arguments
C- TheSet-Cookieheader string to parse, which requires theAsRef<str>bound.
§Returns
A CookieBuilder instance populated with the parsed cookie attributes.
Sourcepub fn set_expires<E>(&mut self, expires: E) -> &mut Self
pub fn set_expires<E>(&mut self, expires: E) -> &mut Self
Sourcepub fn set_max_age<M>(&mut self, max_age: M) -> &mut Self
pub fn set_max_age<M>(&mut self, max_age: M) -> &mut Self
Sourcepub fn set_domain<D>(&mut self, domain: D) -> &mut Self
pub fn set_domain<D>(&mut self, domain: D) -> &mut Self
Sourcepub fn secure(&mut self) -> &mut Self
pub fn secure(&mut self) -> &mut Self
Sets the Secure flag for the cookie.
This flag indicates that the cookie should only be transmitted over secure (HTTPS) connections.
§Returns
The CookieBuilder instance for method chaining.
Sourcepub fn http_only(&mut self) -> &mut Self
pub fn http_only(&mut self) -> &mut Self
Sets the HttpOnly flag for the cookie.
This flag prevents client-side JavaScript from accessing the cookie.
§Returns
The CookieBuilder instance for method chaining.
Sourcepub fn disable_secure(&mut self) -> &mut Self
pub fn disable_secure(&mut self) -> &mut Self
Explicitly disables the Secure flag for the cookie.
This method explicitly sets the Secure flag to false, which may be useful
for overriding default secure settings or for testing purposes.
§Returns
The CookieBuilder instance for method chaining.
Sourcepub fn disable_http_only(&mut self) -> &mut Self
pub fn disable_http_only(&mut self) -> &mut Self
Explicitly disables the HttpOnly flag for the cookie.
This method explicitly sets the HttpOnly flag to false, which may be useful
for overriding default HttpOnly settings or for testing purposes.
§Returns
The CookieBuilder instance for method chaining.
Sourcepub fn set_same_site<T>(&mut self, same_site: T) -> &mut Self
pub fn set_same_site<T>(&mut self, same_site: T) -> &mut Self
Sourcepub fn build(&self) -> String
pub fn build(&self) -> String
Builds the cookie string according to the Set-Cookie header format.
The name, the value and every attribute are checked against the grammar of RFC 6265 Section 4.1.1 first, so a value that carries a control byte cannot end the header line and turn the rest of the cookie into a second header. A cookie that fails the check is not emitted at all, which is safer than emitting a truncated one.
§Returns
String- A formatted cookie string ready to be sent in aSet-Cookieheader.
Sourcepub fn try_build(&self) -> Result<String, CookieError>
pub fn try_build(&self) -> Result<String, CookieError>
Builds the cookie string, reporting why a cookie may not be built.
The name, the value and every attribute are checked against the grammar of RFC 6265 Section 4.1.1. A name that is not a token would end early and turn the rest of it into an attribute, a value that carries a control byte would end the header line, and an attribute that carries a semicolon would start another attribute.
§Returns
Result<String, CookieError>- The formatted cookie string, or the reason the cookie cannot be represented.