add http jsonrpc (#450)

This commit is contained in:
brindosch
2017-07-05 23:19:52 +02:00
committed by GitHub
parent a99b7c5f59
commit 622a171808
4 changed files with 59 additions and 19 deletions

View File

@@ -60,10 +60,14 @@ void StaticFileServing::onServerStarted (quint16 port)
txtRecord
);
Debug(_log, "Web Config mDNS responder started");
// json-rpc for http
_jsonProcessor = new JsonProcessor(QString("HTTP-API"),true);
}
void StaticFileServing::onServerStopped () {
Info(_log, "stopped %s", _server->getServerName().toStdString().c_str());
delete _jsonProcessor;
}
void StaticFileServing::onServerError (QString msg)
@@ -115,24 +119,44 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
// special uri handling for server commands
if ( ! uri_parts.empty() && uri_parts.at(0) == "cgi" )
if ( ! uri_parts.empty() )
{
uri_parts.removeAt(0);
try
if(uri_parts.at(0) == "cgi")
{
_cgi.exec(uri_parts, request, reply);
uri_parts.removeAt(0);
try
{
_cgi.exec(uri_parts, request, reply);
}
catch(int err)
{
Error(_log,"Exception while executing cgi %s : %d", path.toStdString().c_str(), err);
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
catch(std::exception &e)
{
Error(_log,"Exception while executing cgi %s : %s", path.toStdString().c_str(), e.what());
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
return;
}
catch(int err)
else if ( uri_parts.at(0) == "json-rpc" )
{
Error(_log,"Exception while executing cgi %s : %d", path.toStdString().c_str(), err);
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
QMetaObject::Connection m_connection;
QByteArray data = request->getRawData();
QtHttpRequest::ClientInfo info = request->getClientInfo();
m_connection = QObject::connect(_jsonProcessor, &JsonProcessor::callbackMessage,
[reply](QJsonObject result) {
QJsonDocument doc(result);
reply->addHeader ("Content-Type", "application/json");
reply->appendRawData (doc.toJson());
});
_jsonProcessor->handleMessage(data,info.clientAddress.toString());
QObject::disconnect( m_connection );
return;
}
catch(std::exception &e)
{
Error(_log,"Exception while executing cgi %s : %s", path.toStdString().c_str(), e.what());
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
return;
}
Q_INIT_RESOURCE(WebConfig);

View File

@@ -12,6 +12,7 @@
#include <hyperion/Hyperion.h>
#include <utils/Logger.h>
#include <utils/JsonProcessor.h>
class StaticFileServing : public QObject {
@@ -34,6 +35,7 @@ private:
QMimeDatabase * _mimeDb;
CgiHandler _cgi;
Logger * _log;
JsonProcessor * _jsonProcessor;
void printErrorToReply (QtHttpReply * reply, QtHttpReply::StatusCode code, QString errorMessage);