summaryrefslogtreecommitdiff
path: root/project2/p2webMain.cpp
blob: 1eafd2da29411b2edbf5afe409cf9c131826195b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <libxml/tree.h>
#include <cgicc/Cgicc.h>
#include <cgicc/CgiEnvironment.h>
#include <fcgi_stdio.h>
#include "cgiEnvironment.h"
#include "cgiAppEngine.h"
#include <boost/bind.hpp>

int
xmlWrite(void * _out, const char * buf, int len)
{
	return FCGX_PutStr(buf, len, (FCGX_Stream*)_out);
}
class CgiReader : public cgicc::CgiInput {
	public:
		CgiReader(FCGX_Stream * i, FCGX_ParamArray & e) :
			in(i),
			envp(e)
		{
		}
		~CgiReader() { }

		size_t read(char * data, size_t length)
		{
			return FCGX_GetStr(data, length, in);
		}
		std::string getenv(const char * env)
		{
			const char * e = FCGX_GetParam(env, envp);
			return e ? e : "";
		}
	private:
		FCGX_Stream *in;
		FCGX_ParamArray envp;
};

int main(void)
{
	if (!FCGX_IsCGI()) {
		FCGX_Stream *in, *_out, *err;
		FCGX_ParamArray envp;

		while (FCGX_Accept(&in, &_out, &err, &envp) >= 0) 
		{
			CgiReader reader(in, envp);
			cgicc::Cgicc cgi(&reader);
			try {
				CgiEnvironment env(&cgi);
				CgiApplicationEngine app(&env);
				app.process();
				FCGX_FPrintF(_out, "Content-type: text/xml-xslt\r\n\r\n");      
				xmlOutputBufferPtr out = xmlOutputBufferCreateIO(
						xmlWrite, NULL, _out, xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8));
				app.write(boost::bind(xmlSaveFileTo, out, _1, "utf-8"));
			}
			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 {
		cgicc::Cgicc cgi(NULL);
		CgiEnvironment env(&cgi);
		CgiApplicationEngine app(&env);
		app.process();
		//app.write(boost::bind(xmlDocDump, stdout, _1));
	}
	return 0;
}