From 102bba9b89beddef5c77439a752ca1ad7e1b7d2a Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 22 Dec 2010 00:38:31 +0000 Subject: Split the plain CGI and the FastCGI variants into separate programs with a now identical shared core implementation --- project2/Jamfile.jam | 24 +++++++- project2/cgi/cgiCommon.cpp | 65 ++++++++++++++++++++++ project2/cgi/cgiCommon.h | 5 ++ project2/cgi/cgiEnvironment.cpp | 1 + project2/cgi/p2webCgi.cpp | 12 ++++ project2/cgi/p2webFCgi.cpp | 59 ++++++++++++++++++++ project2/cgi/p2webMain.cpp | 119 ---------------------------------------- 7 files changed, 163 insertions(+), 122 deletions(-) create mode 100644 project2/cgi/cgiCommon.cpp create mode 100644 project2/cgi/cgiCommon.h create mode 100644 project2/cgi/p2webCgi.cpp create mode 100644 project2/cgi/p2webFCgi.cpp delete mode 100644 project2/cgi/p2webMain.cpp 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 : esmtp ; -exe p2web : - [ glob cgi/*.cpp ] : +lib p2web : + [ glob cgi/cgi*.cpp ] : p2common + cgicc + ; + +exe p2cgi : + cgi/p2webCgi.cpp : + p2web p2url p2sql p2mail @@ -103,9 +109,21 @@ exe p2web : p2processes p2xml p2xmlSession + ; + +exe p2fcgi : + cgi/p2webFCgi.cpp cgi/FCgiIO.cpp : + p2web fcgi++ fcgi - cgicc + p2url + p2sql + p2mail + p2files + p2regex + p2processes + p2xml + 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 +#include +#include +#include +#include +#include +#include "cgiEnvironment.h" +#include "cgiAppEngine.h" +#include +#include + +static +int +xmlWrite(void * _out, const char * buf, int len) +{ + std::ostream * IO = static_cast(_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 +#include + +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 +#include #include #include 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 +#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 -#include -#include -#include -#include -#include -FILE * realstdout = stdout; -#include -#include "cgiEnvironment.h" -#include "cgiAppEngine.h" -#include -#include -#include "FCgiIO.h" -#include - -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; -} -- cgit v1.2.3