23 #include "ScriptHandler.h"
25 Logger ScriptHandler::logger;
27 ScriptHandler::ScriptHandler() {
28 logger = Logger::getLogger(
"ScriptHandler");
31 ScriptHandler::~ScriptHandler() {
35 string ScriptHandler::chdirExecute(
string exe,
string tmpf,
bool retErrs)
37 if(chdir(tmpf.c_str())==0)
38 return execute(exe, retErrs);
42 string ScriptHandler::execute(
string exe,
bool retErrs)
50 pp = popen(exe.c_str(),
"r");
55 line = fgets(buf,
sizeof buf, pp);
56 if (line == NULL)
break;
65 int ScriptHandler::popenRWE(
int *rwepipe,
const char *exe,
const char *
const argv[],
string tmpf)
94 }
else if (pid == 0) {
106 execvp(exe, (
char**)argv);
126 int ScriptHandler::popenRWEN(
int *rwepipe,
const char *exe,
const char** argv)
154 }
else if (pid == 0) {
165 execvp(exe, (
char**)argv);
185 int ScriptHandler::pcloseRWE(
int pid,
int *rwepipe)
191 waitpid(pid, &status, 0);
196 string ext, map<string, string> props)
199 string referer = req->getReferer();
200 if(referer.find(
"http://")!=string::npos)
202 string appl = referer.substr(referer.find(
"http://")+7);
203 appl = appl.substr(referer.find(
"/")+1);
204 if(appl.find(req->getCntxt_name())==0 && handoffs.find(req->getCntxt_name())!=handoffs.end())
206 if(appl==req->getCntxt_name()+
"/"+handoffs[req->getCntxt_name()])
218 string tmpf =
"/temp/";
220 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
222 def = handoffs[req->getCntxt_name()];
225 string phpcnts = req->toPHPVariablesString(def);
227 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".php";
228 tmpf = req->getCntxt_root() + tmpf;
230 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
232 string command =
"php " + filen;
233 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
234 if((content.length()==0))
236 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
241 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
242 res.setContent_type(props[
".html"]);
243 res.setContent_str(content);
253 string tmpf =
"/temp/";
255 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
257 def = handoffs[req->getCntxt_name()];
260 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".pl";
261 tmpf = req->getCntxt_root() + tmpf;
262 string phpcnts = req->toPerlVariablesString();
264 string plfile = req->getUrl();
265 ifstream infile(plfile.c_str());
269 while(getline(infile, xml))
271 phpcnts.append(xml+
"\n");
275 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
277 string command =
"perl " + filen;
278 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
279 if((content.length()==0))
281 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
286 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
287 res.setContent_type(props[
".html"]);
288 res.setContent_str(content);
298 string tmpf =
"/temp/";
300 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
302 def = handoffs[req->getCntxt_name()];
305 string phpcnts = req->toRubyVariablesString();
307 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".rb";
308 tmpf = req->getCntxt_root() + tmpf;
310 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
312 string command =
"ruby " + filen;
313 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
314 if((content.length()==0))
316 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
321 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
322 res.setContent_type(props[
".html"]);
323 res.setContent_str(content);
333 string tmpf =
"/temp/";
335 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
337 def = handoffs[req->getCntxt_name()];
340 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".py";
341 tmpf = req->getCntxt_root() + tmpf;
342 string phpcnts = req->toPythonVariablesString();
343 string plfile = req->getUrl();
344 ifstream infile(plfile.c_str());
348 while(getline(infile, xml))
350 phpcnts.append(xml+
"\n");
354 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
356 string command =
"python " + filen;
357 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
358 if((content.length()==0))
360 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
365 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
366 res.setContent_type(props[
".html"]);
367 res.setContent_str(content);
377 string tmpf =
"/temp/";
379 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
381 def = handoffs[req->getCntxt_name()];
384 string phpcnts = req->toLuaVariablesString();
386 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".lua";
387 tmpf = req->getCntxt_root() + tmpf;
389 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
391 string command =
"lua " + filen;
392 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
393 if((content.length()==0))
395 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
400 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
401 res.setContent_type(props[
".html"]);
402 res.setContent_str(content);
412 string tmpf =
"/temp/";
414 if(handoffs.find(req->getCntxt_name())!=handoffs.end())
416 def = handoffs[req->getCntxt_name()];
419 string phpcnts = req->toNodejsVariablesString();
421 filen = CastUtil::lexical_cast<
string>(Timer::getCurrentTime()) +
".njs";
422 tmpf = req->getCntxt_root() + tmpf;
424 AfcUtil::writeTofile(tmpf+filen, phpcnts,
true);
426 string command =
"node " + filen;
427 string content = chdirExecute(command, tmpf, Constants::SCRIPT_EXEC_SHOW_ERRS);
428 if((content.length()==0))
430 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
435 res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
436 res.setContent_type(props[
".html"]);
437 res.setContent_str(content);