Home
HTTP module

Cookie

HTTP cookie value plus its response/header attributes.

#include <icy/http/cookie.h>
class Cookie

Defined in src/http/include/icy/http/cookie.h:39

HTTP cookie value plus its response/header attributes.

A cookie is a small amount of information sent by a Web server to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

This class supports both the Version 0 (by Netscape) and Version 1 (by RFC 2109) cookie specifications. By default, cookies are created using Version 0 to ensure the best interoperability.

List of all members

NameKindOwner
CookiefunctionDeclared here
CookiefunctionDeclared here
CookiefunctionDeclared here
CookiefunctionDeclared here
CookiefunctionDeclared here
~CookiefunctionDeclared here
operator=functionDeclared here
setVersionfunctionDeclared here
getVersionfunctionDeclared here
setNamefunctionDeclared here
getNamefunctionDeclared here
setValuefunctionDeclared here
getValuefunctionDeclared here
setCommentfunctionDeclared here
getCommentfunctionDeclared here
setDomainfunctionDeclared here
getDomainfunctionDeclared here
setPathfunctionDeclared here
getPathfunctionDeclared here
setSecurefunctionDeclared here
getSecurefunctionDeclared here
setMaxAgefunctionDeclared here
getMaxAgefunctionDeclared here
setHttpOnlyfunctionDeclared here
getHttpOnlyfunctionDeclared here
toStringfunctionDeclared here
escapefunctionDeclared here
unescapefunctionDeclared here
_versionvariableDeclared here
_namevariableDeclared here
_valuevariableDeclared here
_commentvariableDeclared here
_domainvariableDeclared here
_pathvariableDeclared here
_securevariableDeclared here
_maxAgevariableDeclared here
_httpOnlyvariableDeclared here

Public Methods

ReturnNameDescription
CookieCreates an empty Cookie.
Cookie explicitCreates a cookie with the given name. The cookie never expires.
Cookie explicitCreates a cookie from the given NVCollection.
CookieCreates a cookie with the given name and value. The cookie never expires.
CookieCreates the Cookie by copying another one.
~CookieDestroys the Cookie.
Cookie &operator=Assigns a cookie.
voidsetVersionSets the version of the cookie.
intgetVersion const inlineReturns the version of the cookie, which is either 0 or 1.
voidsetNameSets the name of the cookie.
const std::string &getName const inlineReturns the name of the cookie.
voidsetValueSets the value of the cookie.
const std::string &getValue const inlineReturns the value of the cookie.
voidsetCommentSets the comment for the cookie.
const std::string &getComment const inlineReturns the comment for the cookie.
voidsetDomainSets the domain for the cookie.
const std::string &getDomain const inlineReturns the domain for the cookie.
voidsetPathSets the path for the cookie.
const std::string &getPath const inlineReturns the path for the cookie.
voidsetSecureSets the value of the secure flag for the cookie.
boolgetSecure const inlineReturns the value of the secure flag for the cookie.
voidsetMaxAgeSets the maximum age in seconds for the cookie.
intgetMaxAge const inlineReturns the maximum age in seconds for the cookie.
voidsetHttpOnlySets the HttpOnly flag for the cookie.
boolgetHttpOnly const inlineReturns true if the cookie's HttpOnly flag is set.
std::stringtoString constReturns a std::string representation of the cookie, suitable for use in a Set-Cookie header.

Cookie()

Defined in src/http/include/icy/http/cookie.h:43

Creates an empty Cookie.


explicit

explicit Cookie(const std::string & name)

Defined in src/http/include/icy/http/cookie.h:47

Creates a cookie with the given name. The cookie never expires.


explicit

explicit Cookie(const NVCollection & nvc)

Defined in src/http/include/icy/http/cookie.h:50

Creates a cookie from the given NVCollection.


Cookie(const std::string & name, const std::string & value)

Defined in src/http/include/icy/http/cookie.h:58

Creates a cookie with the given name and value. The cookie never expires.

Note: If value contains whitespace or non-alphanumeric characters, the value should be escaped by calling escape() before passing it to the constructor.


Cookie(const Cookie & cookie)

Defined in src/http/include/icy/http/cookie.h:61

Creates the Cookie by copying another one.


~Cookie()

Defined in src/http/include/icy/http/cookie.h:64

Destroys the Cookie.


operator=

Cookie & operator=(const Cookie & cookie)

Defined in src/http/include/icy/http/cookie.h:67

Assigns a cookie.


setVersion

void setVersion(int version)

Defined in src/http/include/icy/http/cookie.h:73

Sets the version of the cookie.

Version must be either 0 (denoting a Netscape cookie) or 1 (denoting a RFC 2109 cookie).


getVersion

const inline

inline int getVersion() const

Defined in src/http/include/icy/http/cookie.h:77

Returns the version of the cookie, which is either 0 or 1.


setName

void setName(const std::string & name)

Defined in src/http/include/icy/http/cookie.h:80

Sets the name of the cookie.


getName

const inline

inline const std::string & getName() const

Defined in src/http/include/icy/http/cookie.h:83

Returns the name of the cookie.


setValue

void setValue(const std::string & value)

Defined in src/http/include/icy/http/cookie.h:93

Sets the value of the cookie.

According to the cookie specification, the size of the value should not exceed 4 Kbytes.

Note: If value contains whitespace or non-alphanumeric characters, the value should be escaped by calling escape() prior to passing it to setName().


getValue

const inline

inline const std::string & getValue() const

Defined in src/http/include/icy/http/cookie.h:96

Returns the value of the cookie.


setComment

void setComment(const std::string & comment)

Defined in src/http/include/icy/http/cookie.h:101

Sets the comment for the cookie.

Comments are only supported for version 1 cookies.


getComment

const inline

inline const std::string & getComment() const

Defined in src/http/include/icy/http/cookie.h:104

Returns the comment for the cookie.


setDomain

void setDomain(const std::string & domain)

Defined in src/http/include/icy/http/cookie.h:107

Sets the domain for the cookie.


getDomain

const inline

inline const std::string & getDomain() const

Defined in src/http/include/icy/http/cookie.h:110

Returns the domain for the cookie.


setPath

void setPath(const std::string & path)

Defined in src/http/include/icy/http/cookie.h:113

Sets the path for the cookie.


getPath

const inline

inline const std::string & getPath() const

Defined in src/http/include/icy/http/cookie.h:116

Returns the path for the cookie.


setSecure

void setSecure(bool secure)

Defined in src/http/include/icy/http/cookie.h:120

Sets the value of the secure flag for the cookie.


getSecure

const inline

inline bool getSecure() const

Defined in src/http/include/icy/http/cookie.h:124

Returns the value of the secure flag for the cookie.


setMaxAge

void setMaxAge(int maxAge)

Defined in src/http/include/icy/http/cookie.h:134

Sets the maximum age in seconds for the cookie.

A value of -1 causes the cookie to never expire on the client.

A value of 0 deletes the cookie on the client.


getMaxAge

const inline

inline int getMaxAge() const

Defined in src/http/include/icy/http/cookie.h:138

Returns the maximum age in seconds for the cookie.


setHttpOnly

void setHttpOnly(bool flag = true)

Defined in src/http/include/icy/http/cookie.h:141

Sets the HttpOnly flag for the cookie.


getHttpOnly

const inline

inline bool getHttpOnly() const

Defined in src/http/include/icy/http/cookie.h:144

Returns true if the cookie's HttpOnly flag is set.


toString

const

std::string toString() const

Defined in src/http/include/icy/http/cookie.h:148

Returns a std::string representation of the cookie, suitable for use in a Set-Cookie header.

Public Static Methods

ReturnNameDescription
std::stringescape staticEscapes the given std::string by replacing all non-alphanumeric characters with escape sequences in the form xx, where xx is the hexadecimal character code.
std::stringunescape staticUnescapes the given std::string by replacing all escape sequences in the form xx with the respective characters.

escape

static

static std::string escape(std::string_view str)

Defined in src/http/include/icy/http/cookie.h:154

Escapes the given std::string by replacing all non-alphanumeric characters with escape sequences in the form xx, where xx is the hexadecimal character code.


unescape

static

static std::string unescape(std::string_view str)

Defined in src/http/include/icy/http/cookie.h:159

Unescapes the given std::string by replacing all escape sequences in the form xx with the respective characters.

Private Attributes

ReturnNameDescription
int_version
std::string_name
std::string_value
std::string_comment
std::string_domain
std::string_path
bool_secure
int_maxAge
bool_httpOnly

_version

int _version

Defined in src/http/include/icy/http/cookie.h:162


_name

std::string _name

Defined in src/http/include/icy/http/cookie.h:163


_value

std::string _value

Defined in src/http/include/icy/http/cookie.h:164


_comment

std::string _comment

Defined in src/http/include/icy/http/cookie.h:165


_domain

std::string _domain

Defined in src/http/include/icy/http/cookie.h:166


_path

std::string _path

Defined in src/http/include/icy/http/cookie.h:167


_secure

bool _secure

Defined in src/http/include/icy/http/cookie.h:168


_maxAge

int _maxAge

Defined in src/http/include/icy/http/cookie.h:169


_httpOnly

bool _httpOnly

Defined in src/http/include/icy/http/cookie.h:170