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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/******************************************************************************
* Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "basejob.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>
#include <QtCore/QTimer>
#include "../connectiondata.h"
using namespace QMatrixClient;
struct NetworkReplyDeleter : public QScopedPointerDeleteLater
{
static inline void cleanup(QNetworkReply* reply)
{
if (reply && reply->isRunning())
reply->abort();
QScopedPointerDeleteLater::cleanup(reply);
}
};
class BaseJob::Private
{
public:
Private(ConnectionData* c, JobHttpType t, bool nt)
: connection(c), type(t), needsToken(nt)
, reply(nullptr), status(NoError)
{}
ConnectionData* connection;
JobHttpType type;
bool needsToken;
QScopedPointer<QNetworkReply, NetworkReplyDeleter> reply;
Status status;
};
BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, QString name, bool needsToken)
: d(new Private(connection, type, needsToken))
{
setObjectName(name);
qDebug() << "Job" << objectName() << " created";
}
BaseJob::~BaseJob()
{
qDebug() << "Job" << objectName() << " destroyed";
}
ConnectionData* BaseJob::connection() const
{
return d->connection;
}
QJsonObject BaseJob::data() const
{
return QJsonObject();
}
QUrlQuery BaseJob::query() const
{
return QUrlQuery();
}
void BaseJob::start()
{
QUrl url = d->connection->baseUrl();
url.setPath( url.path() + "/" + apiPath() );
QUrlQuery query = this->query();
if( d->needsToken )
query.addQueryItem("access_token", connection()->token());
url.setQuery(query);
QNetworkRequest req = QNetworkRequest(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
req.setMaximumRedirectsAllowed(10);
#endif
QJsonDocument data = QJsonDocument(this->data());
switch( d->type )
{
case JobHttpType::GetJob:
d->reply.reset( d->connection->nam()->get(req) );
break;
case JobHttpType::PostJob:
d->reply.reset( d->connection->nam()->post(req, data.toJson()) );
break;
case JobHttpType::PutJob:
d->reply.reset( d->connection->nam()->put(req, data.toJson()) );
break;
}
connect( d->reply.data(), &QNetworkReply::sslErrors, this, &BaseJob::sslErrors );
connect( d->reply.data(), &QNetworkReply::finished, this, &BaseJob::gotReply );
QTimer::singleShot( 120*1000, this, SLOT(timeout()) );
// connect( d->reply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
// this, &BaseJob::networkError ); // http://doc.qt.io/qt-5/qnetworkreply.html#error-1
}
void BaseJob::gotReply()
{
setStatus(checkReply(d->reply.data()));
if (status().good())
setStatus(parseReply(d->reply->readAll()));
finishJob(true);
}
BaseJob::Status BaseJob::checkReply(QNetworkReply* reply) const
{
switch( reply->error() )
{
case QNetworkReply::NoError:
return NoError;
case QNetworkReply::AuthenticationRequiredError:
case QNetworkReply::ContentAccessDenied:
case QNetworkReply::ContentOperationNotPermittedError:
qDebug() << "Content access error, Qt error code:" << reply->error();
return { ContentAccessError, reply->errorString() };
default:
qDebug() << "NetworkError, Qt error code:" << reply->error();
return { NetworkError, reply->errorString() };
}
}
BaseJob::Status BaseJob::parseReply(QByteArray data)
{
QJsonParseError error;
QJsonDocument json = QJsonDocument::fromJson(data, &error);
if( error.error == QJsonParseError::NoError )
return parseJson(json);
else
return { JsonParseError, error.errorString() };
}
BaseJob::Status BaseJob::parseJson(const QJsonDocument&)
{
return Success;
}
void BaseJob::finishJob(bool emitResult)
{
if (!d->reply)
{
qWarning() << objectName()
<< ": empty network reply (finishJob() called more than once?)";
return;
}
// Notify those that are interested in any completion of the job (including killing)
emit finished(this);
if (emitResult) {
emit result(this);
if (error())
emit failure(this);
else
emit success(this);
}
d->reply.reset();
deleteLater();
}
BaseJob::Status BaseJob::status() const
{
return d->status;
}
int BaseJob::error() const
{
return d->status.code;
}
QString BaseJob::errorString() const
{
return d->status.message;
}
void BaseJob::setStatus(Status s)
{
d->status = s;
if (!s.good())
{
qWarning() << QString("Job %1 status: %2, code %3")
.arg(objectName()).arg(s.message).arg(s.code);
}
}
void BaseJob::setStatus(int code, QString message)
{
setStatus({ code, message });
}
void BaseJob::abandon()
{
finishJob(false);
}
void BaseJob::timeout()
{
setStatus( TimeoutError, "The job has timed out" );
finishJob(true);
}
void BaseJob::sslErrors(const QList<QSslError>& errors)
{
foreach (const QSslError &error, errors) {
qWarning() << "SSL ERROR" << error.errorString();
}
d->reply->ignoreSslErrors(); // TODO: insecure! should prompt user first
}
|