-
Notifications
You must be signed in to change notification settings - Fork 0
/
safeworker.cpp
165 lines (140 loc) · 5.16 KB
/
safeworker.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "safeworker.h"
#include "safecalls.h"
#include <QDebug>
#include <QtNetwork/QNetworkRequest>
SafeWorker::SafeWorker(QString host)
{
this->host = host;
manager = new QNetworkAccessManager(this);
}
void SafeWorker::handleData()
{
if(reply->error()) {
emit error(reply->errorString());
}
emit done(this, reply->readAll());
}
void SafeWorker::run()
{
if(!manager) {
return;
} else {
params.addQueryItem(PARAM_CMD, this->cmd);
}
QNetworkRequest req("https://" + this->host + "/");
req.setHeader(QNetworkRequest::UserAgentHeader, "lib2safe/0.1");
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
this->reply = manager->post(req, params.query(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::sslErrors, [=](QList<QSslError> ssl_errors){
qWarning() << "SSL ERRORS DETECTED: \n" << ssl_errors;
});
connect(reply, &QNetworkReply::finished, this, &SafeWorker::handleData);
connect(reply, &QNetworkReply::metaDataChanged, [=](){
QVariant cookie_header = reply->header(QNetworkRequest::SetCookieHeader);
if(cookie_header.isValid() &&
cookie_header.canConvert<QList<QNetworkCookie>>()) {
/* Store any cookie key-value pairs if given */
foreach(auto cookie, cookie_header.value<QList<QNetworkCookie>>()) {
auto kv = cookie.toRawForm(QNetworkCookie::NameAndValueOnly).split('=');
cookies.insert(kv[0], kv[1]);
}
}
});
}
void SafeWorker::pushFile()
{
if(!manager) {
return;
} else {
params.addQueryItem(PARAM_CMD, CALL_PUSH_FILE);
}
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QFile *file = new QFile(this->filepath);
if(!file->open(QIODevice::ReadOnly)) {
qWarning() << "[" << this->id << "] Unable to open" << file->fileName() << "for reading";
delete file;
delete multiPart;
return;
}
file->setParent(multiPart);
foreach(auto param, params.queryItems()) {
QHttpPart metaPart;
metaPart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
metaPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"" + param.first + "\""));
metaPart.setBody(param.second.toUtf8());
multiPart->append(metaPart);
}
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/octet-stream"));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file\"; filename=\"deadbeef\""));
filePart.setBodyDevice(file);
multiPart->append(filePart);
QNetworkRequest req("https://" + this->host + "/");
req.setHeader(QNetworkRequest::UserAgentHeader, "lib2safe/0.1");
QNetworkReply *reply = manager->post(req, multiPart);
multiPart->setParent(reply);
connect(reply, &QNetworkReply::sslErrors, [=](QList<QSslError> ssl_errors) {
qWarning() << "SSL ERRORS DETECTED: \n" << ssl_errors;
});
connect(reply, &QNetworkReply::readyRead, [=](){
emit done(this, reply->readAll());
});
connect(reply, &QNetworkReply::uploadProgress, [=](ulong bytes, ulong total_bytes) {
if(total_bytes > 0 || bytes > 0)
emit progress(bytes, total_bytes);
});
connect(reply, &QNetworkReply::finished, [=]() {
file->close();
if(reply->error()) {
emit error(reply->errorString());
}
reply->deleteLater();
});
}
void SafeWorker::pullFile()
{
if(!manager) {
return;
} else {
params.addQueryItem(PARAM_CMD, CALL_PULL_FILE);
}
QFile *file = new QFile(this->filepath);
if(file->exists()) file->remove();
if(!file->open(QIODevice::Append)) {
qWarning() << "[" << this->id << "] Unable to open" << file->fileName() << "for writing";
delete file;
return;
}
QNetworkRequest req("https://" + this->host + "/");
req.setHeader(QNetworkRequest::UserAgentHeader, "lib2safe/0.1");
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *reply = manager->post(req, params.query(QUrl::FullyEncoded).toUtf8());
file->setParent(reply);
connect(reply, &QNetworkReply::sslErrors, [=](QList<QSslError> ssl_errors) {
qWarning() << "SSL ERRORS DETECTED: \n" << ssl_errors;
});
connect(reply, &QNetworkReply::readyRead, [=](){
file->write(reply->readAll());
file->flush();
});
connect(reply, &QNetworkReply::downloadProgress, [=](ulong bytes, ulong total_bytes) {
if(total_bytes > 0 || bytes > 0)
emit progress(bytes, total_bytes);
});
connect(reply, &QNetworkReply::finished, [=]() {
file->close();
if(reply->error()) {
emit error(reply->errorString());
} else {
emit done(this, 0);
}
reply->deleteLater();
});
}
void SafeWorker::addParam(QString name, QVariant value)
{
params.addQueryItem(name, value.toString());
}