From 66b391ce6a5fdf1222c1e2a331122a12787fe105 Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 10 Jul 2013 11:56:10 +0000 Subject: Tidy up around hostname platform lookups --- project2/cgi/cgiAppEngine.cpp | 39 ++++++++++++++++++++++++++++++++++++-- project2/cgi/cgiAppEngine.h | 5 ++++- project2/cgi/cgiRequestContext.cpp | 39 -------------------------------------- project2/cgi/cgiRequestContext.h | 14 -------------- project2/cgi/p2webCgi.cpp | 2 +- project2/cgi/p2webFCgi.cpp | 2 +- project2/cgi/testCgi.cpp | 2 +- 7 files changed, 44 insertions(+), 59 deletions(-) diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 2a82595..8adfb35 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -16,6 +16,7 @@ #include #include #include +#include typedef boost::uuids::uuid SIDKey; typedef std::string SValue; @@ -37,7 +38,29 @@ std::string CgiApplicationEngine::defaultPresenter; SessionContainerPtr CgiApplicationEngine::sessionsContainer; std::string CgiApplicationEngine::sessionCookie; boost::shared_ptr CgiApplicationEngine::router; -boost::intrusive_ptr CgiApplicationEngine::hpi; +CgiApplicationEngine::PlatformHostnameList CgiApplicationEngine::platHosts; + +class PlatformHostnameTarget : public Options::Target { + public: + void + reset() const + { + CgiApplicationEngine::platHosts.clear(); + } + + bool + paramRequired() const + { + return true; + } + + void + consume(const Glib::ustring & p, const VariableType & m, const Options::CurrentPlatform &) const + { + CgiApplicationEngine::platHosts.push_back( + CgiApplicationEngine::PlatformHostname(p, Glib::Regex::create(m, Glib::REGEX_CASELESS | Glib::REGEX_DOTALL))); + } +}; DECLARE_OPTIONS(CgiApplicationEngine, "Project2 CGI options") ("cgi.defaultPresenter", Options::value(&defaultPresenter, "xml"), @@ -68,7 +91,7 @@ DECLARE_OPTIONS(CgiApplicationEngine, "Project2 CGI options") "The module with which to implement session management") ("cgi.sessionCookie", Options::value(&sessionCookie, "sessionID"), "The name of the cookie for storing session IDs") -("cgi.hostRegex", (hpi = new HostnamePlatformIdentifier()), +("cgi.hostRegex", new PlatformHostnameTarget(), "Regular expression used to define a hostname -> platform association") ("cgi.router", Options::function([](const VariableType & r) { router = RouterLoader::getFor(r); }, "simple"), "Implemenation of router model to map request paths to scripts") @@ -309,3 +332,15 @@ CgiApplicationEngine::process(std::ostream & IO, CgiRequestContext * crc) const } } +const Glib::ustring & +CgiApplicationEngine::derivedPlatform(CgiRequestContext * crc) +{ + auto pi = std::find_if(platHosts.begin(), platHosts.end(), [crc](const PlatformHostname & r) -> bool { + return r.second->match(crc->getServerName()); + }); + if (pi == platHosts.end()) { + throw NoSuchPlatform(crc->getServerName()); + } + return pi->first; +} + diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 6d3cd0c..476378a 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -26,11 +26,14 @@ namespace cgicc { class CgiApplicationEngine { public: typedef boost::shared_ptr HttpHeaderPtr; + typedef std::pair> PlatformHostname; + typedef std::vector PlatformHostnameList; CgiApplicationEngine(); virtual ~CgiApplicationEngine(); void process(std::ostream & IO, CgiRequestContext *) const; + static const Glib::ustring & derivedPlatform(CgiRequestContext *); private: void processRun(std::ostream & IO, CgiRequestContext *) const; @@ -156,7 +159,7 @@ class CgiApplicationEngine { }; INITOPTIONS; - static boost::intrusive_ptr hpi; + static PlatformHostnameList platHosts; static boost::shared_ptr router; static SessionContainerPtr sessionsContainer; static std::string sessionCookie; diff --git a/project2/cgi/cgiRequestContext.cpp b/project2/cgi/cgiRequestContext.cpp index 5a93243..d38a256 100644 --- a/project2/cgi/cgiRequestContext.cpp +++ b/project2/cgi/cgiRequestContext.cpp @@ -7,7 +7,6 @@ #include "exceptions.h" #include #include -#include #include #include #include @@ -121,41 +120,3 @@ CgiRequestContext::getParameter(const VariableType & p) const return cgi(p); } -HostnamePlatformIdentifier::HostnamePlatformIdentifier() -{ -} - -HostnamePlatformIdentifier::~HostnamePlatformIdentifier() -{ -} - -const Glib::ustring & -HostnamePlatformIdentifier::derivedPlatform(CgiRequestContext * crc) const -{ - auto pi = std::find_if(platHosts.begin(), platHosts.end(), [crc](const PlatformHostname & r) -> bool { - return r.second->match(crc->getServerName()); - }); - if (pi == platHosts.end()) { - throw NoSuchPlatform(crc->getServerName()); - } - return pi->first; -} - -void -HostnamePlatformIdentifier::reset() const -{ - platHosts.clear(); -} - -bool -HostnamePlatformIdentifier::paramRequired() const -{ - return true; -} - -void -HostnamePlatformIdentifier::consume(const Glib::ustring & p, const VariableType & r, const Options::CurrentPlatform &) const -{ - platHosts.push_back(PlatformHostname(p, Glib::Regex::create(r, Glib::REGEX_CASELESS | Glib::REGEX_DOTALL))); -} - diff --git a/project2/cgi/cgiRequestContext.h b/project2/cgi/cgiRequestContext.h index 1dbc613..57c233d 100644 --- a/project2/cgi/cgiRequestContext.h +++ b/project2/cgi/cgiRequestContext.h @@ -18,20 +18,6 @@ namespace Glib { class Regex; } -class HostnamePlatformIdentifier : public Options::Target { - public: - HostnamePlatformIdentifier(); - virtual ~HostnamePlatformIdentifier(); - void reset() const; - bool paramRequired() const; - void consume(const Glib::ustring &, const VariableType &, const Options::CurrentPlatform &) const; - const Glib::ustring & derivedPlatform(CgiRequestContext *) const; - private: - typedef std::pair> PlatformHostname; - typedef std::vector PlatformHostnameList; - mutable PlatformHostnameList platHosts; -}; - class CgiRequestContext : public ExecContext { public: typedef std::string ETag; diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp index 4f806b2..a8b47e8 100644 --- a/project2/cgi/p2webCgi.cpp +++ b/project2/cgi/p2webCgi.cpp @@ -19,7 +19,7 @@ main(void) CgiApplicationEngine app; GetEnv ge; CgiRequestContext crc(NULL, ge); - OptionsSource::loadSources(boost::bind(&HostnamePlatformIdentifier::derivedPlatform, boost::cref(CgiApplicationEngine::hpi), &crc)); + OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, &crc)); Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); app.process(std::cout, &crc); Plugable::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp index 4320c48..4bc9b2d 100644 --- a/project2/cgi/p2webFCgi.cpp +++ b/project2/cgi/p2webFCgi.cpp @@ -46,7 +46,7 @@ main(void) alarm(0); cgicc::FCgiIO IO(request); CgiRequestContext crc(&IO, IO); - OptionsSource::loadSources(boost::bind(&HostnamePlatformIdentifier::derivedPlatform, boost::cref(CgiApplicationEngine::hpi), &crc)); + OptionsSource::loadSources(boost::bind(&CgiApplicationEngine::derivedPlatform, &crc)); Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); app.process(IO, &crc); FCGX_Finish_r(&request); diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 2025594..6fa8d42 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -26,7 +26,7 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { TestInput(int argc, char ** argv) : crc(NULL, *this) { - auto cp = boost::bind(&HostnamePlatformIdentifier::derivedPlatform, boost::cref(CgiApplicationEngine::hpi), &crc); + auto cp = boost::bind(&CgiApplicationEngine::derivedPlatform, &crc); OptionsSource::loadSources(cp); FileOptions fo(".testCgi.settings"); fo.loadInto(TestConfigConsumer(), cp); -- cgit v1.2.3