# Cookie

<a id="cookie"></a>

# Cookie

```cpp
#include <icy/http/cookie.h>

class Cookie
```

[Defined in src/http/include/icy/http/cookie.h:39](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L39)

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`](#cookie-1)  | Creates an empty [Cookie](#cookie). |
|  | [`Cookie`](#cookie-2) `explicit` | Creates a cookie with the given name. The cookie never expires. |
|  | [`Cookie`](#cookie-3) `explicit` | Creates a cookie from the given [NVCollection](https://auscaster.com/icey/docs/api-reference/icy-NVCollection.md#nvcollection). |
|  | [`Cookie`](#cookie-4)  | Creates a cookie with the given name and value. The cookie never expires. |
|  | [`Cookie`](#cookie-5)  | Creates the [Cookie](#cookie) by copying another one. |
|  | [`~Cookie`](#cookie-6)  | Destroys the [Cookie](#cookie). |
| [`Cookie`](#cookie) & | [`operator=`](#operator-201)  | Assigns a cookie. |
| `void` | [`setVersion`](#setversion)  | Sets the version of the cookie. |
| `int` | [`getVersion`](#getversion) `const` `inline` | Returns the version of the cookie, which is either 0 or 1. |
| `void` | [`setName`](#setname)  | Sets the name of the cookie. |
| `const std::string &` | [`getName`](#getname) `const` `inline` | Returns the name of the cookie. |
| `void` | [`setValue`](#setvalue)  | Sets the value of the cookie. |
| `const std::string &` | [`getValue`](#getvalue) `const` `inline` | Returns the value of the cookie. |
| `void` | [`setComment`](#setcomment)  | Sets the comment for the cookie. |
| `const std::string &` | [`getComment`](#getcomment) `const` `inline` | Returns the comment for the cookie. |
| `void` | [`setDomain`](#setdomain)  | Sets the domain for the cookie. |
| `const std::string &` | [`getDomain`](#getdomain) `const` `inline` | Returns the domain for the cookie. |
| `void` | [`setPath`](#setpath-1)  | Sets the path for the cookie. |
| `const std::string &` | [`getPath`](#getpath) `const` `inline` | Returns the path for the cookie. |
| `void` | [`setSecure`](#setsecure)  | Sets the value of the secure flag for the cookie. |
| `bool` | [`getSecure`](#getsecure) `const` `inline` | Returns the value of the secure flag for the cookie. |
| `void` | [`setMaxAge`](#setmaxage)  | Sets the maximum age in seconds for the cookie. |
| `int` | [`getMaxAge`](#getmaxage) `const` `inline` | Returns the maximum age in seconds for the cookie. |
| `void` | [`setHttpOnly`](#sethttponly)  | Sets the HttpOnly flag for the cookie. |
| `bool` | [`getHttpOnly`](#gethttponly) `const` `inline` | Returns true if the cookie's HttpOnly flag is set. |
| `std::string` | [`toString`](#tostring-8) `const` | Returns a std::string representation of the cookie, suitable for use in a Set-Cookie header. |

---

<a id="cookie-1"></a>

### Cookie

```cpp
Cookie()
```

[Defined in src/http/include/icy/http/cookie.h:43](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L43)

Creates an empty [Cookie](#cookie).

---

<a id="cookie-2"></a>

### Cookie

`explicit`

```cpp
explicit Cookie(const std::string & name)
```

[Defined in src/http/include/icy/http/cookie.h:47](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L47)

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

---

<a id="cookie-3"></a>

### Cookie

`explicit`

```cpp
explicit Cookie(const NVCollection & nvc)
```

[Defined in src/http/include/icy/http/cookie.h:50](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L50)

Creates a cookie from the given [NVCollection](https://auscaster.com/icey/docs/api-reference/icy-NVCollection.md#nvcollection).

---

<a id="cookie-4"></a>

### Cookie

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

[Defined in src/http/include/icy/http/cookie.h:58](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L58)

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()](#escape) before passing it to the constructor.

---

<a id="cookie-5"></a>

### Cookie

```cpp
Cookie(const Cookie & cookie)
```

[Defined in src/http/include/icy/http/cookie.h:61](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L61)

Creates the [Cookie](#cookie) by copying another one.

---

<a id="cookie-6"></a>

### ~Cookie

```cpp
~Cookie()
```

[Defined in src/http/include/icy/http/cookie.h:64](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L64)

Destroys the [Cookie](#cookie).

---

<a id="operator-201"></a>

### operator=

```cpp
Cookie & operator=(const Cookie & cookie)
```

[Defined in src/http/include/icy/http/cookie.h:67](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L67)

Assigns a cookie.

---

<a id="setversion"></a>

### setVersion

```cpp
void setVersion(int version)
```

[Defined in src/http/include/icy/http/cookie.h:73](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L73)

Sets the version of the cookie.

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

---

<a id="getversion"></a>

### getVersion

`const` `inline`

```cpp
inline int getVersion() const
```

[Defined in src/http/include/icy/http/cookie.h:77](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L77)

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

---

<a id="setname"></a>

### setName

```cpp
void setName(const std::string & name)
```

[Defined in src/http/include/icy/http/cookie.h:80](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L80)

Sets the name of the cookie.

---

<a id="getname"></a>

### getName

`const` `inline`

```cpp
inline const std::string & getName() const
```

[Defined in src/http/include/icy/http/cookie.h:83](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L83)

Returns the name of the cookie.

---

<a id="setvalue"></a>

### setValue

```cpp
void setValue(const std::string & value)
```

[Defined in src/http/include/icy/http/cookie.h:93](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L93)

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()](#escape) prior to passing it to [setName()](#setname).

---

<a id="getvalue"></a>

### getValue

`const` `inline`

```cpp
inline const std::string & getValue() const
```

[Defined in src/http/include/icy/http/cookie.h:96](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L96)

Returns the value of the cookie.

---

<a id="setcomment"></a>

### setComment

```cpp
void setComment(const std::string & comment)
```

[Defined in src/http/include/icy/http/cookie.h:101](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L101)

Sets the comment for the cookie.

Comments are only supported for version 1 cookies.

---

<a id="getcomment"></a>

### getComment

`const` `inline`

```cpp
inline const std::string & getComment() const
```

[Defined in src/http/include/icy/http/cookie.h:104](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L104)

Returns the comment for the cookie.

---

<a id="setdomain"></a>

### setDomain

```cpp
void setDomain(const std::string & domain)
```

[Defined in src/http/include/icy/http/cookie.h:107](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L107)

Sets the domain for the cookie.

---

<a id="getdomain"></a>

### getDomain

`const` `inline`

```cpp
inline const std::string & getDomain() const
```

[Defined in src/http/include/icy/http/cookie.h:110](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L110)

Returns the domain for the cookie.

---

<a id="setpath-1"></a>

### setPath

```cpp
void setPath(const std::string & path)
```

[Defined in src/http/include/icy/http/cookie.h:113](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L113)

Sets the path for the cookie.

---

<a id="getpath"></a>

### getPath

`const` `inline`

```cpp
inline const std::string & getPath() const
```

[Defined in src/http/include/icy/http/cookie.h:116](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L116)

Returns the path for the cookie.

---

<a id="setsecure"></a>

### setSecure

```cpp
void setSecure(bool secure)
```

[Defined in src/http/include/icy/http/cookie.h:120](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L120)

Sets the value of the secure flag for the cookie.

---

<a id="getsecure"></a>

### getSecure

`const` `inline`

```cpp
inline bool getSecure() const
```

[Defined in src/http/include/icy/http/cookie.h:124](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L124)

Returns the value of the secure flag for the cookie.

---

<a id="setmaxage"></a>

### setMaxAge

```cpp
void setMaxAge(int maxAge)
```

[Defined in src/http/include/icy/http/cookie.h:134](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L134)

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.

---

<a id="getmaxage"></a>

### getMaxAge

`const` `inline`

```cpp
inline int getMaxAge() const
```

[Defined in src/http/include/icy/http/cookie.h:138](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L138)

Returns the maximum age in seconds for the cookie.

---

<a id="sethttponly"></a>

### setHttpOnly

```cpp
void setHttpOnly(bool flag = true)
```

[Defined in src/http/include/icy/http/cookie.h:141](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L141)

Sets the HttpOnly flag for the cookie.

---

<a id="gethttponly"></a>

### getHttpOnly

`const` `inline`

```cpp
inline bool getHttpOnly() const
```

[Defined in src/http/include/icy/http/cookie.h:144](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L144)

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

---

<a id="tostring-8"></a>

### toString

`const`

```cpp
std::string toString() const
```

[Defined in src/http/include/icy/http/cookie.h:148](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L148)

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

## Public Static Methods

| Return | Name | Description |
|--------|------|-------------|
| `std::string` | [`escape`](#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`](#unescape) `static` | Unescapes the given std::string by replacing all escape sequences in the form xx with the respective characters. |

---

<a id="escape"></a>

### escape

`static`

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

[Defined in src/http/include/icy/http/cookie.h:154](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L154)

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.

---

<a id="unescape"></a>

### unescape

`static`

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

[Defined in src/http/include/icy/http/cookie.h:159](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L159)

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`](#_version)  |  |
| `std::string` | [`_name`](#_name-2)  |  |
| `std::string` | [`_value`](#_value)  |  |
| `std::string` | [`_comment`](#_comment)  |  |
| `std::string` | [`_domain`](#_domain)  |  |
| `std::string` | [`_path`](#_path-1)  |  |
| `bool` | [`_secure`](#_secure)  |  |
| `int` | [`_maxAge`](#_maxage)  |  |
| `bool` | [`_httpOnly`](#_httponly)  |  |

---

<a id="_version"></a>

### _version

```cpp
int _version
```

[Defined in src/http/include/icy/http/cookie.h:162](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L162)

---

<a id="_name-2"></a>

### _name

```cpp
std::string _name
```

[Defined in src/http/include/icy/http/cookie.h:163](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L163)

---

<a id="_value"></a>

### _value

```cpp
std::string _value
```

[Defined in src/http/include/icy/http/cookie.h:164](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L164)

---

<a id="_comment"></a>

### _comment

```cpp
std::string _comment
```

[Defined in src/http/include/icy/http/cookie.h:165](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L165)

---

<a id="_domain"></a>

### _domain

```cpp
std::string _domain
```

[Defined in src/http/include/icy/http/cookie.h:166](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L166)

---

<a id="_path-1"></a>

### _path

```cpp
std::string _path
```

[Defined in src/http/include/icy/http/cookie.h:167](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L167)

---

<a id="_secure"></a>

### _secure

```cpp
bool _secure
```

[Defined in src/http/include/icy/http/cookie.h:168](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L168)

---

<a id="_maxage"></a>

### _maxAge

```cpp
int _maxAge
```

[Defined in src/http/include/icy/http/cookie.h:169](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L169)

---

<a id="_httponly"></a>

### _httpOnly

```cpp
bool _httpOnly
```

[Defined in src/http/include/icy/http/cookie.h:170](https://github.com/auscaster/icey/blob/main/src/http/include/icy/http/cookie.h#L170)
