• Main Page
  • Classes
  • Files
  • File List
  • File Members

lib/bfHttpServer/src/httpsessionstore.cpp

Go to the documentation of this file.
00001 
00006 #include "httpsessionstore.h"
00007 #include <QDateTime>
00008 #include <QUuid>
00009 
00010 HttpSessionStore::HttpSessionStore(QSettings* settings, QObject* parent)
00011     :QObject(parent)
00012 {
00013     this->settings=settings;
00014     connect(&cleanupTimer,SIGNAL(timeout()),this,SLOT(timerEvent()));
00015     cleanupTimer.start(60000);
00016     cookieName=settings->value("cookieName","sessionid").toByteArray();
00017     expirationTime=settings->value("expirationTime",3600000).toInt();
00018     qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
00019 }
00020 
00021 HttpSessionStore::~HttpSessionStore()
00022 {
00023     cleanupTimer.stop();
00024 }
00025 
00026 QByteArray HttpSessionStore::getSessionId(HttpRequest& request, HttpResponse& response) {
00027     // The session ID in the response has priority because this one will be used in the next request.
00028     mutex.lock();
00029     // Get the session ID from the response cookie
00030     QByteArray sessionId=response.getCookies().value(cookieName).getValue();
00031     if (sessionId.isEmpty()) {
00032         // Get the session ID from the request cookie
00033         sessionId=request.getCookie(cookieName);
00034     }
00035     // Clear the session ID if there is no such session in the storage.
00036     if (!sessionId.isEmpty()) {
00037         if (!sessions.contains(sessionId)) {
00038             qDebug("HttpSessionStore: received invalid session cookie with ID %s",sessionId.data());
00039             sessionId.clear();
00040         }
00041     }
00042     mutex.unlock();
00043     return sessionId;
00044 }
00045 
00046 HttpSession HttpSessionStore::getSession(HttpRequest& request, HttpResponse& response, bool allowCreate) {
00047     QByteArray sessionId=getSessionId(request,response);
00048     mutex.lock();
00049     if (!sessionId.isEmpty()) {
00050         HttpSession session=sessions.value(sessionId);
00051         if (!session.isNull()) {
00052             mutex.unlock();
00053             session.setLastAccess();
00054             return session;
00055         }
00056     }
00057     // Need to create a new session
00058     if (allowCreate) {
00059         QByteArray cookieName=settings->value("cookieName","sessionid").toByteArray();
00060         QByteArray cookiePath=settings->value("cookiePath").toByteArray();
00061         QByteArray cookieComment=settings->value("cookieComment").toByteArray();
00062         QByteArray cookieDomain=settings->value("cookieDomain").toByteArray();
00063         HttpSession session(true);
00064         qDebug("HttpSessionStore: create new session with ID %s",session.getId().data());
00065         sessions.insert(session.getId(),session);
00066         response.setCookie(HttpCookie(cookieName,session.getId(),expirationTime/1000,cookiePath,cookieComment,cookieDomain));
00067         mutex.unlock();
00068         return session;
00069     }
00070     // Return a null session
00071     mutex.unlock();
00072     return HttpSession();
00073 }
00074 
00075 HttpSession HttpSessionStore::getSession(const QByteArray id) {
00076     mutex.lock();
00077     HttpSession session=sessions.value(id);
00078     mutex.unlock();
00079     session.setLastAccess();
00080     return session;
00081 }
00082 
00083 void HttpSessionStore::timerEvent() {
00084     // Todo: find a way to delete sessions only if no controller is accessing them
00085     mutex.lock();
00086     qint64 now=QDateTime::currentMSecsSinceEpoch();
00087     QMap<QByteArray,HttpSession>::iterator i = sessions.begin();
00088     while (i != sessions.end()) {
00089         QMap<QByteArray,HttpSession>::iterator prev = i;
00090         ++i;
00091         HttpSession session=prev.value();
00092         qint64 lastAccess=session.getLastAccess();
00093         if (now-lastAccess>expirationTime) {
00094             qDebug("HttpSessionStore: session %s expired",session.getId().data());
00095             sessions.erase(prev);
00096         }
00097     }
00098     mutex.unlock();
00099 }
00100 
00101 
00103 void HttpSessionStore::removeSession(HttpSession session) {
00104     mutex.lock();
00105     sessions.remove(session.getId());
00106     mutex.unlock();
00107 }

Generated on Mon Dec 26 2011 12:09:22 for QtWebApp by  doxygen 1.7.1