drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
Cookie.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <drogon/exports.h>
17#include <trantor/utils/Date.h>
18#include <trantor/utils/Logger.h>
20#include <cctype>
21#include <string>
22#include <limits>
23#include <optional>
24#include <string_view>
25
26namespace drogon
27{
31class DROGON_EXPORT Cookie
32{
33 public:
35
39 Cookie(std::string key, std::string value)
40 : key_(std::move(key)), value_(std::move(value))
41 {
42 }
43
44 Cookie() = default;
45 enum class SameSite
46 {
47 kNull,
48 kLax,
49 kStrict,
50 kNone
51 };
52
58 void setExpiresDate(const trantor::Date &date)
59 {
60 expiresDate_ = date;
61 }
62
66 void setHttpOnly(bool only)
67 {
68 httpOnly_ = only;
69 }
70
74 void setSecure(bool secure)
75 {
76 secure_ = secure;
77 }
78
82 void setDomain(const std::string &domain)
83 {
84 domain_ = domain;
85 }
86
90 void setDomain(std::string &&domain)
91 {
92 domain_ = std::move(domain);
93 }
94
98 void setPath(const std::string &path)
99 {
100 path_ = path;
101 }
102
106 void setPath(std::string &&path)
107 {
108 path_ = std::move(path);
109 }
110
114 void setKey(const std::string &key)
115 {
116 key_ = key;
117 }
118
122 void setKey(std::string &&key)
123 {
124 key_ = std::move(key);
125 }
126
130 void setValue(const std::string &value)
131 {
132 value_ = value;
133 }
134
138 void setValue(std::string &&value)
139 {
140 value_ = std::move(value);
141 }
142
146 void setMaxAge(int value)
147 {
148 maxAge_ = value;
149 }
150
154 void setSameSite(SameSite sameSite)
155 {
156 sameSite_ = sameSite;
157 }
158
162 void setPartitioned(bool partitioned)
163 {
164 partitioned_ = partitioned;
165 if (partitioned)
166 {
167 setSecure(true);
168 }
169 }
170
174 std::string cookieString() const;
175
179 std::string getCookieString() const
180 {
181 return cookieString();
182 }
183
187 const trantor::Date &expiresDate() const
188 {
189 return expiresDate_;
190 }
191
195 const trantor::Date &getExpiresDate() const
196 {
197 return expiresDate_;
198 }
199
203 const std::string &domain() const
204 {
205 return domain_;
206 }
207
211 const std::string &getDomain() const
212 {
213 return domain_;
214 }
215
219 const std::string &path() const
220 {
221 return path_;
222 }
223
227 const std::string &getPath() const
228 {
229 return path_;
230 }
231
235 const std::string &key() const
236 {
237 return key_;
238 }
239
243 const std::string &getKey() const
244 {
245 return key_;
246 }
247
251 const std::string &value() const
252 {
253 return value_;
254 }
255
259 const std::string &getValue() const
260 {
261 return value_;
262 }
263
270 operator bool() const
271 {
272 return (!key_.empty()) && (!value_.empty());
273 }
274
281 bool isHttpOnly() const
282 {
283 return httpOnly_;
284 }
285
292 bool isSecure() const
293 {
294 return secure_;
295 }
296
303 bool isPartitioned() const
304 {
305 return partitioned_;
306 }
307
311 std::optional<int> maxAge() const
312 {
313 return maxAge_;
314 }
315
319 std::optional<int> getMaxAge() const
320 {
321 return maxAge_;
322 }
323
327 SameSite sameSite() const
328 {
329 return sameSite_;
330 }
331
335 SameSite getSameSite() const
336 {
337 return sameSite_;
338 }
339
353 static bool stricmp(const std::string_view str1,
354 const std::string_view str2)
355 {
356 auto str1Len{str1.length()};
357 auto str2Len{str2.length()};
358 if (str1Len != str2Len)
359 return false;
360 for (size_t idx{0}; idx < str1Len; ++idx)
361 {
362 auto lowerChar{tolower(str1[idx])};
363
364 if (lowerChar != str2[idx])
365 {
366 return false;
367 }
368 }
369 return true;
370 }
371
376 static SameSite convertString2SameSite(std::string_view sameSite)
377 {
378 if (stricmp(sameSite, "lax"))
379 return Cookie::SameSite::kLax;
380 if (stricmp(sameSite, "strict"))
381 return Cookie::SameSite::kStrict;
382 if (stricmp(sameSite, "none"))
383 return Cookie::SameSite::kNone;
384 if (!stricmp(sameSite, "null"))
385 {
386 LOG_WARN
387 << "'" << sameSite
388 << "' is not a valid SameSite policy. 'Null', 'Lax', 'Strict' "
389 "or "
390 "'None' are proper values. Return value is SameSite::kNull.";
391 }
392 return Cookie::SameSite::kNull;
393 }
394
399 static std::string_view convertSameSite2String(SameSite sameSite)
400 {
401 switch (sameSite)
402 {
403 case SameSite::kLax:
404 return "Lax";
405 case SameSite::kStrict:
406 return "Strict";
407 case SameSite::kNone:
408 return "None";
409 case SameSite::kNull:
410 return "Null";
411 default:
412 return "UNDEFINED";
413 }
414 }
415
416 private:
417 trantor::Date expiresDate_{(std::numeric_limits<int64_t>::max)()};
418 bool httpOnly_{true};
419 bool secure_{false};
420 bool partitioned_{false};
421 std::string domain_;
422 std::string path_;
423 std::string key_;
424 std::string value_;
425 std::optional<int> maxAge_;
426 SameSite sameSite_{SameSite::kNull};
427};
428
429} // namespace drogon
this class represents a cookie entity.
Definition Cookie.h:32
const std::string & getPath() const
Get the path of the cookie.
Definition Cookie.h:227
void setDomain(std::string &&domain)
Set the domain of the cookie.
Definition Cookie.h:90
static SameSite convertString2SameSite(std::string_view sameSite)
Converts a string value to its associated enum class SameSite value.
Definition Cookie.h:376
const std::string & getValue() const
Get the value of the cookie.
Definition Cookie.h:259
static bool stricmp(const std::string_view str1, const std::string_view str2)
Compare two strings ignoring the their cases.
Definition Cookie.h:353
void setHttpOnly(bool only)
Set if the cookie is HTTP only.
Definition Cookie.h:66
SameSite sameSite() const
Get the same site of the cookie.
Definition Cookie.h:327
const std::string & domain() const
Get the domain of the cookie.
Definition Cookie.h:203
void setPath(const std::string &path)
Set the path of the cookie.
Definition Cookie.h:98
void setPartitioned(bool partitioned)
Set the partitioned status of the cookie.
Definition Cookie.h:162
std::optional< int > getMaxAge() const
Get the max-age of the cookie.
Definition Cookie.h:319
bool isPartitioned() const
Check if the cookie is partitioned.
Definition Cookie.h:303
const std::string & path() const
Get the path of the cookie.
Definition Cookie.h:219
void setSecure(bool secure)
Set if the cookie is secure.
Definition Cookie.h:74
SameSite getSameSite() const
Get the same site of the cookie.
Definition Cookie.h:335
const std::string & getKey() const
Get the keyword of the cookie.
Definition Cookie.h:243
void setKey(const std::string &key)
Set the key of the cookie.
Definition Cookie.h:114
bool isSecure() const
Check if the cookie is secure.
Definition Cookie.h:292
void setKey(std::string &&key)
Set the key of the cookie.
Definition Cookie.h:122
std::string getCookieString() const
Get the string value of the cookie.
Definition Cookie.h:179
std::optional< int > maxAge() const
Get the max-age of the cookie.
Definition Cookie.h:311
static std::string_view convertSameSite2String(SameSite sameSite)
Converts an enum class SameSite value to its associated string value.
Definition Cookie.h:399
bool isHttpOnly() const
Check if the cookie is HTTP only.
Definition Cookie.h:281
void setSameSite(SameSite sameSite)
Set the same site of the cookie.
Definition Cookie.h:154
void setExpiresDate(const trantor::Date &date)
Set the Expires Date.
Definition Cookie.h:58
const std::string & key() const
Get the keyword of the cookie.
Definition Cookie.h:235
const std::string & value() const
Get the value of the cookie.
Definition Cookie.h:251
const trantor::Date & expiresDate() const
Get the expiration date of the cookie.
Definition Cookie.h:187
void setPath(std::string &&path)
Set the path of the cookie.
Definition Cookie.h:106
const trantor::Date & getExpiresDate() const
Get the expiration date of the cookie.
Definition Cookie.h:195
void setValue(std::string &&value)
Set the value of the cookie.
Definition Cookie.h:138
std::string cookieString() const
Get the string value of the cookie.
Cookie(std::string key, std::string value)
Constructor.
Definition Cookie.h:39
void setValue(const std::string &value)
Set the value of the cookie.
Definition Cookie.h:130
void setMaxAge(int value)
Set the max-age of the cookie.
Definition Cookie.h:146
const std::string & getDomain() const
Get the domain of the cookie.
Definition Cookie.h:211
void setDomain(const std::string &domain)
Set the domain of the cookie.
Definition Cookie.h:82
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
STL namespace.