Wt  4.10.4
Public Types | Public Member Functions | List of all members
Wt::Http::Cookie Class Reference

An HTTP Cookie. More...

#include <Wt/Http/Cookie.h>

Public Types

enum class  SameSite { None , Lax , Strict }
 Enumeration of SameSite attribute values. More...
 

Public Member Functions

 Cookie (const std::string &name)
 Constructor for cookie without value. More...
 
 Cookie (const std::string &name, const std::string &value, const Wt::WDateTime &expires)
 Constructor for a cookie that expires at a certain datetime. More...
 
 Cookie (const std::string &name, const std::string &value, const std::chrono::seconds &maxAge)
 Constructor for a cookie that expires after certain duration. More...
 
const std::string & name () const
 The cookie name.
 
void setValue (const std::string &value)
 Sets the cookie value. More...
 
const std::string & value () const
 The cookie value. More...
 
void setExpires (const Wt::WDateTime &expires)
 Sets when the cookie will expire. More...
 
const Wt::WDateTimeexpires () const
 The cookie expiration. More...
 
void setMaxAge (const std::chrono::seconds &maxAge)
 Sets the cookie's Max-Age. More...
 
const std::chrono::seconds & maxAge () const
 The cookie's Max-Age. More...
 
void setDomain (const std::string &domain)
 Sets the cookie domain. More...
 
const std::string & domain () const
 The cookie domain. More...
 
void setPath (const std::string &path)
 Sets the cookie path. More...
 
const std::string & path () const
 The cookie path. More...
 
void setSecure (bool secure)
 Sets if the cookie must be sent over a secure connection. More...
 
bool secure () const
 Returns if the cookie must be sent over secure connection. More...
 
void setHttpOnly (bool httpOnly)
 Sets if the cookie is only accessible through HTTP. More...
 
bool httpOnly () const
 Returns if the cookie is only accessible through HTTP. More...
 
void setSameSite (SameSite sameSite)
 Sets the cookie SameSite attribute. More...
 
SameSite sameSite () const
 The cookie SameSite attribute. More...
 

Detailed Description

An HTTP Cookie.

Cookies can be set with WApplication::setCookie(const Http::Cookie&) and removed with WApplication::removeCookie(const Http::Cookie&).

See also
WApplication::setCookie(const Http::Cookie&), WApplication::removeCookie(const Http::Cookie&), WEnvironment::supportsCookies(), WEnvironment::getCookie()

Member Enumeration Documentation

◆ SameSite

Enumeration of SameSite attribute values.

Enumerator
None 

Sent in all contexts (note: generally not allowed if secure not specified)

Lax 

Sent for first-party and top-level GET from third-party.

Strict 

Sent for first-party only.

Constructor & Destructor Documentation

◆ Cookie() [1/3]

Wt::Http::Cookie::Cookie ( const std::string &  name)
explicit

Constructor for cookie without value.

Allows you to only specify the name of the cookie. This can be useful if the intention is to pass it to WApplication::removeCookie(), in which case no value is needed.

See also
WApplication::removeCookie(const Http::Cookie&)

◆ Cookie() [2/3]

Wt::Http::Cookie::Cookie ( const std::string &  name,
const std::string &  value,
const Wt::WDateTime expires 
)

Constructor for a cookie that expires at a certain datetime.

The name must be a valid cookie name (of type 'token': no special characters or separators, see RFC2616 page 16). The value may be anything.

See also
WApplication::setCookie(const Http::Cookie&)

◆ Cookie() [3/3]

Wt::Http::Cookie::Cookie ( const std::string &  name,
const std::string &  value,
const std::chrono::seconds &  maxAge 
)

Constructor for a cookie that expires after certain duration.

The name must be a valid cookie name (of type 'token': no special characters or separators, see RFC2616 page 16). The value may be anything.

See also
WApplication::setCookie(const Http::Cookie&)

Member Function Documentation

◆ domain()

const std::string& Wt::Http::Cookie::domain ( ) const

The cookie domain.

See also
setDomain()

◆ expires()

const Wt::WDateTime& Wt::Http::Cookie::expires ( ) const

The cookie expiration.

See also
setExpires(), setMaxAge()

◆ httpOnly()

bool Wt::Http::Cookie::httpOnly ( ) const

Returns if the cookie is only accessible through HTTP.

See also
setHttpOnly()

◆ maxAge()

const std::chrono::seconds& Wt::Http::Cookie::maxAge ( ) const

The cookie's Max-Age.

See also
setMaxAge(), setExpires()

◆ path()

const std::string& Wt::Http::Cookie::path ( ) const

The cookie path.

See also
setPath()

◆ sameSite()

SameSite Wt::Http::Cookie::sameSite ( ) const

The cookie SameSite attribute.

See also
setSameSite()

◆ secure()

bool Wt::Http::Cookie::secure ( ) const

Returns if the cookie must be sent over secure connection.

See also
setSecure()

◆ setDomain()

void Wt::Http::Cookie::setDomain ( const std::string &  domain)

Sets the cookie domain.

By default the Domain attribute is omitted, which the browser interprets as the current domain (not including subdomains).

See also
domain()

◆ setExpires()

void Wt::Http::Cookie::setExpires ( const Wt::WDateTime expires)

Sets when the cookie will expire.

If WDateTime::isNull(), it will be a session cookie (which expires when the browser is closed).

See also
expires(), setMaxAge()

◆ setHttpOnly()

void Wt::Http::Cookie::setHttpOnly ( bool  httpOnly)

Sets if the cookie is only accessible through HTTP.

The default value is true.

See also
httpOnly()

◆ setMaxAge()

void Wt::Http::Cookie::setMaxAge ( const std::chrono::seconds &  maxAge)

Sets the cookie's Max-Age.

The number of seconds until the cookie expires. Note that if both Expires and Max-Age are set, maxAge has precedence. When the duration is negative, Max-Age will not be set.

See also
maxAge(), setExpires()

◆ setPath()

void Wt::Http::Cookie::setPath ( const std::string &  path)

Sets the cookie path.

By default the cookie only applies to the application deployment path (WEnvironment::deploymentPath()).

See also
path()

◆ setSameSite()

void Wt::Http::Cookie::setSameSite ( SameSite  sameSite)

Sets the cookie SameSite attribute.

The default value is SameSite::Lax.

Note
A browser will reject a cookie with SameSite::None if secure() is false.
See also
sameSite()

◆ setSecure()

void Wt::Http::Cookie::setSecure ( bool  secure)

Sets if the cookie must be sent over a secure connection.

The default value is false.

See also
secure()

◆ setValue()

void Wt::Http::Cookie::setValue ( const std::string &  value)

Sets the cookie value.

See also
value()

◆ value()

const std::string& Wt::Http::Cookie::value ( ) const

The cookie value.

See also
setValue()