2016-06-14 20:14:06 +02:00
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QByteArray>
|
2017-01-14 19:04:58 +01:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
2016-06-14 20:14:06 +02:00
|
|
|
|
|
|
|
#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-09-15 20:42:58 +02:00
|
|
|
#include <utils/Process.h>
|
2017-01-14 19:04:58 +01:00
|
|
|
#include <utils/jsonschema/QJsonFactory.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)
|
2017-01-14 19:04:58 +01:00
|
|
|
, _args(QStringList())
|
2016-10-11 19:51:20 +02:00
|
|
|
, _hyperionConfig(_hyperion->getQJsonConfig())
|
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();
|
2017-01-14 19:04:58 +01:00
|
|
|
_args = args;
|
|
|
|
_request = request;
|
|
|
|
_reply = reply;
|
|
|
|
cmd_cfg_jsonserver();
|
|
|
|
cmd_cfg_get();
|
|
|
|
cmd_cfg_set();
|
|
|
|
cmd_runscript();
|
2016-06-14 20:14:06 +02:00
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
catch(int e)
|
|
|
|
{
|
|
|
|
if (e != 0)
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
void CgiHandler::cmd_cfg_jsonserver()
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
if ( _args.at(0) == "cfg_jsonserver" )
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
|
|
|
quint16 jsonPort = 19444;
|
2016-10-11 19:51:20 +02:00
|
|
|
if (_hyperionConfig.contains("jsonServer"))
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2016-10-11 19:51:20 +02:00
|
|
|
const QJsonObject jsonConfig = _hyperionConfig["jsonServer"].toObject();
|
|
|
|
jsonPort = jsonConfig["port"].toInt(jsonPort);
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send result as reply
|
2017-01-14 19:04:58 +01:00
|
|
|
_reply->addHeader ("Content-Type", "text/plain" );
|
|
|
|
_reply->appendRawData (QByteArrayLiteral(":") % QString::number(jsonPort).toUtf8() );
|
|
|
|
|
2016-06-14 20:14:06 +02:00
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
void CgiHandler::cmd_cfg_get()
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
if ( _args.at(0) == "cfg_get" )
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
|
|
|
QFile file ( _hyperion->getConfigFileName().c_str() );
|
|
|
|
if (file.exists ())
|
|
|
|
{
|
|
|
|
if (file.open (QFile::ReadOnly)) {
|
|
|
|
QByteArray data = file.readAll ();
|
2017-01-14 19:04:58 +01:00
|
|
|
_reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
_reply->appendRawData (data);
|
2016-06-14 20:14:06 +02:00
|
|
|
file.close ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
}
|
2016-09-03 15:54:33 +02:00
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
void CgiHandler::cmd_cfg_set()
|
|
|
|
{
|
|
|
|
_reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
if ( _args.at(0) == "cfg_set" )
|
|
|
|
{
|
|
|
|
QtHttpPostData data = _request->getPostData();
|
|
|
|
QJsonParseError error;
|
|
|
|
if (data.contains("cfg"))
|
|
|
|
{
|
2017-01-15 08:53:52 +01:00
|
|
|
QJsonDocument hyperionConfig = QJsonDocument::fromJson(QByteArray::fromPercentEncoding(data["cfg"]), &error);
|
2017-01-14 19:04:58 +01:00
|
|
|
|
|
|
|
if (error.error == QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
QJsonObject hyperionConfigJsonObj = hyperionConfig.object();
|
|
|
|
QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-15 08:53:52 +01:00
|
|
|
//Debug(_log, "error while saving: %s", error.errorString()).toLocal8bit.constData());
|
2017-01-14 19:04:58 +01:00
|
|
|
_reply->appendRawData (QString("Error while validating json: "+error.errorString()).toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CgiHandler::cmd_runscript()
|
2016-09-03 15:54:33 +02:00
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
if ( _args.at(0) == "run" )
|
2016-09-03 15:54:33 +02:00
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
QStringList scriptFilePathList(_args);
|
2016-09-03 15:54:33 +02:00
|
|
|
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())
|
|
|
|
{
|
2016-09-15 20:42:58 +02:00
|
|
|
QByteArray data = Process::command_exec(QString(interpreter + " " + scriptFilePath).toUtf8().constData()).c_str();
|
2016-09-03 15:54:33 +02:00
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
_reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
_reply->appendRawData (data);
|
2016-09-03 15:54:33 +02:00
|
|
|
throw 0;
|
|
|
|
}
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|