summaryrefslogtreecommitdiff
path: root/project2/p2webMain.cpp
blob: a526f8d94560d834f290dd32535f921949aa7cd0 (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
#include <libxml/tree.h>
#include <cgicc/Cgicc.h>
#include <cgicc/CgiEnvironment.h>
#include <cgicc/HTTPContentHeader.h>
FILE * realstdout = stdout;
#include <fcgi_stdio.h>
#include "cgiEnvironment.h"
#include "cgiAppEngine.h"
#include <boost/bind.hpp>
#include "FCgiIO.h"

int
xmlWrite(void * _out, const char * buf, int len)
{
	cgicc::FCgiIO & IO = *((cgicc::FCgiIO*)_out);
	IO.write(buf, len);
	return len;
}

int main(void)
{
	if (!FCGX_IsCGI()) {
		FCGX_Request request;

		FCGX_Init();
		FCGX_InitRequest(&request, 0, 0);

		while (FCGX_Accept_r(&request) == 0) {
			cgicc::FCgiIO IO(request);
			try {
				cgicc::Cgicc cgi(&IO);
				CgiEnvironment env(&cgi);
				cgicc::HTTPContentHeader header("text/xml-xslt");
				CgiApplicationEngine app(&env, &header);
				app.process();
				IO << "Cache-control: no-cache" << std::endl;
				header.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::HTTPContentHeader header("text/plain");
				header.render(IO);
				IO << "Kaboom!" << std::endl << std::endl << e.what();
			}
			catch (...) {
				cgicc::HTTPContentHeader header("text/plain");
				header.render(IO);
				IO << "Kaboom!" << std::endl << std::endl << "Unknown exception.";      
			}
		}
	}
	else {
		cgicc::Cgicc cgi(NULL);
		CgiEnvironment env(&cgi);
		cgicc::HTTPContentHeader header("text/xml-xslt");
		CgiApplicationEngine app(&env, &header);
		app.process();
		app.write(boost::bind(xmlDocDump, realstdout, _1));
	}
	return 0;
}