diff options
author | randomdan <randomdan@localhost> | 2010-12-22 00:38:31 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2010-12-22 00:38:31 +0000 |
commit | 17e093f159821c6a106421e5a08d3b8bbcbc05e7 (patch) | |
tree | 08e943263818d49933593554f7eeba40845918a8 | |
parent | Add missing execute on custom SQL merge inserter (diff) | |
download | project2-17e093f159821c6a106421e5a08d3b8bbcbc05e7.tar.bz2 project2-17e093f159821c6a106421e5a08d3b8bbcbc05e7.tar.xz project2-17e093f159821c6a106421e5a08d3b8bbcbc05e7.zip |
Split the plain CGI and the FastCGI variants into separate programs with a now identical shared core implementation
-rw-r--r-- | project2/Jamfile.jam | 24 | ||||
-rw-r--r-- | project2/cgi/cgiCommon.cpp | 65 | ||||
-rw-r--r-- | project2/cgi/cgiCommon.h | 5 | ||||
-rw-r--r-- | project2/cgi/cgiEnvironment.cpp | 1 | ||||
-rw-r--r-- | project2/cgi/p2webCgi.cpp | 12 | ||||
-rw-r--r-- | project2/cgi/p2webFCgi.cpp | 59 | ||||
-rw-r--r-- | project2/cgi/p2webMain.cpp | 119 |
7 files changed, 163 insertions, 122 deletions
diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 640fa39..a707a05 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -92,9 +92,15 @@ lib p2mail : <library>esmtp ; -exe p2web : - [ glob cgi/*.cpp ] : +lib p2web : + [ glob cgi/cgi*.cpp ] : <library>p2common + <library>cgicc + ; + +exe p2cgi : + cgi/p2webCgi.cpp : + <library>p2web <library>p2url <library>p2sql <library>p2mail @@ -103,9 +109,21 @@ exe p2web : <library>p2processes <library>p2xml <library>p2xmlSession + ; + +exe p2fcgi : + cgi/p2webFCgi.cpp cgi/FCgiIO.cpp : + <library>p2web <library>fcgi++ <library>fcgi - <library>cgicc + <library>p2url + <library>p2sql + <library>p2mail + <library>p2files + <library>p2regex + <library>p2processes + <library>p2xml + <library>p2xmlSession ; exe p2console : diff --git a/project2/cgi/cgiCommon.cpp b/project2/cgi/cgiCommon.cpp new file mode 100644 index 0000000..bfbec36 --- /dev/null +++ b/project2/cgi/cgiCommon.cpp @@ -0,0 +1,65 @@ +#include "cgiCommon.h" +#include <libxml/tree.h> +#include <glibmm/exception.h> +#include <cgicc/CgiEnvironment.h> +#include <cgicc/HTTPContentHeader.h> +#include <cgicc/HTTPStatusHeader.h> +#include <fcgi_stdio.h> +#include "cgiEnvironment.h" +#include "cgiAppEngine.h" +#include <boost/bind.hpp> +#include <cxxabi.h> + +static +int +xmlWrite(void * _out, const char * buf, int len) +{ + std::ostream * IO = static_cast<std::ostream*>(_out); + IO->write(buf, len); + return len; +} + +void +cgiServe(cgicc::CgiInput * i, std::ostream & IO) +{ + try { + cgicc::Cgicc cgi(i); + CgiEnvironment env(&cgi); + CgiApplicationEngine app(&env); + app.process(); + IO << "Cache-control: no-cache" << std::endl; + app.getHeader()->render(IO); + xmlOutputBufferPtr out = xmlOutputBufferCreateIO( + xmlWrite, NULL, &IO, xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8)); + app.write(boost::bind(xmlSaveFileTo, out, _1, "utf-8")); + } + catch (const std::exception & e) { + cgicc::HTTPStatusHeader header(500, e.what()); + header.render(IO); + char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); + IO << "Kaboom!" << std::endl + << std::endl + << buf << std::endl + << e.what() << std::endl; + free(buf); + } + catch (const Glib::Exception & e) { + cgicc::HTTPStatusHeader header(500, e.what()); + header.render(IO); + char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); + IO << "Kaboom!" << std::endl + << std::endl + << buf << std::endl + << e.what() << std::endl; + free(buf); + } + catch (...) { + cgicc::HTTPStatusHeader header(500, "Unknown exception"); + header.render(IO); + IO << "Kaboom!" << std::endl + << std::endl + << "Unknown exception." << std::endl; + throw; + } +} + diff --git a/project2/cgi/cgiCommon.h b/project2/cgi/cgiCommon.h new file mode 100644 index 0000000..489e3fb --- /dev/null +++ b/project2/cgi/cgiCommon.h @@ -0,0 +1,5 @@ +#include <ostream> +#include <cgicc/Cgicc.h> + +void cgiServe(cgicc::CgiInput * i, std::ostream & o); + diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp index 3461973..e541f7f 100644 --- a/project2/cgi/cgiEnvironment.cpp +++ b/project2/cgi/cgiEnvironment.cpp @@ -2,6 +2,7 @@ #include "../appEngine.h" #include "../exceptions.h" #include <map> +#include <syslog.h> #include <cgicc/Cgicc.h> #include <boost/tokenizer.hpp> diff --git a/project2/cgi/p2webCgi.cpp b/project2/cgi/p2webCgi.cpp new file mode 100644 index 0000000..f10573f --- /dev/null +++ b/project2/cgi/p2webCgi.cpp @@ -0,0 +1,12 @@ +#include "cgiCommon.h" +#include "../xmlObjectLoader.h" + +int +main(void) +{ + cgiServe(NULL, std::cout); + LoaderBase::onIteration(); + LoaderBase::onPeriodic(); + LoaderBase::onIdle(); +} + diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp new file mode 100644 index 0000000..620c441 --- /dev/null +++ b/project2/cgi/p2webFCgi.cpp @@ -0,0 +1,59 @@ +#include <syslog.h> +#include "cgiCommon.h" +#include "FCgiIO.h" +#include "../xmlObjectLoader.h" + +time_t lastPeriodic = 0; +time_t periodicDelay = 600; + +static +void +p2webPeriodic() +{ + time(&lastPeriodic); + LoaderBase::onPeriodic(); +} + +static +void +p2webGoingIdle(int) +{ + if (time(NULL) > lastPeriodic + periodicDelay) { + p2webPeriodic(); + } + LoaderBase::onIdle(); +} + +int +main(void) +{ + if (!FCGX_IsCGI()) { + syslog(LOG_NOTICE, "FCGID Startup ($Id$)"); + FCGX_Request request; + + FCGX_Init(); + FCGX_InitRequest(&request, 0, 0); + + struct sigaction onAlarm; + onAlarm.sa_handler = &p2webGoingIdle; + onAlarm.sa_flags = 0; + if (sigaction(SIGALRM, &onAlarm, NULL)) { + syslog(LOG_WARNING, "Failed to set signal handler"); + } + alarm(60); + while (FCGX_Accept_r(&request) == 0) { + alarm(0); + cgicc::FCgiIO IO(request); + cgiServe(&IO, IO); + alarm(60); + LoaderBase::onIteration(); + if (time(NULL) > lastPeriodic + periodicDelay) { + p2webPeriodic(); + } + } + } + else { + syslog(LOG_ERR, "FCGID not running as a FastCGI program"); + } + return 0; +} diff --git a/project2/cgi/p2webMain.cpp b/project2/cgi/p2webMain.cpp deleted file mode 100644 index bce54fa..0000000 --- a/project2/cgi/p2webMain.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include <libxml/tree.h> -#include <glibmm/exception.h> -#include <cgicc/Cgicc.h> -#include <cgicc/CgiEnvironment.h> -#include <cgicc/HTTPContentHeader.h> -#include <cgicc/HTTPStatusHeader.h> -FILE * realstdout = stdout; -#include <fcgi_stdio.h> -#include "cgiEnvironment.h" -#include "cgiAppEngine.h" -#include <boost/bind.hpp> -#include <syslog.h> -#include "FCgiIO.h" -#include <cxxabi.h> - -int -xmlWrite(void * _out, const char * buf, int len) -{ - cgicc::FCgiIO & IO = *((cgicc::FCgiIO*)_out); - IO.write(buf, len); - return len; -} - -time_t lastPeriodic = 0; -time_t periodicDelay = 600; - -void -p2webPeriodic() -{ - time(&lastPeriodic); - LoaderBase::onPeriodic(); -} - -void -p2webGoingIdle(int) -{ - if (time(NULL) > lastPeriodic + periodicDelay) { - p2webPeriodic(); - } - LoaderBase::onIdle(); -} - -int main(void) -{ - if (!FCGX_IsCGI()) { - syslog(LOG_NOTICE, "FCGID Startup ($Id$)"); - FCGX_Request request; - - FCGX_Init(); - FCGX_InitRequest(&request, 0, 0); - - struct sigaction onAlarm; - onAlarm.sa_handler = &p2webGoingIdle; - onAlarm.sa_flags = 0; - if (sigaction(SIGALRM, &onAlarm, NULL)) { - syslog(LOG_WARNING, "Failed to set signal handler"); - } - alarm(60); - while (FCGX_Accept_r(&request) == 0) { - alarm(0); - cgicc::FCgiIO IO(request); - try { - cgicc::Cgicc cgi(&IO); - CgiEnvironment env(&cgi); - CgiApplicationEngine app(&env); - app.process(); - IO << "Cache-control: no-cache" << std::endl; - app.getHeader()->render(IO); - xmlOutputBufferPtr out = xmlOutputBufferCreateIO( - xmlWrite, NULL, &IO, xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8)); - app.write(boost::bind(xmlSaveFileTo, out, _1, "utf-8")); - } - catch (const std::exception & e) { - cgicc::HTTPStatusHeader header(500, e.what()); - header.render(IO); - char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); - IO << "Kaboom!" << std::endl - << std::endl - << buf << std::endl - << e.what() << std::endl; - free(buf); - } - catch (const Glib::Exception & e) { - cgicc::HTTPStatusHeader header(500, e.what()); - header.render(IO); - char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); - IO << "Kaboom!" << std::endl - << std::endl - << buf << std::endl - << e.what() << std::endl; - free(buf); - } - catch (...) { - cgicc::HTTPStatusHeader header(500, "Unknown exception"); - header.render(IO); - IO << "Kaboom!" << std::endl - << std::endl - << "Unknown exception." << std::endl; - throw; - } - alarm(60); - LoaderBase::onIteration(); - if (time(NULL) > lastPeriodic + periodicDelay) { - p2webPeriodic(); - } - } - } - else { - cgicc::Cgicc cgi(NULL); - CgiEnvironment env(&cgi); - CgiApplicationEngine app(&env); - app.process(); - app.write(boost::bind(xmlDocDump, realstdout, _1)); - LoaderBase::onIteration(); - LoaderBase::onPeriodic(); - LoaderBase::onIdle(); - } - return 0; -} |