pub struct CookieBuilder {
pub name: String,
pub value: String,
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: StringCookie name identifier.
value: StringCookie 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 new<N, V>(name: N, value: V) -> CookieBuilder
pub fn new<N, V>(name: N, value: V) -> CookieBuilder
Sourcepub fn parse<C>(cookie: C) -> CookieBuilder
pub fn parse<C>(cookie: C) -> CookieBuilder
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 CookieBuilder
pub fn set_expires<E>(&mut self, expires: E) -> &mut CookieBuilder
Sourcepub fn set_max_age<M>(&mut self, max_age: M) -> &mut CookieBuilder
pub fn set_max_age<M>(&mut self, max_age: M) -> &mut CookieBuilder
Sourcepub fn set_domain<D>(&mut self, domain: D) -> &mut CookieBuilder
pub fn set_domain<D>(&mut self, domain: D) -> &mut CookieBuilder
Sourcepub fn set_path<T>(&mut self, path: T) -> &mut CookieBuilder
pub fn set_path<T>(&mut self, path: T) -> &mut CookieBuilder
Sourcepub fn secure(&mut self) -> &mut CookieBuilder
pub fn secure(&mut self) -> &mut CookieBuilder
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 CookieBuilder
pub fn http_only(&mut self) -> &mut CookieBuilder
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 CookieBuilder
pub fn disable_secure(&mut self) -> &mut CookieBuilder
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 CookieBuilder
pub fn disable_http_only(&mut self) -> &mut CookieBuilder
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 CookieBuilder
pub fn set_same_site<T>(&mut self, same_site: T) -> &mut CookieBuilder
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.
Source§impl CookieBuilder
impl CookieBuilder
pub fn set_name(&mut self, val: impl AsRef<str>) -> &mut CookieBuilder
pub fn get_name(&self) -> &String
pub fn get_mut_name(&mut self) -> &mut String
pub fn set_value(&mut self, val: impl AsRef<str>) -> &mut CookieBuilder
pub fn get_value(&self) -> &String
pub fn get_mut_value(&mut self) -> &mut String
pub fn get_expires(&self) -> String
pub fn try_get_expires(&self) -> &Option<String>
pub fn get_mut_expires(&mut self) -> &mut Option<String>
pub fn get_max_age(&self) -> Option<i64>
pub fn try_get_max_age(&self) -> Option<i64>
pub fn get_mut_max_age(&mut self) -> &mut Option<i64>
pub fn get_domain(&self) -> String
pub fn try_get_domain(&self) -> &Option<String>
pub fn get_mut_domain(&mut self) -> &mut Option<String>
pub fn get_path(&self) -> String
pub fn try_get_path(&self) -> &Option<String>
pub fn get_mut_path(&mut self) -> &mut Option<String>
pub fn get_secure(&self) -> Option<bool>
pub fn try_get_secure(&self) -> Option<bool>
pub fn get_mut_secure(&mut self) -> &mut Option<bool>
pub fn get_http_only(&self) -> Option<bool>
pub fn try_get_http_only(&self) -> Option<bool>
pub fn get_mut_http_only(&mut self) -> &mut Option<bool>
pub fn get_same_site(&self) -> String
pub fn try_get_same_site(&self) -> &Option<String>
pub fn get_mut_same_site(&mut self) -> &mut Option<String>
Trait Implementations§
Source§impl Clone for CookieBuilder
impl Clone for CookieBuilder
Source§fn clone(&self) -> CookieBuilder
fn clone(&self) -> CookieBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CookieBuilder
impl Debug for CookieBuilder
Source§impl Default for CookieBuilder
impl Default for CookieBuilder
Source§fn default() -> CookieBuilder
fn default() -> CookieBuilder
Source§impl<'de> Deserialize<'de> for CookieBuilder
impl<'de> Deserialize<'de> for CookieBuilder
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<CookieBuilder, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<CookieBuilder, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
impl Eq for CookieBuilder
Source§impl PartialEq for CookieBuilder
impl PartialEq for CookieBuilder
Source§impl Serialize for CookieBuilder
impl Serialize for CookieBuilder
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for CookieBuilder
Auto Trait Implementations§
impl Freeze for CookieBuilder
impl RefUnwindSafe for CookieBuilder
impl Send for CookieBuilder
impl Sync for CookieBuilder
impl Unpin for CookieBuilder
impl UnsafeUnpin for CookieBuilder
impl UnwindSafe for CookieBuilder
Blanket Implementations§
impl<T> AnySend for T
impl<T> AnySendClone for T
impl<T> AnySendSync for T
impl<T> AnySendSyncClone for T
impl<T> AnySync for T
impl<T> AnySyncClone for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.