-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpapi.cpp
More file actions
145 lines (140 loc) · 6.82 KB
/
Copy pathhttpapi.cpp
File metadata and controls
145 lines (140 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "httpapi.h"
#include "json.h"
#include "mainwindow.h"
#include "config.h"
#include "feedserver.h"
#include <QJsonDocument>
#include <QJsonObject>
using namespace qhttp;
using namespace qhttp::server;
HttpApi::HttpApi(QObject *parent) : QObject(parent),
_httpServer(new qhttp::server::QHttpServer(parent))
{
}
/*
* Warning: QHttp is running in a separate thread from the Qt UI, so calls to the
* rest of the code need to be handled carefully.
* The safest way to synchronize is probably through signals, signals should be
* queued by default when coming from another thread (see `Qt::QueuedConnection`).
* Otherwise make sure that data are properly protected by a mutex.
*/
int HttpApi::start(QString port, MainWindow *mainWindow)
{
this->_httpServer->listen(port,
[mainWindow,this](qhttp::server::QHttpRequest *req, qhttp::server::QHttpResponse* res)
{
req->collectData();
req->onEnd([req, res, mainWindow,this]() {
TStatusCode status = qhttp::ESTATUS_OK;
QByteArray returnValue;
switch (req->method()) {
case EHTTP_GET: {
if (req->url().path() == "/feeds") {
QVariantList list;
QMutexLocker locker(mainWindow->getFeedMutex());
for (FeedServer &fe : mainWindow->getFeedServerList()) {
QVariantMap feed;
feed["label"] = fe.label;
feed["url"] = fe.url;
feed["enabled"] = QString::number(fe.enabled);
list.append(feed);
}
returnValue = Json::serialize(QVariant(list));
} else if (req->url().path() == "/images") {
QVariantList list;
QMutexLocker locker(mainWindow->_imageList->GetMutex());
for (QVariantMap &m : mainWindow->_imageList->imageList()) {
QVariantMap image;
image["name"] = m["name"].value<QString>();
image["version"] = m["version"].value<QString>();
image["release_date"] = m["release_date"].value<QString>();
image["uri"] = m["uri"].value<QString>();
list.append(image);
}
returnValue = Json::serialize(QVariant(list));
} else if (req->url().path() == "/status"){
QVariantMap tezi_status;
switch (mainWindow->getTeziState()) {
case TEZI_IDLE:
tezi_status["status"] = "idle";
break;
case TEZI_SCANNING:
tezi_status["status"] = "scanning";
break;
case TEZI_INSTALLING:
tezi_status["status"] = "installing";
break;
case TEZI_INSTALLED:
tezi_status["status"] = "installed";
break;
case TEZI_ABORTED:
tezi_status["status"] = "aborted";
break;
case TEZI_FAILED:
tezi_status["status"] = "failed";
break;
default:
tezi_status["status"] = "unkown";
break;
}
returnValue = Json::serialize(QVariant(tezi_status));
} else {
QString info("Toradex Easy Installer " VERSION_NUMBER " HTTP API");
returnValue = info.toUtf8();
}
}
break;
case EHTTP_POST: {
if (req->url().path() == "/feeds") {
QJsonDocument doc = QJsonDocument::fromJson(req->body());
if (doc.isNull()) {
status = qhttp::ESTATUS_BAD_REQUEST;
} else {
QJsonObject object = doc.object();
if (object.contains("feed_url")) {
emit this->newImageUrl(object["feed_url"].toString());
}
}
} else if (req->url().path() == "/images") {
QJsonDocument doc = QJsonDocument::fromJson(req->body());
if (doc.isNull() || mainWindow->getTeziState() >= TEZI_INSTALLING){
status = qhttp::ESTATUS_BAD_REQUEST;
} else {
QJsonObject object = doc.object();
bool acceptAllLicenses = false;
if (object.contains("accept_all_licenses"))
acceptAllLicenses = object["accept_all_licenses"].toBool();
if (object.contains("image_id")) {
if (!object["image_id"].isDouble()) {
status = qhttp::ESTATUS_BAD_REQUEST;
break;
}
int imageId = object["image_id"].toInt();
QMutexLocker locker(mainWindow->_imageList->GetMutex());
if (mainWindow->_imageList->imageList().count() < imageId) {
status = qhttp::ESTATUS_BAD_REQUEST;
break;
}
mainWindow->_acceptAllLicenses = acceptAllLicenses;
emit this->httpApiInstallImageById(mainWindow->_imageList->imageList()[imageId], false);
}
if (object.contains("image_url")) {
mainWindow->_acceptAllLicenses = acceptAllLicenses;
emit this->httpApiInstallImageByUrl(object["image_url"].toString(), ImageSource::SOURCE_NETWORK);
}
}
} else {
status = qhttp::ESTATUS_BAD_REQUEST;
}
}
break;
default:
status = qhttp::ESTATUS_BAD_REQUEST;
break;
}
res->setStatusCode(status);
res->end(returnValue);
});
});
return 1;
}