#include "HTTPSessions.h" #include "HTTPSession.h" #include "Log.h" #include namespace http { HTTPSession * HTTPSessions::findSessionByHeader(core::Header &header, core::Response &response) { std::string sessionId = header.getCookie("sessionId"); HTTPSession *session = findSessionById(sessionId, response); return session; } HTTPSession * HTTPSessions::findSessionById(std::string sessionId, core::Response &response) { HTTPSession *httpSession; if(sessionId.length() > 0) { httpSession = (HTTPSession *)sessions.find(sessionId)->second; } else { httpSession = createHTTPSession(); response.setCookie("sessionId", httpSession->getSessionId()); } return httpSession; } HTTPSession * HTTPSessions::createHTTPSession() { HTTPSession *httpSession = new HTTPSession(generateSessionId()); sessions.insert(std::make_pair(httpSession->getSessionId(), httpSession)); return httpSession; } std::string HTTPSessions::generateSessionId() { uuid_t uuid; uuid_generate_random(uuid); char uuid_s[37]; uuid_unparse(uuid, uuid_s); return uuid_s; } }