-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcgihandler.cpp
69 lines (54 loc) · 1.46 KB
/
fcgihandler.cpp
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
#include "fcgihandler.h"
#include <sstream>
#include <thread>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
FcgiHandler::FcgiHandler(NetConfReader & netConfReader, const AppConf::FcgiParamsStruct & params):
in(nullptr),
out(nullptr),
err(nullptr),
socketId(-1),
_params(params),
_netConfReader(netConfReader)
{
}
FcgiHandler::~FcgiHandler()
{
}
bool FcgiHandler::Init()
{
FCGX_Init();
socketId = FCGX_OpenSocket(_params.sockPath.c_str(),
_params.queueLength);
if(socketId < 0)
return 0;
else
return 1;
}
void FcgiHandler::ThreadFunc()
{
FCGX_Request request;
FCGX_InitRequest(&request, socketId, 0);
while (true)
{
{
std::lock_guard<std::mutex> lock(fcgxAcceptMutex);
if(FCGX_Accept_r(&request) != 0)
return;
}
std::ostringstream outString;
outString << "Content-type: text/html\r\n"
<< "\r\n";
boost::property_tree::json_parser::write_json(outString, _netConfReader.GetNetConfiguration(), false);
PrintOut(request, outString.str());
FCGX_Finish_r(&request);
}
}
void FcgiHandler::PrintOut(FCGX_Request & request, const std::string &str)
{
std::lock_guard<std::mutex> lock(outStreamMutex);
fcgi_streambuf out_fcgi_streambuf(request.out);
out.rdbuf(&out_fcgi_streambuf);
out << str;
}