fix setting cookies
This commit is contained in:
parent
ae1aa94aa3
commit
e2ed32f414
13
response.hpp
13
response.hpp
@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
using std::string_view;
|
using std::string_view;
|
||||||
@ -13,6 +14,7 @@ struct Response {
|
|||||||
std::string body;
|
std::string body;
|
||||||
std::string content_type = "text/plain";
|
std::string content_type = "text/plain";
|
||||||
std::unordered_map<std::string, std::string> headers;
|
std::unordered_map<std::string, std::string> headers;
|
||||||
|
std::vector<std::string> cookies;
|
||||||
|
|
||||||
void set_json(const std::string& json) {
|
void set_json(const std::string& json) {
|
||||||
body = json;
|
body = json;
|
||||||
@ -33,12 +35,12 @@ struct Response {
|
|||||||
void set_cookie(string_view name, string_view value, int max_age = -1,
|
void set_cookie(string_view name, string_view value, int max_age = -1,
|
||||||
string_view path = "", string_view domain = "", bool secure = false, bool http_only = false) {
|
string_view path = "", string_view domain = "", bool secure = false, bool http_only = false) {
|
||||||
std::string cookie_header = CookieHelpers::build_set_cookie(name, value, max_age, path, domain, secure, http_only);
|
std::string cookie_header = CookieHelpers::build_set_cookie(name, value, max_age, path, domain, secure, http_only);
|
||||||
headers["Set-Cookie"] = cookie_header;
|
cookies.push_back(cookie_header);
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_cookie(string_view name, string_view path = "") {
|
void delete_cookie(string_view name, string_view path = "") {
|
||||||
std::string cookie_header = CookieHelpers::build_delete_cookie(name, path);
|
std::string cookie_header = CookieHelpers::build_delete_cookie(name, path);
|
||||||
headers["Set-Cookie"] = cookie_header;
|
cookies.push_back(cookie_header);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -95,6 +97,13 @@ public:
|
|||||||
result += "\r\n";
|
result += "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Multiple Set-Cookie headers
|
||||||
|
for (const auto& cookie : response.cookies) {
|
||||||
|
result += "Set-Cookie: ";
|
||||||
|
result += cookie;
|
||||||
|
result += "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
// Connection handling
|
// Connection handling
|
||||||
bool keep_alive = version != "HTTP/1.0";
|
bool keep_alive = version != "HTTP/1.0";
|
||||||
if (response.headers.find("Connection") == response.headers.end()) {
|
if (response.headers.find("Connection") == response.headers.end()) {
|
||||||
|
@ -193,7 +193,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void set_session_cookie(Response& response, const std::string& session_id) {
|
void set_session_cookie(Response& response, const std::string& session_id) {
|
||||||
response.headers["Set-Cookie"] = "session_id=" + session_id + "; HttpOnly; Path=/; SameSite=Strict";
|
response.cookies.push_back("session_id=" + session_id + "; HttpOnly; Path=/; SameSite=Strict");
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_response(int client_fd, const Response& response, string_view version) {
|
void send_response(int client_fd, const Response& response, string_view version) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user