HTTPServer/HTTPSessions.cpp

47 lines
1.6 KiB
C++
Raw Normal View History

#include "HTTPSessions.h"
#include "HTTPSession.h"
#include "Log.h"
namespace http {
2022-06-29 11:38:44 -07:00
HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest &httpRequest) {
coreutils::ZString sessionId(httpRequest.message.getHeaderKeyPairValue("Cookie", "SessionId"));
if(sessionId.str() == "")
return createHTTPSession(httpRequest);
else
return findSessionById(sessionId, httpRequest);
}
2022-06-29 11:38:44 -07:00
HTTPSession * HTTPSessions::findSessionById(coreutils::ZString &sessionId, HTTPRequest &httpRequest) {
try {
HTTPSession *httpSession = sessions.at(sessionId);
return httpSession;
}
catch (...) {
return createHTTPSession(httpRequest);
}
}
2022-06-29 11:38:44 -07:00
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;
}
2022-06-29 11:38:44 -07:00
int HTTPSessions::processCommand(std::string command, core::TCPSession *session, std::stringstream &data) {
2019-09-13 10:30:20 -07:00
if(sessions.size() > 0) {
int seq = 0;
for(auto httpSession: sessions)
2019-09-13 10:30:20 -07:00
data << "|" << ++seq << "|" << httpSession.second->getSessionId() << "|" << std::endl;
}
else
data << "There are no sessions active." << std::endl;
2019-09-13 10:30:20 -07:00
return 1;
}
}