2016-06-14 20:14:06 +02:00
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QByteArray>
|
|
|
|
|
|
|
|
#include "CgiHandler.h"
|
2016-06-19 00:56:47 +02:00
|
|
|
#include "QtHttpHeader.h"
|
2016-09-03 15:54:33 +02:00
|
|
|
#include <utils/FileUtils.h>
|
2016-06-14 20:14:06 +02:00
|
|
|
|
2016-09-03 15:54:33 +02:00
|
|
|
CgiHandler::CgiHandler (Hyperion * hyperion, QString baseUrl, QObject * parent)
|
2016-06-14 20:14:06 +02:00
|
|
|
: QObject(parent)
|
|
|
|
, _hyperion(hyperion)
|
|
|
|
, _hyperionConfig(_hyperion->getJsonConfig())
|
2016-09-03 15:54:33 +02:00
|
|
|
, _baseUrl(baseUrl)
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CgiHandler::~CgiHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:56:47 +02:00
|
|
|
void CgiHandler::exec(const QStringList & args, QtHttpRequest * request, QtHttpReply * reply)
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-06-19 00:56:47 +02:00
|
|
|
// QByteArray header = reply->getHeader(QtHttpHeader::Host);
|
|
|
|
// QtHttpRequest::ClientInfo info = request->getClientInfo();
|
|
|
|
|
2016-06-14 20:14:06 +02:00
|
|
|
cmd_cfg_jsonserver(args,reply);
|
|
|
|
cmd_cfg_hyperion(args,reply);
|
2016-09-03 15:54:33 +02:00
|
|
|
cmd_runscript(args,reply);
|
2016-06-14 20:14:06 +02:00
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
catch(int e)
|
|
|
|
{
|
|
|
|
if (e != 0)
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CgiHandler::cmd_cfg_jsonserver(const QStringList & args, QtHttpReply * reply)
|
|
|
|
{
|
|
|
|
if ( args.at(0) == "cfg_jsonserver" )
|
|
|
|
{
|
|
|
|
quint16 jsonPort = 19444;
|
|
|
|
if (_hyperionConfig.isMember("jsonServer"))
|
|
|
|
{
|
|
|
|
const Json::Value & jsonConfig = _hyperionConfig["jsonServer"];
|
|
|
|
jsonPort = jsonConfig.get("port", jsonPort).asUInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
// send result as reply
|
|
|
|
reply->addHeader ("Content-Type", "text/plain" );
|
|
|
|
reply->appendRawData (QByteArrayLiteral(":") % QString::number(jsonPort).toUtf8() );
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CgiHandler::cmd_cfg_hyperion(const QStringList & args, QtHttpReply * reply)
|
|
|
|
{
|
|
|
|
if ( args.at(0) == "cfg_hyperion" )
|
|
|
|
{
|
|
|
|
QFile file ( _hyperion->getConfigFileName().c_str() );
|
|
|
|
if (file.exists ())
|
|
|
|
{
|
|
|
|
if (file.open (QFile::ReadOnly)) {
|
|
|
|
QByteArray data = file.readAll ();
|
|
|
|
reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
reply->appendRawData (data);
|
|
|
|
file.close ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
}
|
2016-09-03 15:54:33 +02:00
|
|
|
|
|
|
|
void CgiHandler::cmd_runscript(const QStringList & args, QtHttpReply * reply)
|
|
|
|
{
|
|
|
|
if ( args.at(0) == "run" )
|
|
|
|
{
|
|
|
|
QStringList scriptFilePathList(args);
|
|
|
|
scriptFilePathList.removeAt(0);
|
|
|
|
|
|
|
|
QString scriptFilePath = scriptFilePathList.join('/');
|
|
|
|
// relative path not allowed
|
|
|
|
if (scriptFilePath.indexOf("..") >=0)
|
|
|
|
{
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptFilePath = _baseUrl+"/server_scripts/"+scriptFilePath;
|
|
|
|
QString interpreter = "";
|
|
|
|
if (scriptFilePath.endsWith(".sh")) interpreter = "sh";
|
|
|
|
if (scriptFilePath.endsWith(".py")) interpreter = "python";
|
|
|
|
|
|
|
|
if (QFile::exists(scriptFilePath) && !interpreter.isEmpty())
|
|
|
|
{
|
|
|
|
QByteArray data = FileUtils::command_exec(QString(interpreter + " " + scriptFilePath).toUtf8().constData()).c_str();
|
|
|
|
|
|
|
|
reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
reply->appendRawData (data);
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|