summaryrefslogtreecommitdiff
path: root/project2/cgi/cgiCommon.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2010-12-22 00:38:31 +0000
committerrandomdan <randomdan@localhost>2010-12-22 00:38:31 +0000
commit17e093f159821c6a106421e5a08d3b8bbcbc05e7 (patch)
tree08e943263818d49933593554f7eeba40845918a8 /project2/cgi/cgiCommon.cpp
parentAdd missing execute on custom SQL merge inserter (diff)
downloadproject2-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
Diffstat (limited to 'project2/cgi/cgiCommon.cpp')
-rw-r--r--project2/cgi/cgiCommon.cpp65
1 files changed, 65 insertions, 0 deletions
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;
+ }
+}
+