Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new member variables response class #151

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading