Cookie
Cookie
#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
| Return | Name | Description |
|---|---|---|
Cookie | Creates an empty Cookie. | |
Cookie explicit | Creates a cookie with the given name. The cookie never expires. | |
Cookie explicit | Creates a cookie from the given NVCollection. | |
Cookie | Creates a cookie with the given name and value. The cookie never expires. | |
Cookie | Creates the Cookie by copying another one. | |
~Cookie | Destroys the Cookie. | |
Cookie & | operator= | Assigns a cookie. |
void | setVersion | Sets the version of the cookie. |
int | getVersion const inline | Returns the version of the cookie, which is either 0 or 1. |
void | setName | Sets the name of the cookie. |
const std::string & | getName const inline | Returns the name of the cookie. |
void | setValue | Sets the value of the cookie. |
const std::string & | getValue const inline | Returns the value of the cookie. |
void | setComment | Sets the comment for the cookie. |
const std::string & | getComment const inline | Returns the comment for the cookie. |
void | setDomain | Sets the domain for the cookie. |
const std::string & | getDomain const inline | Returns the domain for the cookie. |
void | setPath | Sets the path for the cookie. |
const std::string & | getPath const inline | Returns the path for the cookie. |
void | setSecure | Sets the value of the secure flag for the cookie. |
bool | getSecure const inline | Returns the value of the secure flag for the cookie. |
void | setMaxAge | Sets the maximum age in seconds for the cookie. |
int | getMaxAge const inline | Returns the maximum age in seconds for the cookie. |
void | setHttpOnly | Sets the HttpOnly flag for the cookie. |
bool | getHttpOnly const inline | Returns true if the cookie's HttpOnly flag is set. |
std::string | toString const | Returns a std::string representation of the cookie, suitable for use in a Set-Cookie header. |
Cookie
Cookie()Creates an empty Cookie.
Cookie
explicit
explicit Cookie(const std::string & name)Creates a cookie with the given name. The cookie never expires.
Cookie
explicit
explicit Cookie(const NVCollection & nvc)Creates a cookie from the given NVCollection.
Cookie
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
Cookie(const Cookie & cookie)Creates the Cookie by copying another one.
~Cookie
~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() constReturns 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() constReturns 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() constReturns 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() constReturns 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() constReturns 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() constReturns 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() constReturns 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() constReturns 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() constReturns true if the cookie's HttpOnly flag is set.
toString
const
std::string toString() constReturns a std::string representation of the cookie, suitable for use in a Set-Cookie header.
Public Static Methods
| Return | Name | Description |
|---|---|---|
std::string | escape static | 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. |
std::string | unescape static | Unescapes 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
| Return | Name | Description |
|---|---|---|
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