41 lines
1.3 KiB
C++
41 lines
1.3 KiB
C++
#include "HTTPSessions.h"
|
|
#include "HTTPSession.h"
|
|
#include "Log.h"
|
|
#include <uuid/uuid.h>
|
|
|
|
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) {
|
|
core::Log(core::LOG_DEBUG_1) << "Session Id: " << sessionId << ":" << sessionId.length();
|
|
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;
|
|
}
|
|
|
|
}
|