-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurlresolver.cpp
42 lines (39 loc) · 1.23 KB
/
urlresolver.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
#include "urlresolver.h"
class Sleeper : public QThread {
public:
static void usleep(unsigned long usecs){QThread::usleep(usecs);}
static void msleep(unsigned long msecs){QThread::msleep(msecs);}
static void sleep(unsigned long secs){QThread::sleep(secs);}
};
QUrl UrlResolver::resolve(const QUrl &origUrl) {
bool done=false;
QNetworkAccessManager nam;
QUrl lastRedirectUrl=origUrl;
QNetworkRequest req=request(origUrl);
QNetworkReply *rep=nam.head(req);
while(!done) {
while(!rep->isFinished()) {
qApp->processEvents();
Sleeper::msleep(200);
printf("wait for reply to continue address resolving...\n");
}
QUrl possibleRedirectUrl = rep->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if(!possibleRedirectUrl.isEmpty() &&
lastRedirectUrl!=possibleRedirectUrl) {
lastRedirectUrl=possibleRedirectUrl;
rep->deleteLater();
req=request(possibleRedirectUrl);
rep=nam.head(req);
} else {
lastRedirectUrl=rep->url();
rep->deleteLater();
done=true;
}
}
return lastRedirectUrl;
}
QNetworkRequest UrlResolver::request(const QUrl &url) {
QNetworkRequest req(url);
req.setRawHeader("User-Agent", "Wget/1.14 (linux-gnu)");
return req;
}