diff options
author | randomdan <randomdan@localhost> | 2012-06-28 00:04:53 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2012-06-28 00:04:53 +0000 |
commit | 4492980ede8db711a8fa35a87237998cc7307ca5 (patch) | |
tree | 6da80c52d9cd029e1a85d04f17aa4b5025f21552 | |
parent | Fixes picked up by GCC 4.6 (diff) | |
download | project2-4492980ede8db711a8fa35a87237998cc7307ca5.tar.bz2 project2-4492980ede8db711a8fa35a87237998cc7307ca5.tar.xz project2-4492980ede8db711a8fa35a87237998cc7307ca5.zip |
Bit of a tidy up
Add a class interface for getting access to any environment variable not provided by Cgicc
-rw-r--r-- | project2/cgi/FCgiIO.cpp | 79 | ||||
-rw-r--r-- | project2/cgi/FCgiIO.h | 24 | ||||
-rw-r--r-- | project2/cgi/cgiCommon.cpp | 4 | ||||
-rw-r--r-- | project2/cgi/cgiCommon.h | 2 | ||||
-rw-r--r-- | project2/cgi/cgiEnvInput.h | 11 | ||||
-rw-r--r-- | project2/cgi/cgiEnvironment.cpp | 3 | ||||
-rw-r--r-- | project2/cgi/cgiEnvironment.h | 4 | ||||
-rw-r--r-- | project2/cgi/p2webCgi.cpp | 13 | ||||
-rw-r--r-- | project2/cgi/p2webFCgi.cpp | 2 | ||||
-rw-r--r-- | project2/cgi/testCgi.cpp | 9 |
10 files changed, 100 insertions, 51 deletions
diff --git a/project2/cgi/FCgiIO.cpp b/project2/cgi/FCgiIO.cpp index 8d52a77..e47b62e 100644 --- a/project2/cgi/FCgiIO.cpp +++ b/project2/cgi/FCgiIO.cpp @@ -28,37 +28,66 @@ #include <iostream> #include <stdexcept> #include <cstdlib> +#include "safeMapFind.h" #include "FCgiIO.h" -cgicc::FCgiIO::FCgiIO(FCGX_Request& request) - : std::ostream(&fOutBuf), - fRequest(request), - fOutBuf(request.out), - fErrBuf(request.err), - fErr(&fErrBuf) +cgicc::FCgiIO::FCgiIO(FCGX_Request& request) : + std::ostream(&fOutBuf), + fRequest(request), + fOutBuf(request.out), + fErrBuf(request.err), + fErr(&fErrBuf) { - rdbuf(&fOutBuf); - fErr.rdbuf(&fErrBuf); + rdbuf(&fOutBuf); + fErr.rdbuf(&fErrBuf); - // Parse environment - for(char **e = fRequest.envp; *e != NULL; ++e) { - std::string s(*e); - std::string::size_type i = s.find('='); - if(i == std::string::npos) - throw std::runtime_error("Illegally formed environment"); - fEnv[s.substr(0, i)] = s.substr(i + 1); - } + // Parse environment + for(char **e = fRequest.envp; *e != NULL; ++e) { + std::string s(*e); + std::string::size_type i = s.find('='); + if(i == std::string::npos) + throw std::runtime_error("Illegally formed environment"); + fEnv[s.substr(0, i)] = s.substr(i + 1); + } } -cgicc::FCgiIO::FCgiIO(const FCgiIO& io) - : std::ios(), - CgiInput(io), - std::ostream(&fOutBuf), - fRequest(io.fRequest), - fErr(&fErrBuf), - fEnv(io.fEnv) +cgicc::FCgiIO::FCgiIO(const FCgiIO& io) : + std::ios(), + CgiInput(io), + std::ostream(&fOutBuf), + fRequest(io.fRequest), + fErr(&fErrBuf), + fEnv(io.fEnv) { - rdbuf(&fOutBuf); - fErr.rdbuf(&fErrBuf); + rdbuf(&fOutBuf); + fErr.rdbuf(&fErrBuf); +} + +cgicc::FCgiIO::~FCgiIO() +{ +} + +size_t +cgicc::FCgiIO::read(char *data, size_t length) +{ + return FCGX_GetStr(data, length, fRequest.in); +} + +std::string +cgicc::FCgiIO::getenv(const char *varName) +{ + return fEnv[varName]; +} + +std::string +cgicc::FCgiIO::getenv(const std::string & varName) const +{ + return defaultMapFind(fEnv, varName, ""); +} + +std::ostream & +cgicc::FCgiIO::err() +{ + return fErr; } diff --git a/project2/cgi/FCgiIO.h b/project2/cgi/FCgiIO.h index a8b67f3..7811fac 100644 --- a/project2/cgi/FCgiIO.h +++ b/project2/cgi/FCgiIO.h @@ -45,7 +45,7 @@ #include <map> #include "fcgio.h" - +#include "cgiEnvInput.h" #include "cgicc/CgiInput.h" namespace cgicc { @@ -64,7 +64,7 @@ namespace cgicc { * It also provides access to the request's output and error streams, using a * similar interface. */ - class CGICC_API FCgiIO : public cgicc::CgiInput, public std::ostream + class CGICC_API FCgiIO : public cgicc::CgiInput, public CgiEnvInput, public std::ostream { public: @@ -91,9 +91,7 @@ namespace cgicc { * * Delete this FCgiIO object */ - virtual inline - ~FCgiIO() - {} + virtual ~FCgiIO(); //@} // ============================================================ @@ -108,10 +106,7 @@ namespace cgicc { * \param length The number of characters to read * \return The number of characters read */ - virtual inline size_t read(char *data, size_t length) - { - return FCGX_GetStr(data, length, fRequest.in); - } + virtual size_t read(char *data, size_t length); /*! * \brief Query the value of an environment variable stored in the request. @@ -120,10 +115,8 @@ namespace cgicc { * \return The value of the requested environment variable, or an empty * string if not found. */ - virtual inline std::string getenv(const char *varName) - { - return fEnv[varName]; - } + virtual std::string getenv(const char *varName); + virtual std::string getenv(const std::string & varName) const; //@} // ============================================================ @@ -134,10 +127,7 @@ namespace cgicc { /*! * \brief Provides access to the error stream. */ - inline std::ostream& err(void) - { - return fErr; - } + std::ostream& err(); //@} protected: diff --git a/project2/cgi/cgiCommon.cpp b/project2/cgi/cgiCommon.cpp index ac3e6d4..60a9abf 100644 --- a/project2/cgi/cgiCommon.cpp +++ b/project2/cgi/cgiCommon.cpp @@ -37,10 +37,10 @@ doExceptionReporting(const E & e, std::ostream & IO) } void -cgiServe(cgicc::CgiInput * i, CgiEnvironment * env, std::ostream & IO) +cgiServe(cgicc::CgiInput * i, CgiEnvironment * env, std::ostream & IO, const CgiEnvInput * e) { cgicc::Cgicc cgi(i); - env->setCGICC(&cgi); + env->setCGICC(&cgi, e); env->init(); try { CgiApplicationEngine app(env, IO); diff --git a/project2/cgi/cgiCommon.h b/project2/cgi/cgiCommon.h index ce6108e..42a88e6 100644 --- a/project2/cgi/cgiCommon.h +++ b/project2/cgi/cgiCommon.h @@ -2,5 +2,5 @@ #include <cgicc/Cgicc.h> #include "cgiEnvironment.h" -void cgiServe(cgicc::CgiInput * i, CgiEnvironment *, std::ostream & o); +void cgiServe(cgicc::CgiInput * i, CgiEnvironment *, std::ostream & o, const CgiEnvInput * e); diff --git a/project2/cgi/cgiEnvInput.h b/project2/cgi/cgiEnvInput.h new file mode 100644 index 0000000..2704fd5 --- /dev/null +++ b/project2/cgi/cgiEnvInput.h @@ -0,0 +1,11 @@ +#ifndef CGIENVINPUT +#define CGIENVINPUT + +#include <string> + +class CgiEnvInput { + public: + virtual std::string getenv(const std::string & varName) const = 0; +}; + +#endif diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp index babeeea..a5d4af2 100644 --- a/project2/cgi/cgiEnvironment.cpp +++ b/project2/cgi/cgiEnvironment.cpp @@ -65,9 +65,10 @@ CgiEnvironment::~CgiEnvironment() } void -CgiEnvironment::setCGICC(const cgicc::Cgicc * c) +CgiEnvironment::setCGICC(const cgicc::Cgicc * c, const CgiEnvInput * e) { cgi = c; + cgienv = e; elems = makeVector(boost::filesystem::path(getRedirectURL())); } diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h index a2867be..be6732c 100644 --- a/project2/cgi/cgiEnvironment.h +++ b/project2/cgi/cgiEnvironment.h @@ -4,6 +4,7 @@ #include <string> #include <vector> #include "environment.h" +#include "cgiEnvInput.h" #include <cgicc/CgiEnvironment.h> SimpleMessageException(NoSuchCookie); @@ -38,8 +39,9 @@ class CgiEnvironment : public Environment { std::string getRequestMethod() const; std::string getCookieValue(const std::string & name) const; - void setCGICC(const cgicc::Cgicc *); + void setCGICC(const cgicc::Cgicc *, const CgiEnvInput * cgienv); const cgicc::Cgicc * cgi; + const CgiEnvInput * cgienv; std::vector<std::string> elems; private: diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp index 646d08b..80b5a98 100644 --- a/project2/cgi/p2webCgi.cpp +++ b/project2/cgi/p2webCgi.cpp @@ -2,12 +2,23 @@ #include "scriptLoader.h" #include <boost/bind.hpp> +class GetEnv : public CgiEnvInput { + public: + std::string + getenv(const std::string & varName) const + { + const char * env = ::getenv(varName.c_str()); + return env ? env : ""; + } +}; + int main(void) { LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); CgiEnvironment env; - cgiServe(NULL, &env, std::cout); + GetEnv ge; + cgiServe(NULL, &env, std::cout, &ge); LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onPeriodic, _1)); LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1)); diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp index a1786d2..7dac2cc 100644 --- a/project2/cgi/p2webFCgi.cpp +++ b/project2/cgi/p2webFCgi.cpp @@ -45,7 +45,7 @@ main(void) while (FCGX_Accept_r(&request) == 0) { alarm(0); cgicc::FCgiIO IO(request); - cgiServe(&IO, &env, IO); + cgiServe(&IO, &env, IO, &IO); alarm(60); LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onIteration, _1)); if (time(NULL) > lastPeriodic + periodicDelay) { diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 1d3f72c..8a4ccb6 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -8,7 +8,7 @@ #define TESTOPT(name, def, desc) \ (name, Options::value(optStore.insert(OptStore::value_type(name, StrPtr(new std::string()))).first->second.get(), def), desc) -class TestInput : public cgicc::CgiInput { +class TestInput : public cgicc::CgiInput, public CgiEnvInput { public: class TestConfigConsumer : public ConfigConsumer { public: @@ -67,6 +67,11 @@ class TestInput : public cgicc::CgiInput { return &opts; } + std::string getenv(const std::string & varName) const + { + StrPtr def(new std::string()); + return *defaultMapFind(optStore, varName, def); + } virtual std::string getenv(const char * varName) { StrPtr def(new std::string()); @@ -75,7 +80,7 @@ class TestInput : public cgicc::CgiInput { void run() { for (int run = 0; run < runCount; run += 1) { - cgiServe(this, &env, std::cout); + cgiServe(this, &env, std::cout, this); } } |