HTTP module

Cookie

HTTP cookie value plus its response/header attributes.

#include <icy/http/cookie.h>

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.

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

Creates an empty Cookie.


explicit

explicit Cookie(const std::string & name)

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


explicit

explicit Cookie(const NVCollection & nvc)

Creates a cookie from the given NVCollection.


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

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)

Creates the Cookie by copying another one.


~Cookie()

Destroys the Cookie.


operator=

Cookie & operator=(const Cookie & cookie)

Assigns a cookie.


setVersion

void setVersion(int version)

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

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


setName

void setName(const std::string & name)

Sets the name of the cookie.


getName

const inline

inline const std::string & getName() const

Returns the name of the cookie.


setValue

void setValue(const std::string & value)

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

Returns the value of the cookie.


setComment

void setComment(const std::string & comment)

Sets the comment for the cookie.

Comments are only supported for version 1 cookies.


getComment

const inline

inline const std::string & getComment() const

Returns the comment for the cookie.


setDomain

void setDomain(const std::string & domain)

Sets the domain for the cookie.


getDomain

const inline

inline const std::string & getDomain() const

Returns the domain for the cookie.


setPath

void setPath(const std::string & path)

Sets the path for the cookie.


getPath

const inline

inline const std::string & getPath() const

Returns the path for the cookie.


setSecure

void setSecure(bool secure)

Sets the value of the secure flag for the cookie.


getSecure

const inline

inline bool getSecure() const

Returns the value of the secure flag for the cookie.


setMaxAge

void setMaxAge(int maxAge)

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

Returns the maximum age in seconds for the cookie.


setHttpOnly

void setHttpOnly(bool flag)

Sets the HttpOnly flag for the cookie.


getHttpOnly

const inline

inline bool getHttpOnly() const

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


toString

const

std::string toString() const

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)

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)

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

_name

std::string _name

_value

std::string _value

_comment

std::string _comment

_domain

std::string _domain

_path

std::string _path

_secure

bool _secure

_maxAge

int _maxAge

_httpOnly

bool _httpOnly