summaryrefslogtreecommitdiff
path: root/project2/p2webMain.cpp
blob: 896af0b2f387051112997cecb3ee8d4c4df7e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Undefine me to run from console for debugging
//#define FCGI

#include <libxml/tree.h>
#ifdef FCGI
#include <fcgi_stdio.h>
#endif
#include "envproc.h"
#include <boost/bind.hpp>

#ifdef FCGI
int
xmlWrite(void * _out, const char * buf, int len)
{
	return FCGX_PutStr(buf, len, (FCGX_Stream*)_out);
}
#endif

int main(void)
{
#ifdef FCGI
	FCGX_Stream *in, *_out, *err;
	FCGX_ParamArray envp;

	while (FCGX_Accept(&in, &_out, &err, &envp) >= 0) 
	{
		try {
			EnvironmentProcessor ep(boost::bind(FCGX_GetParam, _1, envp));

			boost::shared_ptr<xmlpp::Document> doc = ep.process();

			FCGX_FPrintF(_out, "Content-type: text/xml-xslt\r\n\r\n");      
			xmlOutputBufferPtr out = xmlOutputBufferCreateIO(
					xmlWrite, NULL, _out, xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8));
			xmlSaveFileTo(out, doc->cobj(), NULL);
		}
		catch (const std::exception & e) {
			FCGX_FPrintF(_out, "Content-type: text/plain\r\n\r\n");      
			FCGX_FPrintF(_out, "Kaboom!\r\n\r\n");      
			FCGX_FPrintF(_out, "%s\r\n\r\n", e.what());      
		}
		catch (...) {
			FCGX_FPrintF(_out, "Content-type: text/plain\r\n\r\n");      
			FCGX_FPrintF(_out, "Kaboom!\r\n\r\n");      
			FCGX_FPrintF(_out, "Unknown exception.\r\n\r\n");      
		}
	}
#else
			EnvironmentProcessor ep(getenv);

			boost::shared_ptr<xmlpp::Document> doc = ep.process();
			xmlDocDump(stdout, doc->cobj());
#endif
	return 0;
}