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
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
CgiHandler::CgiHandler (QObject * parent)
|
2016-06-14 20:14:06 +02:00
|
|
|
: QObject(parent)
|
2017-01-14 19:04:58 +01:00
|
|
|
, _args(QStringList())
|
2018-12-27 23:11:32 +01:00
|
|
|
, _baseUrl()
|
2017-01-29 21:20:12 +01:00
|
|
|
, _log(Logger::getInstance("WEBSERVER"))
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void CgiHandler::setBaseUrl(const QString& url)
|
|
|
|
{
|
|
|
|
_baseUrl = url;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-07-19 14:44:59 +02:00
|
|
|
_args = args;
|
|
|
|
_request = request;
|
|
|
|
_reply = reply;
|
|
|
|
|
|
|
|
if ( _args.at(0) == "cfg_jsonserver" )
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
cmd_cfg_jsonserver();
|
2020-07-19 14:44:59 +02:00
|
|
|
}
|
|
|
|
else if ( _args.at(0) == "run" )
|
|
|
|
{
|
2017-01-14 19:04:58 +01:00
|
|
|
cmd_runscript();
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
2020-07-19 14:44:59 +02:00
|
|
|
else
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2020-07-19 14:44:59 +02:00
|
|
|
throw std::runtime_error("CGI command not found");
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
void CgiHandler::cmd_cfg_jsonserver()
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2020-07-19 14:44:59 +02:00
|
|
|
quint16 jsonPort = 19444;
|
|
|
|
// send result as reply
|
|
|
|
_reply->addHeader ("Content-Type", "text/plain" );
|
|
|
|
_reply->appendRawData (QByteArrayLiteral(":") % QString::number(jsonPort).toUtf8() );
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 19:04:58 +01:00
|
|
|
void CgiHandler::cmd_runscript()
|
2016-09-03 15:54:33 +02:00
|
|
|
{
|
2020-07-19 14:44:59 +02:00
|
|
|
QStringList scriptFilePathList(_args);
|
|
|
|
scriptFilePathList.removeAt(0);
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2020-07-19 14:44:59 +02:00
|
|
|
QString scriptFilePath = scriptFilePathList.join('/');
|
|
|
|
// relative path not allowed
|
|
|
|
if ( scriptFilePath.indexOf("..") >=0 )
|
|
|
|
{
|
|
|
|
throw std::runtime_error("[cmd_runscript] Relative path not allowed : %s" + scriptFilePath.toStdString());
|
|
|
|
}
|
2017-01-29 21:20:12 +01:00
|
|
|
|
2020-07-19 14:44:59 +02:00
|
|
|
scriptFilePath = _baseUrl+"/server_scripts/"+scriptFilePath;
|
2017-01-29 21:20:12 +01:00
|
|
|
|
2020-07-19 14:44:59 +02:00
|
|
|
if ( !QFile::exists(scriptFilePath) || !scriptFilePath.endsWith(".py") )
|
|
|
|
{
|
|
|
|
throw std::runtime_error("[cmd_runscript] Script %s doesn't exists or is no python file : " + scriptFilePath.toStdString());
|
2016-09-03 15:54:33 +02:00
|
|
|
}
|
2020-07-19 14:44:59 +02:00
|
|
|
|
|
|
|
QtHttpPostData postData = _request->getPostData();
|
|
|
|
QByteArray inputData; // should be filled with post data
|
|
|
|
QByteArray data = Process::command_exec("python " + scriptFilePath, inputData);
|
|
|
|
_reply->addHeader ("Content-Type", "text/plain");
|
|
|
|
_reply->appendRawData (data);
|
2016-09-03 15:54:33 +02:00
|
|
|
}
|