HTTPServer/HTTPSessions.cpp
2022-06-29 11:38:44 -07:00

47 lines
1.6 KiB
C++

#include "HTTPSessions.h"
#include "HTTPSession.h"
#include "Log.h"
namespace http {
HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest &httpRequest) {
coreutils::ZString sessionId(httpRequest.message.getHeaderKeyPairValue("Cookie", "SessionId"));
if(sessionId.str() == "")
return createHTTPSession(httpRequest);
else
return findSessionById(sessionId, httpRequest);
}
HTTPSession * HTTPSessions::findSessionById(coreutils::ZString &sessionId, HTTPRequest &httpRequest) {
try {
HTTPSession *httpSession = sessions.at(sessionId);
return httpSession;
}
catch (...) {
return createHTTPSession(httpRequest);
}
}
HTTPSession * HTTPSessions::createHTTPSession(HTTPRequest &httpRequest) {
HTTPSession *httpSession = new HTTPSession();
coreutils::ZString sessionId(httpSession->getSessionId());
sessions.insert(std::make_pair(sessionId, httpSession));
std::stringstream temp;
temp << "SessionId=" << httpSession->getSessionId();
httpRequest.response.addHeader(coreutils::IMFHeader("Set-Cookie", temp.str().c_str()));
return httpSession;
}
int HTTPSessions::processCommand(std::string command, core::TCPSession *session, std::stringstream &data) {
if(sessions.size() > 0) {
int seq = 0;
for(auto httpSession: sessions)
data << "|" << ++seq << "|" << httpSession.second->getSessionId() << "|" << std::endl;
}
else
data << "There are no sessions active." << std::endl;
return 1;
}
}