mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
move write config from json api to http post (#363)
* implement config save over http post instead of json * remove json set config finish config write thrugh http post * remove debug code and add failure messages
This commit is contained in:
@@ -2,15 +2,20 @@
|
||||
#include <QUrlQuery>
|
||||
#include <QFile>
|
||||
#include <QByteArray>
|
||||
#include <QStringList>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "CgiHandler.h"
|
||||
#include "QtHttpHeader.h"
|
||||
#include <utils/FileUtils.h>
|
||||
#include <utils/Process.h>
|
||||
#include <utils/jsonschema/QJsonFactory.h>
|
||||
|
||||
CgiHandler::CgiHandler (Hyperion * hyperion, QString baseUrl, QObject * parent)
|
||||
: QObject(parent)
|
||||
, _hyperion(hyperion)
|
||||
, _args(QStringList())
|
||||
, _hyperionConfig(_hyperion->getQJsonConfig())
|
||||
, _baseUrl(baseUrl)
|
||||
{
|
||||
@@ -26,10 +31,13 @@ void CgiHandler::exec(const QStringList & args, QtHttpRequest * request, QtHttpR
|
||||
{
|
||||
// QByteArray header = reply->getHeader(QtHttpHeader::Host);
|
||||
// QtHttpRequest::ClientInfo info = request->getClientInfo();
|
||||
|
||||
cmd_cfg_jsonserver(args,reply);
|
||||
cmd_cfg_hyperion(args,reply);
|
||||
cmd_runscript(args,reply);
|
||||
_args = args;
|
||||
_request = request;
|
||||
_reply = reply;
|
||||
cmd_cfg_jsonserver();
|
||||
cmd_cfg_get();
|
||||
cmd_cfg_set();
|
||||
cmd_runscript();
|
||||
throw 1;
|
||||
}
|
||||
catch(int e)
|
||||
@@ -39,9 +47,9 @@ void CgiHandler::exec(const QStringList & args, QtHttpRequest * request, QtHttpR
|
||||
}
|
||||
}
|
||||
|
||||
void CgiHandler::cmd_cfg_jsonserver(const QStringList & args, QtHttpReply * reply)
|
||||
void CgiHandler::cmd_cfg_jsonserver()
|
||||
{
|
||||
if ( args.at(0) == "cfg_jsonserver" )
|
||||
if ( _args.at(0) == "cfg_jsonserver" )
|
||||
{
|
||||
quint16 jsonPort = 19444;
|
||||
if (_hyperionConfig.contains("jsonServer"))
|
||||
@@ -51,24 +59,25 @@ void CgiHandler::cmd_cfg_jsonserver(const QStringList & args, QtHttpReply * repl
|
||||
}
|
||||
|
||||
// send result as reply
|
||||
reply->addHeader ("Content-Type", "text/plain" );
|
||||
reply->appendRawData (QByteArrayLiteral(":") % QString::number(jsonPort).toUtf8() );
|
||||
_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)
|
||||
void CgiHandler::cmd_cfg_get()
|
||||
{
|
||||
if ( args.at(0) == "cfg_hyperion" )
|
||||
if ( _args.at(0) == "cfg_get" )
|
||||
{
|
||||
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);
|
||||
_reply->addHeader ("Content-Type", "text/plain");
|
||||
_reply->appendRawData (data);
|
||||
file.close ();
|
||||
}
|
||||
}
|
||||
@@ -76,11 +85,37 @@ void CgiHandler::cmd_cfg_hyperion(const QStringList & args, QtHttpReply * reply)
|
||||
}
|
||||
}
|
||||
|
||||
void CgiHandler::cmd_runscript(const QStringList & args, QtHttpReply * reply)
|
||||
void CgiHandler::cmd_cfg_set()
|
||||
{
|
||||
if ( args.at(0) == "run" )
|
||||
_reply->addHeader ("Content-Type", "text/plain");
|
||||
if ( _args.at(0) == "cfg_set" )
|
||||
{
|
||||
QStringList scriptFilePathList(args);
|
||||
QtHttpPostData data = _request->getPostData();
|
||||
QJsonParseError error;
|
||||
if (data.contains("cfg"))
|
||||
{
|
||||
QJsonDocument hyperionConfig = QJsonDocument::fromJson(data["cfg"], &error);
|
||||
|
||||
if (error.error == QJsonParseError::NoError)
|
||||
{
|
||||
QJsonObject hyperionConfigJsonObj = hyperionConfig.object();
|
||||
QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj);
|
||||
}
|
||||
else
|
||||
{
|
||||
_reply->appendRawData (QString("Error while validating json: "+error.errorString()).toUtf8());
|
||||
}
|
||||
}
|
||||
|
||||
throw 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CgiHandler::cmd_runscript()
|
||||
{
|
||||
if ( _args.at(0) == "run" )
|
||||
{
|
||||
QStringList scriptFilePathList(_args);
|
||||
scriptFilePathList.removeAt(0);
|
||||
|
||||
QString scriptFilePath = scriptFilePathList.join('/');
|
||||
@@ -99,8 +134,8 @@ void CgiHandler::cmd_runscript(const QStringList & args, QtHttpReply * reply)
|
||||
{
|
||||
QByteArray data = Process::command_exec(QString(interpreter + " " + scriptFilePath).toUtf8().constData()).c_str();
|
||||
|
||||
reply->addHeader ("Content-Type", "text/plain");
|
||||
reply->appendRawData (data);
|
||||
_reply->addHeader ("Content-Type", "text/plain");
|
||||
_reply->appendRawData (data);
|
||||
throw 0;
|
||||
}
|
||||
throw 1;
|
||||
|
@@ -20,15 +20,18 @@ public:
|
||||
void exec(const QStringList & args,QtHttpRequest * request, QtHttpReply * reply);
|
||||
|
||||
// cgi commands
|
||||
void cmd_cfg_jsonserver(const QStringList & args, QtHttpReply * reply);
|
||||
void cmd_cfg_hyperion (const QStringList & args, QtHttpReply * reply);
|
||||
void cmd_runscript (const QStringList & args, QtHttpReply * reply);
|
||||
void cmd_cfg_jsonserver();
|
||||
void cmd_cfg_get ();
|
||||
void cmd_cfg_set ();
|
||||
void cmd_runscript ();
|
||||
|
||||
private:
|
||||
Hyperion* _hyperion;
|
||||
QtHttpReply * _reply;
|
||||
QtHttpRequest * _request;
|
||||
QStringList _args;
|
||||
const QJsonObject &_hyperionConfig;
|
||||
const QString _baseUrl;
|
||||
const QString _baseUrl;
|
||||
};
|
||||
|
||||
#endif // CGIHANDLER_H
|
||||
|
@@ -50,7 +50,7 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
|
||||
QString url = parts.at (1);
|
||||
QString version = parts.at (2);
|
||||
if (version == QtHttpServer::HTTP_VERSION) {
|
||||
m_currentRequest = new QtHttpRequest (m_serverHandle);
|
||||
m_currentRequest = new QtHttpRequest (this, m_serverHandle);
|
||||
m_currentRequest->setClientInfo(m_sockClient->localAddress(), m_sockClient->peerAddress());
|
||||
m_currentRequest->setUrl (QUrl (url));
|
||||
m_currentRequest->setCommand (command);
|
||||
@@ -76,9 +76,8 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
|
||||
QByteArray value = raw.mid (pos +1).trimmed ();
|
||||
m_currentRequest->addHeader (header, value);
|
||||
if (header == QtHttpHeader::ContentLength) {
|
||||
int len = -1;
|
||||
bool ok = false;
|
||||
len = value.toInt (&ok, 10);
|
||||
const int len = value.toInt (&ok, 10);
|
||||
if (ok) {
|
||||
m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len));
|
||||
}
|
||||
@@ -110,6 +109,24 @@ void QtHttpClientWrapper::onClientDataReceived (void) {
|
||||
}
|
||||
switch (m_parsingStatus) { // handle parsing status end/error
|
||||
case RequestParsed: { // a valid request has ben fully parsed
|
||||
// add post data to request
|
||||
if ( m_currentRequest->getCommand() == "POST")
|
||||
{
|
||||
QtHttpPostData postData;
|
||||
QByteArray data = m_currentRequest->getRawData();
|
||||
QList<QByteArray> parts = data.split('&');
|
||||
for (int i = 0; i < parts.size(); ++i)
|
||||
{
|
||||
QList<QByteArray> keyValue = parts.at(i).split('=');
|
||||
QByteArray value;
|
||||
if (keyValue.size()>1)
|
||||
{
|
||||
value = QByteArray::fromPercentEncoding(keyValue.at(1));
|
||||
}
|
||||
postData.insert(QString::fromUtf8(keyValue.at(0)),value);
|
||||
}
|
||||
m_currentRequest->setPostData(postData);
|
||||
}
|
||||
QtHttpReply reply (m_serverHandle);
|
||||
connect (&reply, &QtHttpReply::requestSendHeaders,
|
||||
this, &QtHttpClientWrapper::onReplySendHeadersRequested);
|
||||
|
@@ -16,11 +16,16 @@ public:
|
||||
explicit QtHttpReply (QtHttpServer * parent);
|
||||
|
||||
enum StatusCode {
|
||||
Ok = 200,
|
||||
BadRequest = 400,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
InternalError = 502,
|
||||
Ok = 200,
|
||||
SeeOther = 303,
|
||||
BadRequest = 400,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
MethodNotAllowed = 405,
|
||||
InternalError = 500,
|
||||
NotImplemented = 501,
|
||||
BadGateway = 502,
|
||||
ServiceUnavailable = 503,
|
||||
};
|
||||
|
||||
int getRawDataSize (void) const;
|
||||
|
@@ -3,12 +3,14 @@
|
||||
#include "QtHttpHeader.h"
|
||||
#include "QtHttpServer.h"
|
||||
|
||||
QtHttpRequest::QtHttpRequest (QtHttpServer * parent)
|
||||
QtHttpRequest::QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent)
|
||||
: QObject (parent)
|
||||
, m_url (QUrl ())
|
||||
, m_command (QString ())
|
||||
, m_data (QByteArray ())
|
||||
, m_serverHandle (parent)
|
||||
, m_clientHandle (client)
|
||||
, m_postData (QtHttpPostData())
|
||||
{
|
||||
// set some additional headers
|
||||
addHeader (QtHttpHeader::ContentLength, QByteArrayLiteral ("0"));
|
||||
@@ -36,10 +38,18 @@ QByteArray QtHttpRequest::getRawData (void) const {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
QtHttpPostData QtHttpRequest::getPostData (void) const {
|
||||
return m_postData;
|
||||
}
|
||||
|
||||
QList<QByteArray> QtHttpRequest::getHeadersList (void) const {
|
||||
return m_headersHash.keys ();
|
||||
}
|
||||
|
||||
QtHttpClientWrapper * QtHttpRequest::getClient (void) const {
|
||||
return m_clientHandle;
|
||||
}
|
||||
|
||||
QByteArray QtHttpRequest::getHeader (const QByteArray & header) const {
|
||||
return m_headersHash.value (header, QByteArray ());
|
||||
}
|
||||
@@ -67,3 +77,7 @@ void QtHttpRequest::addHeader (const QByteArray & header, const QByteArray & val
|
||||
void QtHttpRequest::appendRawData (const QByteArray & data) {
|
||||
m_data.append (data);
|
||||
}
|
||||
|
||||
void QtHttpRequest::setPostData (const QtHttpPostData & data) {
|
||||
m_postData = data;
|
||||
}
|
||||
|
@@ -7,28 +7,35 @@
|
||||
#include <QHash>
|
||||
#include <QUrl>
|
||||
#include <QHostAddress>
|
||||
#include <QMap>
|
||||
|
||||
class QtHttpServer;
|
||||
class QtHttpClientWrapper;
|
||||
|
||||
using QtHttpPostData = QMap<QString,QByteArray>;
|
||||
|
||||
class QtHttpRequest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit QtHttpRequest (QtHttpServer * parent);
|
||||
explicit QtHttpRequest (QtHttpClientWrapper * client, QtHttpServer * parent);
|
||||
|
||||
struct ClientInfo {
|
||||
QHostAddress serverAddress;
|
||||
QHostAddress clientAddress;
|
||||
QHostAddress serverAddress;
|
||||
QHostAddress clientAddress;
|
||||
};
|
||||
|
||||
int getRawDataSize (void) const;
|
||||
QUrl getUrl (void) const;
|
||||
QString getCommand (void) const;
|
||||
QByteArray getRawData (void) const;
|
||||
QList<QByteArray> getHeadersList (void) const;
|
||||
ClientInfo getClientInfo (void) const;
|
||||
int getRawDataSize (void) const;
|
||||
QUrl getUrl (void) const;
|
||||
QString getCommand (void) const;
|
||||
QByteArray getRawData (void) const;
|
||||
QList<QByteArray> getHeadersList (void) const;
|
||||
QtHttpClientWrapper * getClient (void) const;
|
||||
|
||||
QByteArray getHeader (const QByteArray & header) const;
|
||||
QtHttpPostData getPostData (void) const;
|
||||
|
||||
ClientInfo getClientInfo (void) const;
|
||||
|
||||
public slots:
|
||||
void setUrl (const QUrl & url);
|
||||
@@ -36,14 +43,17 @@ public slots:
|
||||
void setClientInfo (const QHostAddress & server, const QHostAddress & client);
|
||||
void addHeader (const QByteArray & header, const QByteArray & value);
|
||||
void appendRawData (const QByteArray & data);
|
||||
void setPostData (const QtHttpPostData & data);
|
||||
|
||||
private:
|
||||
QUrl m_url;
|
||||
QString m_command;
|
||||
QByteArray m_data;
|
||||
QtHttpServer * m_serverHandle;
|
||||
QtHttpClientWrapper * m_clientHandle;
|
||||
QHash<QByteArray, QByteArray> m_headersHash;
|
||||
ClientInfo m_clientInfo;
|
||||
QtHttpPostData m_postData;
|
||||
};
|
||||
|
||||
#endif // QTHTTPREQUEST_H
|
||||
|
@@ -18,10 +18,18 @@ QtHttpServer::QtHttpServer (QObject * parent)
|
||||
connect (m_sockServer, &QTcpServer::newConnection, this, &QtHttpServer::onClientConnected);
|
||||
}
|
||||
|
||||
const QString QtHttpServer::getServerName (void) const {
|
||||
const QString & QtHttpServer::getServerName (void) const {
|
||||
return m_serverName;
|
||||
}
|
||||
|
||||
quint16 QtHttpServer::getServerPort (void) const {
|
||||
return m_sockServer->serverPort ();
|
||||
}
|
||||
|
||||
QString QtHttpServer::getErrorString (void) const {
|
||||
return m_sockServer->errorString ();
|
||||
}
|
||||
|
||||
void QtHttpServer::start (quint16 port) {
|
||||
if (m_sockServer->listen (QHostAddress::Any, port)) {
|
||||
emit started (m_sockServer->serverPort ());
|
||||
|
@@ -19,8 +19,9 @@ public:
|
||||
explicit QtHttpServer (QObject * parent = Q_NULLPTR);
|
||||
|
||||
static const QString & HTTP_VERSION;
|
||||
|
||||
const QString getServerName (void) const;
|
||||
const QString & getServerName (void) const;
|
||||
quint16 getServerPort (void) const;
|
||||
QString getErrorString (void) const;
|
||||
|
||||
public slots:
|
||||
void start (quint16 port = 0);
|
||||
|
@@ -76,7 +76,7 @@ static inline void printErrorToReply (QtHttpReply * reply, QString errorMessage)
|
||||
void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpReply * reply)
|
||||
{
|
||||
QString command = request->getCommand ();
|
||||
if (command == QStringLiteral ("GET"))
|
||||
if (command == QStringLiteral ("GET") || command == QStringLiteral ("POST"))
|
||||
{
|
||||
QString path = request->getUrl ().path ();
|
||||
QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
|
||||
@@ -87,6 +87,11 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
|
||||
uri_parts.removeAt(0);
|
||||
try
|
||||
{
|
||||
if (command == QStringLiteral ("POST"))
|
||||
{
|
||||
QString postData = request->getRawData();
|
||||
uri_parts.append(postData.split('&', QString::SkipEmptyParts));
|
||||
}
|
||||
_cgi.exec(uri_parts, request, reply);
|
||||
}
|
||||
catch(...)
|
||||
@@ -134,7 +139,7 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
|
||||
}
|
||||
else
|
||||
{
|
||||
printErrorToReply (reply, "Unhandled HTTP/1.1 method " % command % " on static file server !");
|
||||
printErrorToReply (reply, "Unhandled HTTP/1.1 method " % command % " on web server !");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user