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.


Origin commit data
------------------
Branch: ni/coolstream
Commit: b5e2998d54
Author: vanhofen <vanhofen@gmx.de>
Date: 2016-02-22 (Mon, 22 Feb 2016)

Origin message was:
------------------
- 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.


------------------
This commit was generated by Migit
This commit is contained in:
vanhofen
2016-02-22 14:15:02 +01:00
parent dbb4706326
commit a9b725c9b5
6 changed files with 70 additions and 122 deletions

View File

@@ -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; 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("[CControlAPI] 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;
}
//-------------------------------------------------------------------------
void CControlAPI::doModifyTimer(CyhookHandler *hh)
{
hh->ParamList["update"]="1";

View File

@@ -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);

View File

@@ -11,8 +11,11 @@
#include <sstream>
#include <iomanip>
#include <unistd.h>
// yhttpd
#include <yconfig.h>
#include <tuxboxapi/controlapi.h>
#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;
}

View File

@@ -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__ */

View File

@@ -397,7 +397,7 @@ std::string CyParser::YWeb_cgi_cmd(CyhookHandler *hh, std::string ycmd) {
yresult = "<!-- " + comment_html + " -->";
}
} 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("<yparser> 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

View File

@@ -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);