diff options
author | randomdan <randomdan@localhost> | 2013-07-10 23:00:44 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2013-07-10 23:00:44 +0000 |
commit | eaf0311ca69dc43c31ed2a290c97ba67efd32c83 (patch) | |
tree | 9ca30f9886554dbf9d71b5f75a933570618cb54b /project2/cgi | |
parent | Tidy up around hostname platform lookups (diff) | |
download | project2-eaf0311ca69dc43c31ed2a290c97ba67efd32c83.tar.bz2 project2-eaf0311ca69dc43c31ed2a290c97ba67efd32c83.tar.xz project2-eaf0311ca69dc43c31ed2a290c97ba67efd32c83.zip |
Central various bits of the CGI start up process
Move the command line arguments option source into a new cli library
Extend testCgi with newly available features
Diffstat (limited to 'project2/cgi')
-rw-r--r-- | project2/cgi/Jamfile.jam | 1 | ||||
-rw-r--r-- | project2/cgi/cgiAppEngine.cpp | 21 | ||||
-rw-r--r-- | project2/cgi/cgiAppEngine.h | 6 | ||||
-rw-r--r-- | project2/cgi/p2webCgi.cpp | 6 | ||||
-rw-r--r-- | project2/cgi/p2webFCgi.cpp | 6 | ||||
-rw-r--r-- | project2/cgi/testCgi.cpp | 60 |
6 files changed, 50 insertions, 50 deletions
diff --git a/project2/cgi/Jamfile.jam b/project2/cgi/Jamfile.jam index 8da2f11..5b8e5a5 100644 --- a/project2/cgi/Jamfile.jam +++ b/project2/cgi/Jamfile.jam @@ -55,5 +55,6 @@ exe testCgi : testCgi.cpp : <library>p2cgicommon + <library>../cli//p2cli <include>../../libmisc ; diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 8adfb35..bd25172 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -1,4 +1,5 @@ #include <pch.hpp> +#include "optionsSource.h" #include "cgiAppEngine.h" #include "cgiResult.h" #include <cgicc/Cgicc.h> @@ -122,7 +123,7 @@ finalTransformSource(TransformSourcePtr ts) } void -CgiApplicationEngine::processRun(std::ostream & IO, CgiRequestContext * crc) const +CgiApplicationEngine::process(std::ostream & IO, CgiRequestContext * crc) const { bool sessionEmpty = crc->getSession()->Empty(); crc->startTime = boost::date_time::microsec_clock<boost::posix_time::ptime>::universal_time(); @@ -308,11 +309,15 @@ doExceptionReporting(const E & e, std::ostream & IO) } void -CgiApplicationEngine::process(std::ostream & IO, CgiRequestContext * crc) const +CgiApplicationEngine::process(std::ostream & IO, cgicc::CgiInput * cgii, const CgiEnvInput & cgienv) const { try { - Logger()->messagef(LOG_DEBUG, "%s: Processing request", __FUNCTION__); - processRun(IO, crc); + boost::function<std::string()> sn = boost::bind(&CgiEnvInput::getenv, &cgienv, "SERVER_NAME"); + OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, sn)); + CgiRequestContext crc(cgii, cgienv); + Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); + Logger()->messagebf(LOG_DEBUG, "%s: Processing request (%s)", __FUNCTION__, crc.getRedirectURL()); + process(IO, &crc); Logger()->messagef(LOG_DEBUG, "%s: Completed request", __FUNCTION__); } catch (const std::exception & e) { @@ -333,13 +338,13 @@ CgiApplicationEngine::process(std::ostream & IO, CgiRequestContext * crc) const } const Glib::ustring & -CgiApplicationEngine::derivedPlatform(CgiRequestContext * crc) +CgiApplicationEngine::derivedPlatform(const boost::function<std::string()> & vhostName) { - auto pi = std::find_if(platHosts.begin(), platHosts.end(), [crc](const PlatformHostname & r) -> bool { - return r.second->match(crc->getServerName()); + auto pi = std::find_if(platHosts.begin(), platHosts.end(), [vhostName](const PlatformHostname & r) -> bool { + return r.second->match(vhostName()); }); if (pi == platHosts.end()) { - throw NoSuchPlatform(crc->getServerName()); + throw NoSuchPlatform(vhostName()); } return pi->first; } diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 476378a..1bc282f 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -32,11 +32,11 @@ class CgiApplicationEngine { CgiApplicationEngine(); virtual ~CgiApplicationEngine(); - void process(std::ostream & IO, CgiRequestContext *) const; - static const Glib::ustring & derivedPlatform(CgiRequestContext *); + void process(std::ostream & IO, cgicc::CgiInput * cgii, const CgiEnvInput & cgienv) const; + static const Glib::ustring & derivedPlatform(const boost::function<std::string()> & vhostName); private: - void processRun(std::ostream & IO, CgiRequestContext *) const; + void process(std::ostream & IO, CgiRequestContext *) const; void addAppData(const MultiRowSetPresenter * p, OutputOptionsPtr o, CgiRequestContext *, bool) const; void addEnvData(const MultiRowSetPresenter * p, OutputOptionsPtr o, CgiRequestContext *, bool) const; diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp index a8b47e8..a4e2328 100644 --- a/project2/cgi/p2webCgi.cpp +++ b/project2/cgi/p2webCgi.cpp @@ -1,5 +1,4 @@ #include "cgiAppEngine.h" -#include "optionsSource.h" #include <boost/bind.hpp> class GetEnv : public CgiEnvInput { @@ -18,10 +17,7 @@ main(void) Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); CgiApplicationEngine app; GetEnv ge; - CgiRequestContext crc(NULL, ge); - OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, &crc)); - Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); - app.process(std::cout, &crc); + app.process(std::cout, NULL, ge); Plugable::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); Plugable::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1)); Plugable::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1)); diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp index 4bc9b2d..0551bf0 100644 --- a/project2/cgi/p2webFCgi.cpp +++ b/project2/cgi/p2webFCgi.cpp @@ -1,6 +1,5 @@ #include "FCgiIO.h" #include "cgiAppEngine.h" -#include "optionsSource.h" #include <boost/bind.hpp> time_t lastPeriodic = 0; @@ -45,10 +44,7 @@ main(void) while (FCGX_Accept_r(&request) == 0) { alarm(0); cgicc::FCgiIO IO(request); - CgiRequestContext crc(&IO, IO); - OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, &crc)); - Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); - app.process(IO, &crc); + app.process(IO, &IO, IO); FCGX_Finish_r(&request); Plugable::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); if (time(NULL) > lastPeriodic + periodicDelay) { diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 6fa8d42..d923d3c 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -4,6 +4,7 @@ #include "options.h" #include "safeMapFind.h" #include "../files/optionsSource.h" +#include "../cli/claOptions.h" #include "cgiRequestContext.h" #include "cgiAppEngine.h" @@ -23,23 +24,11 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { typedef boost::shared_ptr<std::string> StrPtr; typedef std::map<std::string, StrPtr> OptStore; - TestInput(int argc, char ** argv) : - crc(NULL, *this) + TestInput(int argc, char ** argv) { - auto cp = boost::bind(&CgiApplicationEngine::derivedPlatform, &crc); - OptionsSource::loadSources(cp); - FileOptions fo(".testCgi.settings"); - fo.loadInto(TestConfigConsumer(), cp); - if (argc > 1) { - const char * qm = strchr(argv[1], '?'); - if (qm) { - optStore()["REDIRECT_URL"] = StrPtr(new std::string(argv[1], qm - argv[1])); - optStore()["QUERY_STRING"] = StrPtr(new std::string(qm + 1)); - } - else { - optStore()["REDIRECT_URL"] = StrPtr(new std::string(argv[1])); - } - } + Plugable::newLoader<OptionsSource, OptionsSource>("_1", new FileOptions(".testCgi.settings")); + Plugable::newLoader<OptionsSource, OptionsSource>("_2", new CommandLineArguments(argc, argv, + [](const char * url) { urls.push_back(url); })); } std::string getenv(const std::string & varName) const @@ -54,30 +43,44 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { } void run() { + Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); CgiApplicationEngine app; - if (urlListFile.empty()) { - for (int run = 0; run < runCount; run += 1) { - app.process(std::cout, &crc); + boost::function<std::string()> sn = boost::bind(&CgiEnvInput::getenv, this, "SERVER_NAME"); + OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, sn)); + + if (!urlListFile.empty()) { + std::ifstream urlFile(urlListFile); + while (!urlFile.eof()) { + std::string url; + urlFile >> url; + urls.push_back(url); } } - else { - for (int run = 0; run < runCount; run += 1) { - std::ifstream urls(urlListFile); - while (!urls.eof()) { - std::string url; - urls >> url; + + while (runCount-- > 0) { + BOOST_FOREACH(const auto & url, urls) { + int qm = url.find('?'); + if (qm != -1) { + optStore()["REDIRECT_URL"] = StrPtr(new std::string(url.substr(0, qm))); + optStore()["QUERY_STRING"] = StrPtr(new std::string(url.substr(qm + 1))); + } + else { optStore()["REDIRECT_URL"] = StrPtr(new std::string(url)); - app.process(std::cout, &crc); + optStore()["QUERY_STRING"] = StrPtr(new std::string()); } + app.process(std::cout, this, *this); + Plugable::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); } + Plugable::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1)); } + Plugable::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1)); } INITOPTIONS; private: - CgiRequestContext crc; static int runCount; static std::string urlListFile; + static std::vector<std::string> urls; static OptStore & optStore() { static OptStore _optStore; @@ -86,6 +89,7 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { }; int TestInput::runCount; std::string TestInput::urlListFile; +std::vector<std::string> TestInput::urls; DECLARE_OPTIONS(TestInput, "Project2 CGI test options") TESTOPT("SERVER_NAME", "localhost", "FQDN of web service") @@ -97,7 +101,6 @@ TESTOPT("REQUEST_METHOD", "GET", "Request method") TESTOPT("PATH_INFO", "", "Path info") TESTOPT("PATH_TRANSLATED", "", "Path translated") TESTOPT("SCRIPT_NAME", "p2fcgi", "Script name") -TESTOPT("QUERY_STRING", "", "Query string") TESTOPT("REMOTE_HOST", "", "Remote host") TESTOPT("REMOTE_ADDR", "", "Remote address") TESTOPT("AUTH_TYPE", "", "Authentication type") @@ -108,7 +111,6 @@ TESTOPT("CONTENT_LENGTH", "", "Content length") TESTOPT("HTTP_ACCEPT", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accepted MIME types") TESTOPT("HTTP_USER_AGENT", "Mozilla/5.0", "User agent") TESTOPT("REDIRECT_REQUEST", "", "Redirect request") -TESTOPT("REDIRECT_URL", "", "Redirect URL") TESTOPT("REDIRECT_STATUS", "", "Redirect status") TESTOPT("HTTP_REFERER", "", "Referrer") TESTOPT("HTTP_COOKIE", "", "Cookie") |