From 748df89a3c10cfa64d0474ba9be4316e6fc35375 Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 13 Feb 2012 13:04:14 +0000 Subject: Lazy session ID creation --- project2/cgi/cgiAppEngine.cpp | 6 +++--- project2/common/session.cpp | 15 ++++++++++++++- project2/common/session.h | 7 +++++-- project2/common/sessionContainer.cpp | 2 +- project2/json/couchSession.cpp | 2 +- project2/xml/sessionXml.cpp | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 9e91800..c08eb3e 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -135,7 +135,7 @@ CgiApplicationEngine::process() const HttpHeaderPtr header = rs->getHeader(); if (!sessionEmpty || !cursession->Empty()) { sessionsContainer->SaveSession(cursession); - header->setCookie(cgicc::HTTPCookie(SESSIONID, cursession->ID.str(), "Session ID", + header->setCookie(cgicc::HTTPCookie(SESSIONID, cursession->ID().str(), "Session ID", _env->getServerName().substr(_env->getServerName().find(".")), env()->sessionTimeOut, "/", false)); } if (TransformSourcePtr ts = currentStage.get<2>()) { @@ -231,10 +231,10 @@ CgiApplicationEngine::addAppData(const MultiRowSetPresenter * p, OutputOptionsPt if (!o || o->Core()) { addCoreAppData(p); } - if (!outputCachingActive && (!o || o->Session())) { + if (!outputCachingActive && (!cursession->Empty()) && (!o || o->Session())) { // Sessions variables p->addNewRowSet("session", env()->scriptNamespacePrefix); - p->addAttribute("id", cursession->ID.str()); + p->addAttribute("id", cursession->ID().str()); cursession->ForeachValue(boost::bind(&CgiApplicationEngine::addVarToPresenter, this, p, _1, _2)); p->finishRowSet(); } diff --git a/project2/common/session.cpp b/project2/common/session.cpp index fd8f569..66d93a5 100644 --- a/project2/common/session.cpp +++ b/project2/common/session.cpp @@ -2,8 +2,12 @@ #include "session.h" #include "safeMapFind.h" +Session::Session() +{ +} + Session::Session(const UUID & sid) : - ID(sid) + id(sid) { } @@ -55,3 +59,12 @@ Session::ForeachValue(const Session::SVH & svh) const } } +const UUID & +Session::ID() const +{ + if (id.is_nil()) { + id = UUID::generate_random(); + } + return id; +} + diff --git a/project2/common/session.h b/project2/common/session.h index 267dc51..e1f0b24 100644 --- a/project2/common/session.h +++ b/project2/common/session.h @@ -17,6 +17,7 @@ class Session : public virtual IntrusivePtrBase { typedef std::map Values; typedef boost::function2 SVH; + Session(); Session(const UUID & id); virtual ~Session(); @@ -26,8 +27,7 @@ class Session : public virtual IntrusivePtrBase { void ClearValue(const Glib::ustring & name); bool Empty() const; time_t ExpiryTime() const; - - const UUID ID; + const UUID & ID() const; protected: void ExpiryTime(time_t); @@ -35,6 +35,9 @@ class Session : public virtual IntrusivePtrBase { Values vars; time_t expires; + + private: + mutable UUID id; }; typedef boost::intrusive_ptr SessionPtr; diff --git a/project2/common/sessionContainer.cpp b/project2/common/sessionContainer.cpp index f4d7cd4..ed36fb3 100644 --- a/project2/common/sessionContainer.cpp +++ b/project2/common/sessionContainer.cpp @@ -15,7 +15,7 @@ SessionContainer::GetSession(const UUID & id) const { SessionPtr s; if (id.is_nil() || !(s = getSession(id))) { - s = new Session(UUID::generate_random()); + s = new Session(); } s->ExpiryTime(time(NULL) + Environment::getCurrent()->sessionTimeOut); return s; diff --git a/project2/json/couchSession.cpp b/project2/json/couchSession.cpp index f810486..5d964aa 100644 --- a/project2/json/couchSession.cpp +++ b/project2/json/couchSession.cpp @@ -46,7 +46,7 @@ class CouchSessionContainer : public SessionContainer { c->setopt(CURLOPT_INFILESIZE_LARGE, (curl_off_t)out.size()); unsigned int off = 0; BOOST_FOREACH(const std::string & b, baseUrls) { - c->setopt(CURLOPT_URL, (b + s->ID.str()).c_str()); + c->setopt(CURLOPT_URL, (b + s->ID().str()).c_str()); try { c->performSend(boost::bind(send, &out, &off, _1, _2)); return; diff --git a/project2/xml/sessionXml.cpp b/project2/xml/sessionXml.cpp index b66a812..a3455f6 100644 --- a/project2/xml/sessionXml.cpp +++ b/project2/xml/sessionXml.cpp @@ -70,7 +70,7 @@ SessionContainerXml::SaveSession(SessionPtr currentSession) const xmlpp::Element * sess = d.create_root_node("session"); sess->set_attribute("expires", boost::lexical_cast(currentSession->ExpiryTime())); currentSession->ForeachValue(boost::bind(appendToXmlNode, sess, _1, _2)); - boost::filesystem::path p = xmlDir / currentSession->ID.str(); + boost::filesystem::path p = xmlDir / currentSession->ID().str(); d.write_to_file(p.string()); } -- cgit v1.2.3