2#include <boost/algorithm/string/trim.hpp>
3#include "crow/http_request.h"
4#include "crow/http_response.h"
35 std::unordered_map<std::string, std::string> jar;
36 std::unordered_map<std::string, std::string> cookies_to_add;
38 std::string get_cookie(
const std::string& key)
const
40 auto cookie = jar.find(key);
41 if (cookie != jar.end())
42 return cookie->second;
46 void set_cookie(
const std::string& key,
const std::string& value)
48 cookies_to_add.emplace(key, value);
54 int count = req.headers.count(
"Cookie");
63 std::string cookies = req.get_header_value(
"Cookie");
65 while(pos < cookies.size())
67 size_t pos_equal = cookies.find(
'=', pos);
68 if (pos_equal == cookies.npos)
70 std::string name = cookies.substr(pos, pos_equal-pos);
73 while(pos < cookies.size() && cookies[pos] ==
' ') pos++;
74 if (pos == cookies.size())
77 size_t pos_semicolon = cookies.find(
';', pos);
78 std::string value = cookies.substr(pos, pos_semicolon-pos);
81 if (value[0] ==
'"' && value[value.size()-1] ==
'"')
83 value = value.substr(1, value.size()-2);
86 ctx.jar.emplace(std::move(name), std::move(value));
89 if (pos == cookies.npos)
92 while(pos < cookies.size() && cookies[pos] ==
' ') pos++;
96 void after_handle(request& , response& res, context& ctx)
98 for(
auto& cookie:ctx.cookies_to_add)
100 if (cookie.second.empty())
101 res.add_header(
"Set-Cookie", cookie.first +
"=\"\"");
103 res.add_header(
"Set-Cookie", cookie.first +
"=" + cookie.second);
Definition: cookie_parser.h:34
Definition: cookie_parser.h:32
An HTTP request.
Definition: http_request.h:27
HTTP response.
Definition: http_response.h:24
int code
The Status code for the response.
Definition: http_response.h:28
void end()
Set the response completion flag and call the handler (to send the response).
Definition: http_response.h:151