From b5e2998d5436285f73ba6971fe9d67d116f3c49a Mon Sep 17 00:00:00 2001 From: svenhoefer Date: Mon, 22 Feb 2016 14:15:02 +0100 Subject: [PATCH] - controlapi/mod_yparse: avoid double code ... ... by moving functions to execute scripts to helpers.cpp|h Change outType from ExecCGI to text/plain because all arguments are passed to the script. So no format can be defined. --- src/nhttpd/tuxboxapi/controlapi.cpp | 79 ++++---------------------- src/nhttpd/tuxboxapi/controlapi.h | 6 +- src/nhttpd/yhttpd_core/helper.cpp | 51 +++++++++++++++++ src/nhttpd/yhttpd_core/helper.h | 5 ++ src/nhttpd/yhttpd_mods/mod_yparser.cpp | 50 +--------------- src/nhttpd/yhttpd_mods/mod_yparser.h | 1 - 6 files changed, 70 insertions(+), 122 deletions(-) diff --git a/src/nhttpd/tuxboxapi/controlapi.cpp b/src/nhttpd/tuxboxapi/controlapi.cpp index b3c31b8b4..df348214b 100644 --- a/src/nhttpd/tuxboxapi/controlapi.cpp +++ b/src/nhttpd/tuxboxapi/controlapi.cpp @@ -194,7 +194,7 @@ const CControlAPI::TyCgiCall CControlAPI::yCgiCallList[]= {"rcem", &CControlAPI::RCEmCGI, "text/plain"}, // Start skripts, plugins {"startplugin", &CControlAPI::StartPluginCGI, "text/plain"}, - {"exec", &CControlAPI::ExecCGI, "+xml"}, + {"exec", &CControlAPI::ExecCGI, "text/plain"}, {"yweb", &CControlAPI::YWebCGI, "text/plain"}, // video & Audio handling {"aspectratio", &CControlAPI::AspectRatioCGI, "text/plain"}, @@ -421,16 +421,8 @@ void CControlAPI::GetModeCGI(CyhookHandler *hh) //----------------------------------------------------------------------------- void CControlAPI::ExecCGI(CyhookHandler *hh) { - bool res = false; std::string script, result; - // override standard header - if (hh->ParamList.size() > 1 && (hh->getOutType() != xml)) - hh->SetHeader(HTTP_OK, "text/html; charset=UTF-8"); - else if (hh->ParamList.size() > 1 && (hh->getOutType() == xml)) - hh->SetHeader(HTTP_OK, "text/xml; charset=UTF-8"); - else - hh->SetHeader(HTTP_OK, "text/plain; charset=UTF-8"); - if ( !hh->ParamList.empty() ) + if (!hh->ParamList.empty() ) { script = hh->ParamList["1"]; unsigned int len = hh->ParamList.size(); @@ -440,16 +432,18 @@ void CControlAPI::ExecCGI(CyhookHandler *hh) script += " "; script += hh->ParamList[itoa(y)]; } - result = YexecuteScript(hh, script); + result = yExecuteScript(script); } else - printf("[CControlAPI] no script given\n"); + { + log_level_printf(0, "[%s] no script given\n", __func__); + result = "error"; + } - res = (result != "error"); - if (res) - hh->Write(result); - else + if (result == "error") hh->SetError(HTTP_NOT_FOUND); + else + hh->WriteLn(result); } //----------------------------------------------------------------------------- @@ -2433,59 +2427,6 @@ void CControlAPI::YWeb_SendRadioStreamingPid(CyhookHandler *hh) } //----------------------------------------------------------------------------- -std::string CControlAPI::YexecuteScript(CyhookHandler *, std::string cmd) -{ - std::string script, para, result; - bool found = false; - - // split script and parameters - int pos; - if ((pos = cmd.find_first_of(" ")) > 0) - { - script = cmd.substr(0, pos); - para = cmd.substr(pos+1,cmd.length() - (pos+1)); // snip - } - else - script=cmd; - // get file - std::string fullfilename; - script += ".sh"; //add script extention - char cwd[255]={0}; - getcwd(cwd, 254); - - for (unsigned int i=0; iParamList["update"]="1"; diff --git a/src/nhttpd/tuxboxapi/controlapi.h b/src/nhttpd/tuxboxapi/controlapi.h index 8b635dd9e..67c812b71 100644 --- a/src/nhttpd/tuxboxapi/controlapi.h +++ b/src/nhttpd/tuxboxapi/controlapi.h @@ -69,7 +69,6 @@ private: void YWeb_SendVideoStreamingPids(CyhookHandler *hh, int apid_no); void YWeb_SendRadioStreamingPid(CyhookHandler *hh); void compatibility_Timer(CyhookHandler *hh); - std::string YexecuteScript(CyhookHandler *hh, std::string cmd); // CGI functions for ExecuteCGI void TimerCGI(CyhookHandler *hh); @@ -133,14 +132,15 @@ private: protected: - static const unsigned int PLUGIN_DIR_COUNT = 9; - static std::string PLUGIN_DIRS[PLUGIN_DIR_COUNT]; CNeutrinoAPI *NeutrinoAPI; void init(CyhookHandler *hh); void Execute(CyhookHandler *hh); public: + static const unsigned int PLUGIN_DIR_COUNT = 9; + static std::string PLUGIN_DIRS[PLUGIN_DIR_COUNT]; + // constructor & deconstructor CControlAPI(CNeutrinoAPI *_NeutrinoAPI); diff --git a/src/nhttpd/yhttpd_core/helper.cpp b/src/nhttpd/yhttpd_core/helper.cpp index 1a4e98aff..1dabc4db6 100644 --- a/src/nhttpd/yhttpd_core/helper.cpp +++ b/src/nhttpd/yhttpd_core/helper.cpp @@ -11,8 +11,11 @@ #include #include +#include + // yhttpd #include +#include #include "ytypes_globals.h" #include "helper.h" #include "ylogging.h" @@ -354,3 +357,51 @@ std::string json_convert_string(std::string s) { } return ss.str(); } + +std::string yExecuteScript(std::string cmd) { + std::string script, para, result; + bool found = false; + + //aprintf("%s: %s\n", __func__, cmd.c_str()); + + // split script and parameters + int pos; + if ((pos = cmd.find_first_of(" ")) > 0) { + script = cmd.substr(0, pos); + para = cmd.substr(pos + 1, cmd.length() - (pos + 1)); // snip + } else + script = cmd; + // get file + std::string fullfilename; + script += ".sh"; //add script extention + + char cwd[255]; + getcwd(cwd, 254); + for (unsigned int i = 0; i < CControlAPI::PLUGIN_DIR_COUNT && !found; i++) { + fullfilename = CControlAPI::PLUGIN_DIRS[i] + "/" + script; + FILE *test = fopen(fullfilename.c_str(), "r"); // use fopen: popen does not work + if (test != NULL) { + fclose(test); + chdir(CControlAPI::PLUGIN_DIRS[i].c_str()); + FILE *f = popen((fullfilename + " " + para).c_str(), "r"); //execute + if (f != NULL) { + found = true; + + char output[1024]; + while (fgets(output, 1024, f)) // get script output + result += output; + pclose(f); + } + } + } + chdir(cwd); + + if (!found) { + printf("%s: script %s not found in:\n", __func__, script.c_str()); + for (unsigned int i = 0; i < CControlAPI::PLUGIN_DIR_COUNT; i++) { + printf("\t%s\n", CControlAPI::PLUGIN_DIRS[i].c_str()); + } + result = "error"; + } + return result; +} diff --git a/src/nhttpd/yhttpd_core/helper.h b/src/nhttpd/yhttpd_core/helper.h index 22a917e8b..0984cf54c 100644 --- a/src/nhttpd/yhttpd_core/helper.h +++ b/src/nhttpd/yhttpd_core/helper.h @@ -49,4 +49,9 @@ std::string json_out_success(std::string _result); std::string json_out_error(std::string _error); std::string json_convert_string(std::string s); +//----------------------------------------------------------------------------- +// Script Helpers +//----------------------------------------------------------------------------- +std::string yExecuteScript(std::string cmd); + #endif /* __yhttpd_helper_h__ */ diff --git a/src/nhttpd/yhttpd_mods/mod_yparser.cpp b/src/nhttpd/yhttpd_mods/mod_yparser.cpp index b75231fc7..bcef54ea4 100644 --- a/src/nhttpd/yhttpd_mods/mod_yparser.cpp +++ b/src/nhttpd/yhttpd_mods/mod_yparser.cpp @@ -397,7 +397,7 @@ std::string CyParser::YWeb_cgi_cmd(CyhookHandler *hh, std::string ycmd) { yresult = ""; } } else if (ycmd_type == "script") - yresult = YexecuteScript(hh, ycmd_name); + yresult = yExecuteScript(ycmd_name); else if (ycmd_type == "if-empty") { std::string if_value, if_then, if_else; if (ySplitString(ycmd_name, "~", if_value, if_then)) { @@ -636,54 +636,6 @@ std::string CyParser::YWeb_cgi_include_block(std::string filename, return yresult; } -//------------------------------------------------------------------------- - -std::string CyParser::YexecuteScript(CyhookHandler *, std::string cmd) { - std::string script, para, result; - bool found = false; - - // split script and parameters - int pos; - if ((pos = cmd.find_first_of(" ")) > 0) { - script = cmd.substr(0, pos); - para = cmd.substr(pos + 1, cmd.length() - (pos + 1)); // snip - } else - script = cmd; - // get file - std::string fullfilename; - script += ".sh"; //add script extention - - char cwd[255]; - getcwd(cwd, 254); - for (unsigned int i = 0; i < PLUGIN_DIR_COUNT && !found; i++) { - fullfilename = PLUGIN_DIRS[i] + "/" + script; - FILE *test = fopen(fullfilename.c_str(), "r"); // use fopen: popen does not work - if (test != NULL) { - fclose(test); - chdir(PLUGIN_DIRS[i].c_str()); - FILE *f = popen((fullfilename + " " + para).c_str(), "r"); //execute - if (f != NULL) { - found = true; - - char output[1024]; - while (fgets(output, 1024, f)) // get script output - result += output; - pclose(f); - } - } - } - chdir(cwd); - - if (!found) { - printf(" script %s not found in\n", script.c_str()); - for (unsigned int i = 0; i < PLUGIN_DIR_COUNT; i++) { - printf("%s\n", PLUGIN_DIRS[i].c_str()); - } - result = "error"; - } - return result; -} - //============================================================================= // y-func : Dispatching // TODO: new functions for diff --git a/src/nhttpd/yhttpd_mods/mod_yparser.h b/src/nhttpd/yhttpd_mods/mod_yparser.h index ae5d49118..dc6209ec2 100644 --- a/src/nhttpd/yhttpd_mods/mod_yparser.h +++ b/src/nhttpd/yhttpd_mods/mod_yparser.h @@ -95,7 +95,6 @@ private: std::string YWeb_cgi_get_ini(CyhookHandler *hh, std::string filename, std::string varname, std::string yaccess); void YWeb_cgi_set_ini(CyhookHandler *hh, std::string filename, std::string varname, std::string varvalue, std::string yaccess); std::string YWeb_cgi_include_block(std::string filename, std::string blockname, std::string ydefault); - std::string YexecuteScript(CyhookHandler *hh, std::string cmd); // CGIs void cgi(CyhookHandler *hh);