diff --git a/inc/Response.hpp b/inc/Response.hpp index 6eb1a23..fcc17f7 100644 --- a/inc/Response.hpp +++ b/inc/Response.hpp @@ -30,6 +30,7 @@ class Response { std::string _status; std::string _statusMsg; std::map _headers; + std::vector _setCookies; std::string _body; std::string _rawData; Server *_server; diff --git a/inc/utils.hpp b/inc/utils.hpp index 9c0f67b..5af3ba5 100644 --- a/inc/utils.hpp +++ b/inc/utils.hpp @@ -38,6 +38,8 @@ int x_chdir(const char *dir); int x_dup2(int fd, int fd2); int x_kill(int pid, int sig); std::string trim(std::string const &line, std::string const &str); +ssize_t compCaseInsensitive(const std::string &s1, const std::string &s2); + } // namespace utils #endif \ No newline at end of file diff --git a/src/response/Response.cpp b/src/response/Response.cpp index a95bdf7..bec55a9 100644 --- a/src/response/Response.cpp +++ b/src/response/Response.cpp @@ -462,7 +462,11 @@ void Response::_setCGIResponseHeader(const bool shouldKeepAlive) { this->_headers.clear(); return; } - this->_headers.insert(std::pair(key, value)); + if (utils::compCaseInsensitive(key, "Set-Cookie") == 0) { + this->_setCookies.push_back(value); + } else { + this->_headers.insert(std::pair(key, value)); + } } std::streampos endPos = ss.tellg(); std::string::size_type readByte = endPos - startPos; @@ -740,6 +744,9 @@ std::string Response::getEntireData() const { iter != this->_headers.end(); ++iter) { entireData += iter->first + ": " + iter->second + "\r\n"; } + for (std::vector::const_iterator iter = this->_setCookies.begin(); iter != this->_setCookies.end(); ++iter) { + entireData += "Set-Cookie: " + *iter + "\r\n"; + } entireData += "\r\n"; entireData += this->_body; return entireData; diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 5bafa35..e41ec89 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -184,4 +184,19 @@ std::string trim(std::string const &line, std::string const &str) { return line.substr(posbig, strRange); } +static unsigned char helper_tolower(unsigned char c) { + if ('A' <= c && c <= 'Z') { + return (c - 'A' + 'a'); + } + return (c); +} + +ssize_t compCaseInsensitive(const std::string &s1, const std::string &s2) { + std::string str1(s1.length(), ' '); + std::string str2(s2.length(), ' '); + std::transform(s1.begin(), s1.end(), str1.begin(), helper_tolower); + std::transform(s2.begin(), s2.end(), str2.begin(), helper_tolower); + return s1.compare(s2); +} + } // namespace utils \ No newline at end of file