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
|
|
|
{
|
|
|
|
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();
|
2017-09-16 09:08:21 +02:00
|
|
|
// cmd_cfg_set();
|
2017-01-14 19:04:58 +01:00
|
|
|
cmd_runscript();
|
2016-06-14 20:14:06 +02:00
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
catch(int e)
|
|
|
|
{
|
2017-09-16 09:08:21 +02:00
|
|
|
if (e != 0) throw 1;
|
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
|
|
|
{
|
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;
|
|
|
|
// 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_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);
|
2017-10-12 11:55:03 +02:00
|
|
|
|
2016-09-03 15:54:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|