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>
|
2017-01-29 21:20:12 +01:00
|
|
|
#include <QProcess>
|
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)
|
2017-01-29 21:20:12 +01:00
|
|
|
, _log(Logger::getInstance("WEBSERVER"))
|
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
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
QFile file ( _hyperion->getConfigFileName() );
|
2016-06-14 20:14:06 +02:00
|
|
|
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();
|
2017-02-26 15:30:10 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(resource);
|
2017-07-30 13:32:10 +02:00
|
|
|
|
|
|
|
QString schemaFile = ":/hyperion-schema";
|
|
|
|
QJsonObject schemaJson;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
schemaJson = QJsonFactory::readSchema(schemaFile);
|
|
|
|
}
|
|
|
|
catch(const std::runtime_error& error)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(error.what());
|
|
|
|
}
|
|
|
|
|
2017-02-26 15:30:10 +01:00
|
|
|
QJsonSchemaChecker schemaChecker;
|
|
|
|
schemaChecker.setSchema(schemaJson);
|
2017-07-30 13:32:10 +02:00
|
|
|
|
|
|
|
QPair<bool, bool> validate = schemaChecker.validate(hyperionConfigJsonObj);
|
|
|
|
|
|
|
|
|
|
|
|
if (validate.first && validate.second)
|
2017-02-26 15:30:10 +01:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
QJsonFactory::writeJson(_hyperion->getConfigFileName(), hyperionConfigJsonObj);
|
2017-02-26 15:30:10 +01:00
|
|
|
}
|
2017-07-30 13:32:10 +02:00
|
|
|
else if (!validate.first && validate.second)
|
|
|
|
{
|
|
|
|
Warning(_log,"Errors have been found in the configuration file. Automatic correction is applied");
|
|
|
|
|
|
|
|
QStringList schemaErrors = schemaChecker.getMessages();
|
|
|
|
foreach (auto & schemaError, schemaErrors)
|
|
|
|
Info(_log, schemaError.toUtf8().constData());
|
|
|
|
|
|
|
|
hyperionConfigJsonObj = schemaChecker.getAutoCorrectedConfig(hyperionConfigJsonObj);
|
|
|
|
|
|
|
|
if (!QJsonFactory::writeJson(_hyperion->getConfigFileName(), hyperionConfigJsonObj))
|
|
|
|
throw std::runtime_error("ERROR: can not save configuration file, aborting ");
|
|
|
|
}
|
|
|
|
else //Error in Schema
|
2017-02-26 15:30:10 +01:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
QString errorMsg = "ERROR: Json validation failed: \n";
|
|
|
|
QStringList schemaErrors = schemaChecker.getMessages();
|
|
|
|
foreach (auto & schemaError, schemaErrors)
|
2017-02-26 15:30:10 +01:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
Error(_log, "config write validation: %s", QSTRING_CSTR(schemaError));
|
|
|
|
errorMsg += schemaError + "\n";
|
2017-02-26 15:30:10 +01:00
|
|
|
}
|
2017-07-30 13:32:10 +02:00
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
throw std::runtime_error(errorMsg.toStdString());
|
2017-02-26 15:30:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::runtime_error& validate_error)
|
|
|
|
{
|
|
|
|
_reply->appendRawData (QString(validate_error.what()).toUtf8());
|
|
|
|
}
|
2017-01-14 19:04:58 +01:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
Error( _log, "relative path not allowed (%s)", scriptFilePath.toStdString().c_str());
|
2016-09-03 15:54:33 +02:00
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptFilePath = _baseUrl+"/server_scripts/"+scriptFilePath;
|
2017-01-29 21:20:12 +01:00
|
|
|
|
|
|
|
if (QFile::exists(scriptFilePath) && scriptFilePath.endsWith(".py") )
|
2016-09-03 15:54:33 +02:00
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
QtHttpPostData postData = _request->getPostData();
|
|
|
|
QByteArray inputData; // should be filled with post data
|
|
|
|
QByteArray data = Process::command_exec("python " + scriptFilePath, inputData);
|
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;
|
|
|
|
}
|
2017-01-29 21:20:12 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Error( _log, "script %s doesn't exists or is no python file", scriptFilePath.toStdString().c_str());
|
|
|
|
}
|
|
|
|
|
2016-09-03 15:54:33 +02:00
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|