Skip to content

Commit

Permalink
Merge pull request #151 from ilovenoah/150-add-set-http_cookie
Browse files Browse the repository at this point in the history
add new member variables response class
  • Loading branch information
ilovenoah authored Mar 9, 2024
2 parents 9a0a9c8 + 40911ee commit 11b739d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions inc/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Response {
std::string _status;
std::string _statusMsg;
std::map<std::string, std::string, CaseInsensitiveCompare> _headers;
std::vector<std::string> _setCookies;
std::string _body;
std::string _rawData;
Server *_server;
Expand Down
2 changes: 2 additions & 0 deletions inc/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion src/response/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ void Response::_setCGIResponseHeader(const bool shouldKeepAlive) {
this->_headers.clear();
return;
}
this->_headers.insert(std::pair<std::string, std::string>(key, value));
if (utils::compCaseInsensitive(key, "Set-Cookie") == 0) {
this->_setCookies.push_back(value);
} else {
this->_headers.insert(std::pair<std::string, std::string>(key, value));
}
}
std::streampos endPos = ss.tellg();
std::string::size_type readByte = endPos - startPos;
Expand Down Expand Up @@ -740,6 +744,9 @@ std::string Response::getEntireData() const {
iter != this->_headers.end(); ++iter) {
entireData += iter->first + ": " + iter->second + "\r\n";
}
for (std::vector<std::string>::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;
Expand Down
15 changes: 15 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 11b739d

Please sign in to comment.