From e2ed32f41468ee9b211a73b97a552575e454aac6 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 13 Jun 2025 18:10:50 -0500 Subject: [PATCH] fix setting cookies --- response.hpp | 13 +++++++++++-- server.hpp | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/response.hpp b/response.hpp index 7fa806e..be4b5ea 100644 --- a/response.hpp +++ b/response.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include using std::string_view; @@ -13,6 +14,7 @@ struct Response { std::string body; std::string content_type = "text/plain"; std::unordered_map headers; + std::vector cookies; void set_json(const std::string& json) { body = json; @@ -33,12 +35,12 @@ struct Response { 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) { 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 = "") { 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"; } + // Multiple Set-Cookie headers + for (const auto& cookie : response.cookies) { + result += "Set-Cookie: "; + result += cookie; + result += "\r\n"; + } + // Connection handling bool keep_alive = version != "HTTP/1.0"; if (response.headers.find("Connection") == response.headers.end()) { diff --git a/server.hpp b/server.hpp index 48be584..5ec2a40 100644 --- a/server.hpp +++ b/server.hpp @@ -193,7 +193,7 @@ private: } 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) {