get proxy list in background thread
This commit is contained in:
@@ -14,8 +14,8 @@
|
|||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
#include <QtNetwork/QSslError>
|
#include <QtNetwork/QSslError>
|
||||||
#include <QtCore/QTimer>
|
#include <QtCore/QTimer>
|
||||||
#include <QtCore/QThread>
|
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QThread>
|
||||||
#include <map>
|
#include <map>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -91,6 +91,8 @@ namespace proxy {
|
|||||||
qFatal("connect failed");
|
qFatal("connect failed");
|
||||||
if (!connect(&_timeout2, SIGNAL(timeout()), SLOT(timeout())))
|
if (!connect(&_timeout2, SIGNAL(timeout()), SLOT(timeout())))
|
||||||
qFatal("connect failed");
|
qFatal("connect failed");
|
||||||
|
if (!connect(this, SIGNAL(finished()), SLOT(threadFinished())))
|
||||||
|
qFatal("connect failed");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
virtual ~Interface() {}
|
virtual ~Interface() {}
|
||||||
@@ -110,12 +112,23 @@ namespace proxy {
|
|||||||
@param timeout [ms] time to give up search */
|
@param timeout [ms] time to give up search */
|
||||||
void proxy(const std::string& url,
|
void proxy(const std::string& url,
|
||||||
int timeout1=1000, int timeout2=30000) {
|
int timeout1=1000, int timeout2=30000) {
|
||||||
|
qDebug()<<"Search proxy for URL, direct and default"
|
||||||
|
<<"url="<<url.data()<<"timeout1="<<timeout1;
|
||||||
_url = url;
|
_url = url;
|
||||||
|
for (Requests::iterator it(_requests.begin());
|
||||||
|
it!=_requests.end(); ++it)
|
||||||
|
delete it->second.first;
|
||||||
|
_requests.clear();
|
||||||
|
_direct = true; // first try direct access
|
||||||
|
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
||||||
|
setupProxyCheck(directProxy, url);
|
||||||
|
QNetworkProxy defaultProxy;
|
||||||
|
setupProxyCheck(defaultProxy, url);
|
||||||
_timeout2.setSingleShot(true);
|
_timeout2.setSingleShot(true);
|
||||||
_timeout2.setInterval(timeout2);
|
_timeout2.setInterval(timeout2);
|
||||||
_timeout1.setSingleShot(true);
|
_timeout1.setSingleShot(true);
|
||||||
_timeout1.setInterval(timeout1);
|
_timeout1.setInterval(timeout1);
|
||||||
start(); // in own thread to prevent hanging at proxy request
|
_timeout1.start();
|
||||||
}
|
}
|
||||||
//! If Qt Network is available you may check the proxies found
|
//! If Qt Network is available you may check the proxies found
|
||||||
/*! @copydoc proxy(const std::string&, int) */
|
/*! @copydoc proxy(const std::string&, int) */
|
||||||
@@ -145,7 +158,8 @@ namespace proxy {
|
|||||||
case QNetworkProxy::HttpProxy:
|
case QNetworkProxy::HttpProxy:
|
||||||
return QString("http://%1:%2").arg(p.hostName()).arg(p.port());
|
return QString("http://%1:%2").arg(p.hostName()).arg(p.port());
|
||||||
case QNetworkProxy::HttpCachingProxy:
|
case QNetworkProxy::HttpCachingProxy:
|
||||||
return QString("http-cache://%1:%2").arg(p.hostName()).arg(p.port());
|
return QString("http-cache://%1:%2").arg(p.hostName())
|
||||||
|
.arg(p.port());
|
||||||
case QNetworkProxy::FtpCachingProxy:
|
case QNetworkProxy::FtpCachingProxy:
|
||||||
return QString("ftp-cache://%1:%2").arg(p.hostName()).arg(p.port());
|
return QString("ftp-cache://%1:%2").arg(p.hostName()).arg(p.port());
|
||||||
}
|
}
|
||||||
@@ -156,23 +170,6 @@ namespace proxy {
|
|||||||
void proxyFound(const QUrl&, const QNetworkProxy&);
|
void proxyFound(const QUrl&, const QNetworkProxy&);
|
||||||
//! Signals a timeout, and no valid proxy for the URL requested earlier.
|
//! Signals a timeout, and no valid proxy for the URL requested earlier.
|
||||||
void proxyError(QNetworkReply::NetworkError);
|
void proxyError(QNetworkReply::NetworkError);
|
||||||
protected:
|
|
||||||
void run() {
|
|
||||||
qDebug()<<"Search proxy for URL, direct and default"
|
|
||||||
<<"url="<<_url.data();
|
|
||||||
for (Requests::iterator it(_requests.begin());
|
|
||||||
it!=_requests.end(); ++it)
|
|
||||||
delete it->second.first;
|
|
||||||
_requests.clear();
|
|
||||||
_direct = true; // first try direct access
|
|
||||||
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
|
||||||
setupProxyCheck(directProxy, _url);
|
|
||||||
QNetworkProxy defaultProxy;
|
|
||||||
setupProxyCheck(defaultProxy, _url);
|
|
||||||
_timeout1.start();
|
|
||||||
exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupProxyCheck(const QNetworkProxy& prxy, const std::string& url) {
|
void setupProxyCheck(const QNetworkProxy& prxy, const std::string& url) {
|
||||||
qDebug()<<"Testing proxy for url:"<<toString(prxy)<<"url="<<url.data();
|
qDebug()<<"Testing proxy for url:"<<toString(prxy)<<"url="<<url.data();
|
||||||
@@ -214,20 +211,8 @@ namespace proxy {
|
|||||||
qDebug()<<"Direct or preconfigured proxy not available,"
|
qDebug()<<"Direct or preconfigured proxy not available,"
|
||||||
" try autoproxy negotiation";
|
" try autoproxy negotiation";
|
||||||
_direct = false;
|
_direct = false;
|
||||||
List l(proxies(url));
|
_url = url;
|
||||||
for (List::const_iterator it(l.begin()); it!=l.end(); ++it) {
|
start(); // autoproxy detection in own thread
|
||||||
QNetworkProxy prxy((it->type==DEFAULT?QNetworkProxy::DefaultProxy
|
|
||||||
:(it->type==HTTP?QNetworkProxy::HttpProxy
|
|
||||||
:(it->type==SOCKS?QNetworkProxy::Socks5Proxy
|
|
||||||
:QNetworkProxy::NoProxy))),
|
|
||||||
QString::fromStdString(it->host), it->port);
|
|
||||||
setupProxyCheck(prxy, url);
|
|
||||||
}
|
|
||||||
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
|
||||||
setupProxyCheck(directProxy, url);
|
|
||||||
QNetworkProxy defaultProxy;
|
|
||||||
setupProxyCheck(defaultProxy, url);
|
|
||||||
_timeout2.start();
|
|
||||||
} else {
|
} else {
|
||||||
qDebug()<<"No proxy at all, giving up - offline?";
|
qDebug()<<"No proxy at all, giving up - offline?";
|
||||||
proxyError(QNetworkReply::TimeoutError);
|
proxyError(QNetworkReply::TimeoutError);
|
||||||
@@ -260,6 +245,26 @@ namespace proxy {
|
|||||||
for (QList<QSslError>::const_iterator it(l.begin()); it!=l.end(); ++it)
|
for (QList<QSslError>::const_iterator it(l.begin()); it!=l.end(); ++it)
|
||||||
qDebug()<<" SSL-Error -> "<<it->errorString();
|
qDebug()<<" SSL-Error -> "<<it->errorString();
|
||||||
}
|
}
|
||||||
|
void threadFinished() {
|
||||||
|
for (List::const_iterator it(_proxies.begin());
|
||||||
|
it!=_proxies.end(); ++it) {
|
||||||
|
QNetworkProxy prxy((it->type==DEFAULT?QNetworkProxy::DefaultProxy
|
||||||
|
:(it->type==HTTP?QNetworkProxy::HttpProxy
|
||||||
|
:(it->type==SOCKS?QNetworkProxy::Socks5Proxy
|
||||||
|
:QNetworkProxy::NoProxy))),
|
||||||
|
QString::fromStdString(it->host), it->port);
|
||||||
|
setupProxyCheck(prxy, _url);
|
||||||
|
}
|
||||||
|
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
||||||
|
setupProxyCheck(directProxy, _url);
|
||||||
|
QNetworkProxy defaultProxy;
|
||||||
|
setupProxyCheck(defaultProxy, _url);
|
||||||
|
_timeout2.start();
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
void run() {
|
||||||
|
_proxies = proxies(_url);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
typedef std::map
|
typedef std::map
|
||||||
<QNetworkReply*, std::pair<QNetworkAccessManager*, QNetworkProxy> >
|
<QNetworkReply*, std::pair<QNetworkAccessManager*, QNetworkProxy> >
|
||||||
@@ -268,6 +273,7 @@ namespace proxy {
|
|||||||
bool _direct;
|
bool _direct;
|
||||||
QTimer _timeout1;
|
QTimer _timeout1;
|
||||||
QTimer _timeout2;
|
QTimer _timeout2;
|
||||||
|
List _proxies;
|
||||||
std::string _url;
|
std::string _url;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user