38 enum class SameSitePolicy
46 Cookie(
const std::string& key, U&& value):
50 value_ = std::forward<U>(value);
53 Cookie(
const std::string& key):
57 std::string dump()
const
59 const static char* HTTP_DATE_FORMAT =
"%a, %d %b %Y %H:%M:%S GMT";
63 ss << (value_.empty() ?
"\"\"" : value_);
64 dumpString(ss, !domain_.empty(),
"Domain=", domain_);
65 dumpString(ss, !path_.empty(),
"Path=", path_);
66 dumpString(ss, secure_,
"Secure");
67 dumpString(ss, httponly_,
"HttpOnly");
70 ss << DIVIDER <<
"Expires="
71 << std::put_time(expires_at_.get(), HTTP_DATE_FORMAT);
75 ss << DIVIDER <<
"Max-Age=" << *max_age_;
79 ss << DIVIDER <<
"SameSite=";
82 case SameSitePolicy::Strict:
85 case SameSitePolicy::Lax:
88 case SameSitePolicy::None:
96 const std::string& name()
104 value_ = std::forward<U>(value);
109 Cookie& expires(
const std::tm& time)
111 expires_at_ = std::unique_ptr<std::tm>(
new std::tm(time));
116 Cookie& max_age(
long long seconds)
118 max_age_ = std::unique_ptr<long long>(
new long long(seconds));
123 Cookie& domain(
const std::string& name)
130 Cookie& path(
const std::string& path)
151 Cookie& same_site(SameSitePolicy ssp)
153 same_site_ = std::unique_ptr<SameSitePolicy>(
new SameSitePolicy(ssp));
163 httponly_(c.httponly_)
166 max_age_ = std::unique_ptr<long long>(
new long long(*c.max_age_));
169 expires_at_ = std::unique_ptr<std::tm>(
new std::tm(*c.expires_at_));
172 same_site_ = std::unique_ptr<SameSitePolicy>(
new SameSitePolicy(*c.same_site_));
178 static void dumpString(std::stringstream& ss,
bool cond,
const char* prefix,
179 const std::string& value =
"")
183 ss << DIVIDER << prefix << value;
190 std::unique_ptr<long long> max_age_{};
191 std::string domain_ =
"";
192 std::string path_ =
"";
193 bool secure_ =
false;
194 bool httponly_ =
false;
195 std::unique_ptr<std::tm> expires_at_{};
196 std::unique_ptr<SameSitePolicy> same_site_{};
198 static constexpr const char* DIVIDER =
"; ";
204 std::unordered_map<std::string, std::string> jar;
206 std::string get_cookie(
const std::string& key)
const
208 auto cookie = jar.find(key);
209 if (cookie != jar.end())
210 return cookie->second;
215 Cookie& set_cookie(
const std::string& key, U&& value)
217 cookies_to_add.emplace_back(key, std::forward<U>(value));
218 return cookies_to_add.back();
223 cookies_to_add.push_back(std::move(cookie));
224 return cookies_to_add.back();
229 std::vector<Cookie> cookies_to_add;