Struct afire::SetCookie

source ·
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§

§cookie: Cookie

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

source

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();
    }
source

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();
    }
source

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()));
source

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();
    }
source

pub fn secure(self, secure: bool) -> SetCookie

Set the Secure field of a SetCookie.

Example
let mut cookie = SetCookie::new("name", "value")
    .secure(true);

assert_eq!(cookie.secure, true);

Trait Implementations§

source§

impl Clone for SetCookie

source§

fn clone(&self) -> SetCookie

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SetCookie

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for SetCookie

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for SetCookie

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<SetCookie> for SetCookie

source§

fn eq(&self, other: &SetCookie) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for SetCookie

source§

impl StructuralEq for SetCookie

source§

impl StructuralPartialEq for SetCookie

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.