mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
cookie.h
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef MOD_SERVLET_COOKIE_H
9 #define MOD_SERVLET_COOKIE_H
10 
11 #include <string>
12 
13 namespace servlet
14 {
15 
46 class cookie
47 {
48 public:
70  cookie(const std::string &name, const std::string &value) : _name{name}, _value{value} {}
71 
82  cookie(std::string &&name, std::string &&value) : _name{std::move(name)}, _value{std::move(value)} {}
83 
94  cookie(std::string &&name, const std::string &value) : _name{std::move(name)}, _value{value} {}
95 
106  cookie(const std::string &name, std::string &&value) : _name{name}, _value{std::move(value)} {}
107 
112  cookie(const cookie& c) = default;
113 
118  cookie(cookie&& c) = default;
119 
124  cookie& operator=(const cookie& c) = default;
125 
130  cookie& operator=(cookie&& c) = default;
131 
135  ~cookie() noexcept = default;
136 
143  const std::string &get_name() const { return _name; }
144 
152  const std::string &get_value() const { return _value; }
153 
162  const std::string &get_comment() const { return _comment; }
163 
171  const std::string &get_domain() const { return _domain; }
172 
182  long get_max_age() const { return _max_age; }
183 
192  const std::string &get_path() const { return _path; }
193 
203  bool is_secure() const { return _secure; }
204 
215  int get_version() const { return _version; }
216 
224  bool is_http_only() const { return _http_only; }
225 
239  void set_value(const std::string &value) { _value = value; }
240 
250  void set_value(std::string &&value) { _value = std::move(value); }
251 
261  void set_comment(const std::string &comment) { _comment = comment; }
262 
273  void set_comment(std::string &&comment) { _comment = std::move(comment); }
274 
288  void set_domain(const std::string &domain) { _domain = domain; }
289 
300  void set_domain(std::string &&domain) { _domain = std::move(domain); }
301 
318  void set_max_age(long max_age) { _max_age = max_age; }
319 
336  void set_path(const std::string &path) { _path = path; }
337 
348  void set_path(std::string &&path) { _path = std::move(path); }
349 
361  void set_secure(bool secure) { _secure = secure; }
362 
375  void set_version(int version) { _version = version; }
376 
383  void set_http_only(bool http_only) { _http_only = http_only; }
384 
390  std::string to_string() const;
391 
392 private:
393  std::string _name; /* NAME= ... "$Name" style is reserved */
394  std::string _value; /* value of NAME */
395 
396  /* Attributes encoded in the header's cookie fields. */
397  std::string _comment; /* ;Comment=VALUE ... describes cookie's use */
398  /* ;Discard ... implied by maxAge < 0 */
399  std::string _domain; /* ;Domain=VALUE ... domain that sees cookie */
400  long _max_age = -1; /* ;Max-Age=VALUE ... cookies auto-expire */
401  std::string _path; /* ;Path=VALUE ... URLs that see the cookie */
402  bool _secure = false; /* ;Secure ... e.g. use SSL */
403  int _version = 0; /* ;Version=1 ... means RFC 2109++ style */
404  bool _http_only = false; /* Not in cookie specs, but supported by browsers */
405 };
406 
407 } // end of servlet namespace
408 
409 #endif // MOD_SERVLET_COOKIE_H
void set_comment(std::string &&comment)
Specifies a comment that describes a cookie's purpose.
Definition: cookie.h:273
const std::string & get_value() const
Returns the value of the cookie.
Definition: cookie.h:152
cookie(std::string &&name, std::string &&value)
Constructs a cookie with a specified name and value.
Definition: cookie.h:82
void set_comment(const std::string &comment)
Specifies a comment that describes a cookie's purpose.
Definition: cookie.h:261
cookie(std::string &&name, const std::string &value)
Constructs a cookie with a specified name and value.
Definition: cookie.h:94
long get_max_age() const
Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie wil...
Definition: cookie.h:182
cookie(const std::string &name, const std::string &value)
Constructs a cookie with a specified name and value.
Definition: cookie.h:70
bool is_secure() const
Returns true if the browser is sending cookies only over a secure protocol, or false if the browser c...
Definition: cookie.h:203
void set_value(std::string &&value)
Assigns a new value to a cookie after the cookie is created.
Definition: cookie.h:250
int get_version() const
Returns the version of the protocol this cookie complies with.
Definition: cookie.h:215
void set_domain(std::string &&domain)
Specifies the domain within which this cookie should be presented.
Definition: cookie.h:300
void set_secure(bool secure)
Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
Definition: cookie.h:361
void set_domain(const std::string &domain)
Specifies the domain within which this cookie should be presented.
Definition: cookie.h:288
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server.
Definition: cookie.h:46
const std::string & get_name() const
Returns the name of the cookie.
Definition: cookie.h:143
~cookie() noexcept=default
Destructor.
std::string to_string() const
Converts this cookie into its string representation.
void set_max_age(long max_age)
Sets the maximum age of the cookie in seconds.
Definition: cookie.h:318
bool is_http_only() const
Gets the flag that controls if this cookie will be hidden from scripts on the client side...
Definition: cookie.h:224
cookie & operator=(const cookie &c)=default
Copy assignment operator.
void set_version(int version)
Sets the version of the cookie protocol this cookie complies with.
Definition: cookie.h:375
void set_path(std::string &&path)
Specifies a path for the cookie to which the client should return the cookie.
Definition: cookie.h:348
void set_value(const std::string &value)
Assigns a new value to a cookie after the cookie is created.
Definition: cookie.h:239
cookie(const std::string &name, std::string &&value)
Constructs a cookie with a specified name and value.
Definition: cookie.h:106
void set_path(const std::string &path)
Specifies a path for the cookie to which the client should return the cookie.
Definition: cookie.h:336
const std::string & get_comment() const
Returns the comment describing the purpose of this cookie, or empty string if the cookie has no comme...
Definition: cookie.h:162
void set_http_only(bool http_only)
Sets the flag that controls if this cookie will be hidden from scripts on the client side...
Definition: cookie.h:383
const std::string & get_domain() const
Returns the domain name set for this cookie.
Definition: cookie.h:171
const std::string & get_path() const
Returns the path on the server to which the browser returns this cookie.
Definition: cookie.h:192