forked from QuiteRSS/quiterss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestfeed.cpp
375 lines (328 loc) · 13.5 KB
/
requestfeed.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* ============================================================
* QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
* Copyright (C) 2011-2021 QuiteRSS Team <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* ============================================================ */
#include "requestfeed.h"
#include "VersionNo.h"
#include "mainapplication.h"
#include "globals.h"
#include <QDebug>
#include <QtSql>
#include <qzregexp.h>
#define REPLY_MAX_COUNT 10
RequestFeed::RequestFeed(int timeoutRequest, int numberRequests,
int numberRepeats, QObject *parent)
: QObject(parent)
, networkManager_(NULL)
, timeoutRequest_(timeoutRequest)
, numberRequests_(numberRequests)
, numberRepeats_(numberRepeats)
{
setObjectName("requestFeed_");
timeout_ = new QTimer(this);
timeout_->setInterval(1000);
connect(timeout_, SIGNAL(timeout()), this, SLOT(slotRequestTimeout()));
getUrlTimer_ = new QTimer(this);
getUrlTimer_->setSingleShot(true);
getUrlTimer_->setInterval(50);
connect(getUrlTimer_, SIGNAL(timeout()), this, SLOT(getQueuedUrl()));
connect(this, SIGNAL(signalHead(QUrl,int,QString,QDateTime,int)),
SLOT(slotHead(QUrl,int,QString,QDateTime,int)),
Qt::QueuedConnection);
connect(this, SIGNAL(signalGet(QUrl,int,QString,QDateTime,int)),
SLOT(slotGet(QUrl,int,QString,QDateTime,int)),
Qt::QueuedConnection);
}
RequestFeed::~RequestFeed()
{
}
void RequestFeed::disconnectObjects()
{
disconnect(this);
if (networkManager_)
networkManager_->disconnect(networkManager_);
}
/** @brief Put URL in request queue
*----------------------------------------------------------------------------*/
void RequestFeed::requestUrl(int id, QString urlString,
QDateTime date, QString userInfo)
{
if (!networkManager_) {
networkManager_ = new NetworkManager(true, this);
connect(networkManager_, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finished(QNetworkReply*)));
}
if (!timeout_->isActive())
timeout_->start();
idsQueue_.enqueue(id);
feedsQueue_.enqueue(urlString);
dateQueue_.enqueue(date);
userInfo_.enqueue(userInfo);
if (!getUrlTimer_->isActive())
getUrlTimer_->start();
qDebug() << "requestUrl() <<" << urlString << "countQueue=" << feedsQueue_.count();
}
void RequestFeed::stopRequest()
{
while (!feedsQueue_.isEmpty()) {
int feedId = idsQueue_.dequeue();
QString feedUrl = feedsQueue_.dequeue();
dateQueue_.clear();
userInfo_.clear();
emit getUrlDone(feedsQueue_.count(), feedId, feedUrl);
}
}
/** @brief Process request queue on timer timeouts
*----------------------------------------------------------------------------*/
void RequestFeed::getQueuedUrl()
{
if ((currentFeeds_.count() >= numberRequests_) ||
(currentFeeds_.count() >= REPLY_MAX_COUNT)) {
getUrlTimer_->start();
return;
}
if (!feedsQueue_.isEmpty()) {
getUrlTimer_->start();
QString feedUrl = feedsQueue_.head();
if (hostList_.contains(QUrl(feedUrl).host())) {
foreach (QString url, currentFeeds_) {
if (QUrl(url).host() == QUrl(feedUrl).host()) {
return;
}
}
}
int feedId = idsQueue_.dequeue();
feedUrl = feedsQueue_.dequeue();
emit setStatusFeed(feedId, "1 Update");
QUrl getUrl = QUrl::fromEncoded(feedUrl.toUtf8());
QString userInfo = userInfo_.dequeue();
if (!userInfo.isEmpty()) {
getUrl.setUserInfo(userInfo);
// getUrl.addQueryItem("auth", getUrl.scheme());
}
qDebug() << "getQueuedUrl() >>" << feedUrl << "countQueue=" << feedsQueue_.count();
QDateTime currentDate = dateQueue_.dequeue();
if (currentDate.isValid())
emit signalHead(getUrl, feedId, feedUrl, currentDate);
else
emit signalGet(getUrl, feedId, feedUrl, currentDate);
}
}
/** @brief Prepare and send network request to get head
*----------------------------------------------------------------------------*/
void RequestFeed::slotHead(const QUrl &getUrl, const int &id, const QString &feedUrl,
const QDateTime &date, const int &count)
{
if (count)
Common::sleep(30);
qDebug() << objectName() << "::head:" << getUrl.toEncoded() << "feed:" << feedUrl << "countRepeats:" << count;
QNetworkRequest request(getUrl);
request.setRawHeader("User-Agent", globals.userAgent().toUtf8());
currentUrls_.append(getUrl);
currentIds_.append(id);
currentFeeds_.append(feedUrl);
currentDates_.append(date);
currentCount_.append(count);
currentHead_.append(true);
currentTime_.append(timeoutRequest_);
QNetworkReply *reply = networkManager_->head(request);
reply->setProperty("feedReply", QVariant(true));
requestUrl_.append(reply->url());
networkReply_.append(reply);
}
/** @brief Prepare and send network request to get all data
*----------------------------------------------------------------------------*/
void RequestFeed::slotGet(const QUrl &getUrl, const int &id, const QString &feedUrl,
const QDateTime &date, const int &count)
{
if (count)
Common::sleep(30);
qDebug() << objectName() << "::get:" << getUrl.toEncoded() << "feed:" << feedUrl << "countRepeats:" <<count;
QNetworkRequest request(getUrl);
request.setRawHeader("Accept", "application/atom+xml,application/rss+xml;q=0.9,application/xml;q=0.8,text/xml;q=0.7,*/*;q=0.6");
request.setRawHeader("User-Agent", globals.userAgent().toUtf8());
currentUrls_.append(getUrl);
currentIds_.append(id);
currentFeeds_.append(feedUrl);
currentDates_.append(date);
currentCount_.append(count);
currentHead_.append(false);
currentTime_.append(timeoutRequest_);
QNetworkReply *reply = networkManager_->get(request);
reply->setProperty("feedReply", QVariant(true));
requestUrl_.append(reply->url());
networkReply_.append(reply);
}
/** @brief Process network reply
*----------------------------------------------------------------------------*/
void RequestFeed::finished(QNetworkReply *reply)
{
QUrl replyUrl = reply->url();
qDebug() << "reply.finished():" << replyUrl.toString();
qDebug() << reply->header(QNetworkRequest::ContentTypeHeader);
qDebug() << reply->header(QNetworkRequest::ContentLengthHeader);
qDebug() << reply->header(QNetworkRequest::LocationHeader);
qDebug() << reply->header(QNetworkRequest::LastModifiedHeader);
qDebug() << reply->header(QNetworkRequest::CookieHeader);
qDebug() << reply->header(QNetworkRequest::SetCookieHeader);
int currentReplyIndex = currentUrls_.indexOf(replyUrl);
if (currentReplyIndex >= 0) {
currentTime_.removeAt(currentReplyIndex);
currentUrls_.removeAt(currentReplyIndex);
int feedId = currentIds_.takeAt(currentReplyIndex);
QString feedUrl = currentFeeds_.takeAt(currentReplyIndex);
QDateTime feedDate = currentDates_.takeAt(currentReplyIndex);
int count = currentCount_.takeAt(currentReplyIndex) + 1;
bool headOk = currentHead_.takeAt(currentReplyIndex);
if (reply->error() != QNetworkReply::NoError) {
qDebug() << " error retrieving RSS feed:" << reply->error() << reply->errorString();
if (!headOk) {
if (reply->error() == QNetworkReply::AuthenticationRequiredError)
emit getUrlDone(-2, feedId, feedUrl, tr("Server requires authentication!"));
else if (reply->error() == QNetworkReply::ContentNotFoundError)
emit getUrlDone(-5, feedId, feedUrl, tr("Server replied: Not Found!"));
else {
if (reply->errorString().contains("Service Temporarily Unavailable")) {
if (!hostList_.contains(QUrl(feedUrl).host())) {
hostList_.append(QUrl(feedUrl).host());
count--;
}
}
if (count < numberRepeats_) {
emit signalGet(replyUrl, feedId, feedUrl, feedDate, count);
} else {
emit getUrlDone(-1, feedId, feedUrl, QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
}
}
} else {
emit signalGet(replyUrl, feedId, feedUrl, feedDate);
}
} else {
QUrl redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (redirectionTarget.isValid()) {
if (count < (numberRepeats_ + 3)) {
if (headOk && (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302)) {
emit signalGet(replyUrl, feedId, feedUrl, feedDate);
} else {
QString host(QUrl::fromEncoded(feedUrl.toUtf8()).host());
if (redirectionTarget.host().isEmpty()) {
if (redirectionTarget.path() == ".") {
if (redirectionTarget.hasQuery()) {
#if QT_VERSION >= 0x050000
QString query = redirectionTarget.query();
redirectionTarget.setUrl(replyUrl.scheme() + "://" + host + replyUrl.path());
redirectionTarget.setQuery(query);
#else
QByteArray query = redirectionTarget.encodedQuery();
redirectionTarget.setUrl(replyUrl.scheme() + "://" + host + replyUrl.path());
redirectionTarget.setEncodedQuery(query);
#endif
}
} else {
redirectionTarget.setUrl(replyUrl.scheme() + "://" + host + redirectionTarget.toString());
}
}
if (redirectionTarget.scheme().isEmpty())
redirectionTarget.setScheme(QUrl(feedUrl).scheme());
if (reply->operation() == QNetworkAccessManager::HeadOperation) {
qDebug() << objectName() << " head redirect..." << redirectionTarget.toString();
emit signalHead(redirectionTarget, feedId, feedUrl, feedDate, count);
}
else {
qDebug() << objectName() << " get redirect..." << redirectionTarget.toString();
emit signalGet(redirectionTarget, feedId, feedUrl, feedDate, count);
}
}
} else {
emit getUrlDone(-4, feedId, feedUrl, tr("Redirect error!"));
}
} else {
QDateTime replyDate = reply->header(QNetworkRequest::LastModifiedHeader).toDateTime();
QDateTime replyLocalDate = QDateTime(replyDate.date(), replyDate.time());
qDebug() << feedDate << replyDate << replyLocalDate;
qDebug() << feedDate.toMSecsSinceEpoch() << replyDate.toMSecsSinceEpoch() << replyLocalDate.toMSecsSinceEpoch();
if ((reply->operation() == QNetworkAccessManager::HeadOperation) &&
((!feedDate.isValid()) || (!replyLocalDate.isValid()) ||
(feedDate != replyLocalDate) || !replyDate.toMSecsSinceEpoch())) {
emit signalGet(replyUrl, feedId, feedUrl, feedDate);
}
else {
QString codecName;
QzRegExp rx("charset=([^\t]+)$", Qt::CaseInsensitive);
int pos = rx.indexIn(reply->header(QNetworkRequest::ContentTypeHeader).toString());
if (pos > -1) {
codecName = rx.cap(1);
}
QByteArray data = reply->readAll();
data = data.trimmed();
rx.setPattern("&(?!([a-z0-9#]+;))");
pos = 0;
while ((pos = rx.indexIn(QString::fromLatin1(data), pos)) != -1) {
data.replace(pos, 1, "&");
pos += 1;
}
data.replace("<br>", "<br/>");
if (data.indexOf("</rss>") > 0)
data.resize(data.indexOf("</rss>") + 6);
if (data.indexOf("</feed>") > 0)
data.resize(data.indexOf("</feed>") + 7);
if (data.indexOf("</rdf:RDF>") > 0)
data.resize(data.indexOf("</rdf:RDF>") + 10);
emit getUrlDone(feedsQueue_.count(), feedId, feedUrl, "", data, replyLocalDate, codecName);
}
}
}
} else {
qCritical() << "Request Url error: " << replyUrl.toString() << reply->errorString();
}
int replyIndex = requestUrl_.indexOf(replyUrl);
if (replyIndex >= 0) {
requestUrl_.removeAt(replyIndex);
networkReply_.removeAt(replyIndex);
}
reply->abort();
reply->deleteLater();
}
/** @brief Timeout to delete network requests which has no answer
*----------------------------------------------------------------------------*/
void RequestFeed::slotRequestTimeout()
{
for (int i = currentTime_.count() - 1; i >= 0; i--) {
int time = currentTime_.at(i) - 1;
if (time <= 0) {
QUrl url = currentUrls_.takeAt(i);
int feedId = currentIds_.takeAt(i);
QString feedUrl = currentFeeds_.takeAt(i);
QDateTime feedDate = currentDates_.takeAt(i);
int count = currentCount_.takeAt(i) + 1;
currentTime_.removeAt(i);
currentHead_.removeAt(i);
int replyIndex = requestUrl_.indexOf(url);
if (replyIndex >= 0) {
QUrl replyUrl = requestUrl_.takeAt(replyIndex);
QNetworkReply *reply = networkReply_.takeAt(replyIndex);
reply->deleteLater();
if (count < numberRepeats_) {
emit signalGet(replyUrl, feedId, feedUrl, feedDate, count);
} else {
emit getUrlDone(-3, feedId, feedUrl, tr("Request timeout!"));
}
}
} else {
currentTime_.replace(i, time);
}
}
}