pub struct SetCookie {
pub cookie: Cookie,
pub max_age: Option<u64>,
pub domain: Option<String>,
pub path: Option<String>,
pub secure: bool,
}
Expand description
Represents a Set-Cookie header. Has more information than a normal Cookie (e.g. max-age, domain, path, secure).
Fields§
Base Cookie
max_age: Option<u64>
Cookie Max-Age. Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately.
domain: Option<String>
Cookie Domain
path: Option<String>
Cookie Path where the cookie is valid
secure: bool
Cookie is secure
Implementations§
source§impl SetCookie
impl SetCookie
sourcepub fn new(name: impl AsRef<str>, value: impl AsRef<str>) -> SetCookie
pub fn new(name: impl AsRef<str>, value: impl AsRef<str>) -> SetCookie
Make a new SetCookie from a name and a value.
Example
use afire::SetCookie;
let cookie = SetCookie::new("name", "value");
Examples found in repository?
examples/basic/cookie.rs (lines 43-46)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new([127, 0, 0, 1], 8080);
// Define a route to show request cookies as a table
server.route(Method::GET, "/", |req| {
// Return all cookies in a *messy* html table
let mut html = String::new();
html.push_str("<style>table, th, td {border:1px solid black;}</style>");
html.push_str("<table>");
html.push_str("<tr><th>Name</th><th>Value</th></tr>");
for cookie in &*req.cookies {
html.push_str("<tr><td>");
html.push_str(&cookie.name);
html.push_str("</td><td>");
html.push_str(&cookie.value);
html.push_str("</td></tr>");
}
html.push_str("</table>");
Response::new().text(html).content(Content::HTML)
});
// Set a cookie defined in the Query
server.route(Method::GET, "/set", |req| {
// Create a new cookie
let cookie = SetCookie::new(
req.query.get("name").unwrap_or("test"),
req.query.get("value").unwrap_or("test"),
)
// Set some options
.max_age(60 * 60)
.path("/");
let body = format!(
"Set Cookie '{}' to '{}'",
cookie.cookie.name, cookie.cookie.value
);
// Set the cookie
Response::new()
.text(body)
.content(Content::HTML)
.cookie(cookie)
});
// Now goto http://localhost:8080/set?name=hello&value=world
// Then goto http://localhost:8080/ and you should see a table with the cookie
// Start the server in single threaded mode
// This will block the current thread
server.start().unwrap();
}
sourcepub fn max_age(self, max_age: u64) -> SetCookie
pub fn max_age(self, max_age: u64) -> SetCookie
Set the Max-Age field of a SetCookie. This is the number of seconds the cookie should be valid for.
Example
let mut cookie = SetCookie::new("name", "value")
.max_age(10 * 60);
assert_eq!(cookie.max_age, Some(10*60));
Examples found in repository?
examples/basic/cookie.rs (line 48)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new([127, 0, 0, 1], 8080);
// Define a route to show request cookies as a table
server.route(Method::GET, "/", |req| {
// Return all cookies in a *messy* html table
let mut html = String::new();
html.push_str("<style>table, th, td {border:1px solid black;}</style>");
html.push_str("<table>");
html.push_str("<tr><th>Name</th><th>Value</th></tr>");
for cookie in &*req.cookies {
html.push_str("<tr><td>");
html.push_str(&cookie.name);
html.push_str("</td><td>");
html.push_str(&cookie.value);
html.push_str("</td></tr>");
}
html.push_str("</table>");
Response::new().text(html).content(Content::HTML)
});
// Set a cookie defined in the Query
server.route(Method::GET, "/set", |req| {
// Create a new cookie
let cookie = SetCookie::new(
req.query.get("name").unwrap_or("test"),
req.query.get("value").unwrap_or("test"),
)
// Set some options
.max_age(60 * 60)
.path("/");
let body = format!(
"Set Cookie '{}' to '{}'",
cookie.cookie.name, cookie.cookie.value
);
// Set the cookie
Response::new()
.text(body)
.content(Content::HTML)
.cookie(cookie)
});
// Now goto http://localhost:8080/set?name=hello&value=world
// Then goto http://localhost:8080/ and you should see a table with the cookie
// Start the server in single threaded mode
// This will block the current thread
server.start().unwrap();
}
sourcepub fn domain(self, domain: impl AsRef<str>) -> SetCookie
pub fn domain(self, domain: impl AsRef<str>) -> SetCookie
Set the Domain field of a SetCookie.
Example
let mut cookie = SetCookie::new("name", "value")
.domain("domain");
assert_eq!(cookie.domain, Some("domain".to_string()));
sourcepub fn path(self, path: impl AsRef<str>) -> SetCookie
pub fn path(self, path: impl AsRef<str>) -> SetCookie
Set the Path field of a SetCookie.
Example
let mut cookie = SetCookie::new("name", "value")
.path("path");
assert_eq!(cookie.path, Some("path".to_string()));
Examples found in repository?
examples/basic/cookie.rs (line 49)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new([127, 0, 0, 1], 8080);
// Define a route to show request cookies as a table
server.route(Method::GET, "/", |req| {
// Return all cookies in a *messy* html table
let mut html = String::new();
html.push_str("<style>table, th, td {border:1px solid black;}</style>");
html.push_str("<table>");
html.push_str("<tr><th>Name</th><th>Value</th></tr>");
for cookie in &*req.cookies {
html.push_str("<tr><td>");
html.push_str(&cookie.name);
html.push_str("</td><td>");
html.push_str(&cookie.value);
html.push_str("</td></tr>");
}
html.push_str("</table>");
Response::new().text(html).content(Content::HTML)
});
// Set a cookie defined in the Query
server.route(Method::GET, "/set", |req| {
// Create a new cookie
let cookie = SetCookie::new(
req.query.get("name").unwrap_or("test"),
req.query.get("value").unwrap_or("test"),
)
// Set some options
.max_age(60 * 60)
.path("/");
let body = format!(
"Set Cookie '{}' to '{}'",
cookie.cookie.name, cookie.cookie.value
);
// Set the cookie
Response::new()
.text(body)
.content(Content::HTML)
.cookie(cookie)
});
// Now goto http://localhost:8080/set?name=hello&value=world
// Then goto http://localhost:8080/ and you should see a table with the cookie
// Start the server in single threaded mode
// This will block the current thread
server.start().unwrap();
}
Trait Implementations§
source§impl PartialEq<SetCookie> for SetCookie
impl PartialEq<SetCookie> for SetCookie
impl Eq for SetCookie
impl StructuralEq for SetCookie
impl StructuralPartialEq for SetCookie
Auto Trait Implementations§
impl RefUnwindSafe for SetCookie
impl Send for SetCookie
impl Sync for SetCookie
impl Unpin for SetCookie
impl UnwindSafe for SetCookie
Blanket Implementations§
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
Mutably borrows from an owned value. Read more