4 #include "crow/utility.h"
5 #include "crow/http_request.h"
6 #include "crow/http_response.h"
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;
235 int count = req.headers.count(
"Cookie");
244 std::string cookies = req.get_header_value(
"Cookie");
246 while (pos < cookies.size())
248 size_t pos_equal = cookies.find(
'=', pos);
249 if (pos_equal == cookies.npos)
251 std::string name = cookies.substr(pos, pos_equal - pos);
252 name = utility::trim(name);
254 if (pos == cookies.size())
257 size_t pos_semicolon = cookies.find(
';', pos);
258 std::string value = cookies.substr(pos, pos_semicolon - pos);
260 value = utility::trim(value);
261 if (value[0] ==
'"' && value[value.size() - 1] ==
'"')
263 value = value.substr(1, value.size() - 2);
266 ctx.jar.emplace(std::move(name), std::move(value));
269 if (pos == cookies.npos)
275 void after_handle(request& , response& res, context& ctx)
277 for (
const auto& cookie : ctx.cookies_to_add)
279 res.add_header(
"Set-Cookie", cookie.dump());
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition: cookie_parser.h:37
Definition: cookie_parser.h:203
Definition: cookie_parser.h:34
An HTTP request.
Definition: http_request.h:36
HTTP response.
Definition: http_response.h:34
int code
The Status code for the response.
Definition: http_response.h:40
void end()
Set the response completion flag and call the handler (to send the response).
Definition: http_response.h:237