diff options
author | randomdan <randomdan@localhost> | 2011-09-06 14:06:44 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-09-06 14:06:44 +0000 |
commit | 55c3def82c32eaebb0feadebbcadd986d8f51941 (patch) | |
tree | b55b4edeac7882b419344b1845e8338b8df947e1 /project2/cgi | |
parent | Minor shuffle (diff) | |
download | project2-55c3def82c32eaebb0feadebbcadd986d8f51941.tar.bz2 project2-55c3def82c32eaebb0feadebbcadd986d8f51941.tar.xz project2-55c3def82c32eaebb0feadebbcadd986d8f51941.zip |
More flexible Curl helpers with boost::bind
Remove lots of duplicate code with safeMapFind
Rework the session code into something slightly more sensible
Add a doobry for forcing a CGI error
Diffstat (limited to 'project2/cgi')
-rw-r--r-- | project2/cgi/cgiAppEngine.cpp | 43 | ||||
-rw-r--r-- | project2/cgi/cgiAppEngine.h | 2 | ||||
-rw-r--r-- | project2/cgi/cgiStageFail.cpp | 52 |
3 files changed, 76 insertions, 21 deletions
diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 2c4feb0..877f9a6 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -17,17 +17,23 @@ typedef std::string SValue; SimpleMessageException(UnknownDomain); +static UUID +getSessionID(const std::vector<cgicc::HTTPCookie> & cookies) { + BOOST_FOREACH(const cgicc::HTTPCookie & c, cookies) { + if (c.getName() == SESSIONID) { + return c.getValue(); + } + } + return UUID(); +} CgiApplicationEngine::CgiApplicationEngine(const CgiEnvironment * e, std::ostream & io) : ApplicationEngine("web/host"), _env(e), sessionsContainer(LoaderBase::getLoader<SessionContainerLoader, NotSupported>(e->sessionModule)->open()), IO(io) { - BOOST_FOREACH(const cgicc::HTTPCookie c, e->getCookieList()) { - if (c.getName() == SESSIONID) { - sessionID = c.getValue(); - } - } + UUID sessID = getSessionID(e->getCookieList()); + cursession = sessionsContainer->GetSession(sessID); currentStage = NextStage(new InitialStage(e)); } @@ -80,10 +86,8 @@ CgiApplicationEngine::process() const addEnvData(currentStage.get<3>().get()); } HttpHeaderPtr header = rs->getHeader(); - if (!sessionID.is_nil()) { - header->setCookie(cgicc::HTTPCookie(SESSIONID, sessionID.str(), "Session ID", - _env->getServerName().substr(_env->getServerName().find(".")), env()->sessionTimeOut, "/", false)); - } + header->setCookie(cgicc::HTTPCookie(SESSIONID, cursession->ID.str(), "Session ID", + _env->getServerName().substr(_env->getServerName().find(".")), env()->sessionTimeOut, "/", false)); header->render(IO); if (currentStage.get<2>()) { TransformSourcePtr ts = currentStage.get<2>(); @@ -98,6 +102,7 @@ CgiApplicationEngine::process() const delete ddd; } } + sessionsContainer->SaveSession(cursession); } CgiApplicationEngine::Stage::Stage(const CgiEnvironment * env) : @@ -139,18 +144,16 @@ CgiApplicationEngine::addAppData(const Presenter * p) const { addCoreAppData(p); // Sessions variables - if (!sessionID.is_nil()) { - p->pushSub("session", env()->xmlPrefix); - p->addField("id", sessionID.str()); - Session::Values session(sessionsContainer->GetSession(sessionID)->GetValuesCopy()); - BOOST_FOREACH(Session::Values::value_type sv, session) { - p->pushSub("var", env()->xmlPrefix); - p->addAttr("value", sv.second); - p->addAttr("name", sv.first); - p->popSub(); - } + p->pushSub("session", env()->xmlPrefix); + p->addField("id", cursession->ID.str()); + Session::Values session(cursession->GetValuesCopy()); + BOOST_FOREACH(Session::Values::value_type sv, session) { + p->pushSub("var", env()->xmlPrefix); + p->addAttr("value", sv.second); + p->addAttr("name", sv.first); p->popSub(); } + p->popSub(); // Timing info p->pushSub("timing", env()->xmlPrefix); @@ -165,7 +168,7 @@ CgiApplicationEngine::addAppData(const Presenter * p) const SessionPtr CgiApplicationEngine::session() const { - return sessionsContainer->GetSession(sessionID); + return cursession; } bool diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 0f534e8..6e202e4 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -143,7 +143,7 @@ class CgiApplicationEngine : public ApplicationEngine, public TransformChainLink private: mutable NextStage currentStage; - mutable UUID sessionID; + SessionPtr cursession; mutable std::ostream & IO; }; diff --git a/project2/cgi/cgiStageFail.cpp b/project2/cgi/cgiStageFail.cpp new file mode 100644 index 0000000..04e131b --- /dev/null +++ b/project2/cgi/cgiStageFail.cpp @@ -0,0 +1,52 @@ +#include <pch.hpp> +#include "cgiAppEngine.h" +#include "cgiEnvironment.h" +#include "cgiHttpHeader.h" +#include "logger.h" + +namespace CgiApplicationExtras { + class FailStage : public CgiApplicationEngine::ResponseStage { + public: + FailStage(const CgiEnvironment * env, int c, const std::string & m) : + ResponseStage(env), + code(c), + message(m) { + } + + CgiApplicationEngine::HttpHeaderPtr getHeader() const + { + Project2HttpHeader * header = new Project2HttpHeader(boost::lexical_cast<std::string>(code) + " " + message); + return CgiApplicationEngine::HttpHeaderPtr(header); + } + + CgiApplicationEngine::NextStage run() + { + return CgiApplicationEngine::NextStage(NULL, this, NULL, NULL); + } + private: + const int code; + const std::string message; + }; + + class CgiFail : public View { + public: + CgiFail(const xmlpp::Element * e) : + SourceObject(e), + View(e), + code(e, "code", false, 500), + message(e, "message", false, "Application error") { + } + void loadComplete(const CommonObjects *) { + } + void execute(const Presenter *) const { + throw CgiApplicationEngine::ResponseStagePtr( + new FailStage(dynamic_cast<const CgiEnvironment *>(Environment::getCurrent()), code(), message())); + } + private: + Variable code, message; + }; +} + +typedef CgiApplicationExtras::CgiFail cgif; +DECLARE_LOADER("cgifail", cgif); + |