Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 2.33 KB

cookie.md

File metadata and controls

59 lines (45 loc) · 2.33 KB

cookie

Cookie support for Mana. Following RFC 6265.

Example service: Acorn Web Server Appliance with its routes that use cookies.

Features

  • Create, update and clear cookies
  • Easy access to an incoming request's existing cookies

Usage

Create a cookie with a name and value on the response:

res->cookie(Cookie{"lang", "nb-NO"});

// Or if you want to create a cookie with more options, f.ex. expires, path and domain:
res->cookie(Cookie{"lang", "nb-NO", {"Expires", "Sun, 11 Dec 2016 08:49:37 GMT",
  "Path", "/path", "Domain", "domain.com"}});

Update an existing cookie's value:

res->update_cookie<Cookie>("lang", "en-US");

// Or if you have specified a path and/or domain when creating the cookie:
res->update_cookie<Cookie>("lang", "/path", "domain.com", "en-US");

Clear a cookie:

res->clear_cookie<Cookie>("lang");

// Or if you have specified a path and/or domain when creating the cookie:
res->clear_cookie<Cookie>("lang", "/path", "domain.com");

The cookie library contains the middleware CookieParser that parses a request's cookie header and puts the cookies in a CookieJar. The CookieJar is then added as an attribute to the request, making the cookies available to the developer:

if (req->has_attribute<CookieJar>()) {
  // Get the CookieJar
  auto req_cookies = req->get_attribute<CookieJar>();
  
  { // Print all the request-cookies (name-value pairs)
    const auto& all_cookies = req_cookies->get_cookies();
    for (const auto& c : all_cookies)
      printf("Cookie: %s=%s\n", c.first.c_str(), c.second.c_str());
  }

  // Get the value of a cookie named lang
  const auto& value = req_cookies->cookie_value("lang");
  
  // Do actions based on the value
}

Requirements