2017-11-20 00:06:45 +01:00
|
|
|
#include "WebJsonRpc.h"
|
|
|
|
#include "QtHttpReply.h"
|
|
|
|
#include "QtHttpRequest.h"
|
|
|
|
#include "QtHttpServer.h"
|
|
|
|
#include "QtHttpClientWrapper.h"
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <api/JsonAPI.h>
|
2017-11-20 00:06:45 +01:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
WebJsonRpc::WebJsonRpc(QtHttpRequest* request, QtHttpServer* server, const bool& localConnection, QtHttpClientWrapper* parent)
|
2017-11-20 00:06:45 +01:00
|
|
|
: QObject(parent)
|
|
|
|
, _server(server)
|
|
|
|
, _wrapper(parent)
|
|
|
|
, _log(Logger::getInstance("HTTPJSONRPC"))
|
|
|
|
{
|
|
|
|
const QString client = request->getClientInfo().clientAddress.toString();
|
2019-07-12 16:54:26 +02:00
|
|
|
_jsonAPI = new JsonAPI(client, _log, localConnection, this, true);
|
2018-12-27 23:11:32 +01:00
|
|
|
connect(_jsonAPI, &JsonAPI::callbackMessage, this, &WebJsonRpc::handleCallback);
|
2017-11-20 00:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebJsonRpc::handleMessage(QtHttpRequest* request)
|
|
|
|
{
|
2019-07-12 16:54:26 +02:00
|
|
|
QByteArray header = request->getHeader("Authorization");
|
2017-11-20 00:06:45 +01:00
|
|
|
QByteArray data = request->getRawData();
|
|
|
|
_unlocked = true;
|
2019-07-12 16:54:26 +02:00
|
|
|
_jsonAPI->handleMessage(data,header);
|
2017-11-20 00:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebJsonRpc::handleCallback(QJsonObject obj)
|
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// guard against wrong callbacks; TODO: Remove when JSONAPI is more solid
|
2017-11-20 00:06:45 +01:00
|
|
|
if(!_unlocked) return;
|
|
|
|
_unlocked = false;
|
|
|
|
// construct reply with headers timestamp and server name
|
|
|
|
QtHttpReply reply(_server);
|
|
|
|
QJsonDocument doc(obj);
|
|
|
|
reply.addHeader ("Content-Type", "application/json");
|
|
|
|
reply.appendRawData (doc.toJson());
|
|
|
|
_wrapper->sendToClientWithReply(&reply);
|
|
|
|
}
|