get proxy list in background thread

master
Marc Wäckerlin 15 years ago
parent 75b09d79e7
commit 0351735d6c
  1. 74
      proxyface/autoproxy.hxx

@ -14,8 +14,8 @@
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>
#include <QtCore/QTimer>
#include <QtCore/QThread>
#include <QtCore/QDebug>
#include <QtCore/QThread>
#include <map>
#endif
@ -91,6 +91,8 @@ namespace proxy {
qFatal("connect failed");
if (!connect(&_timeout2, SIGNAL(timeout()), SLOT(timeout())))
qFatal("connect failed");
if (!connect(this, SIGNAL(finished()), SLOT(threadFinished())))
qFatal("connect failed");
#endif
}
virtual ~Interface() {}
@ -110,12 +112,23 @@ namespace proxy {
@param timeout [ms] time to give up search */
void proxy(const std::string& url,
int timeout1=1000, int timeout2=30000) {
qDebug()<<"Search proxy for URL, direct and default"
<<"url="<<url.data()<<"timeout1="<<timeout1;
_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.setInterval(timeout2);
_timeout1.setSingleShot(true);
_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
/*! @copydoc proxy(const std::string&, int) */
@ -145,7 +158,8 @@ namespace proxy {
case QNetworkProxy::HttpProxy:
return QString("http://%1:%2").arg(p.hostName()).arg(p.port());
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:
return QString("ftp-cache://%1:%2").arg(p.hostName()).arg(p.port());
}
@ -156,23 +170,6 @@ namespace proxy {
void proxyFound(const QUrl&, const QNetworkProxy&);
//! Signals a timeout, and no valid proxy for the URL requested earlier.
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:
void setupProxyCheck(const QNetworkProxy& prxy, const std::string& url) {
qDebug()<<"Testing proxy for url:"<<toString(prxy)<<"url="<<url.data();
@ -214,20 +211,8 @@ namespace proxy {
qDebug()<<"Direct or preconfigured proxy not available,"
" try autoproxy negotiation";
_direct = false;
List l(proxies(url));
for (List::const_iterator it(l.begin()); it!=l.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();
_url = url;
start(); // autoproxy detection in own thread
} else {
qDebug()<<"No proxy at all, giving up - offline?";
proxyError(QNetworkReply::TimeoutError);
@ -260,6 +245,26 @@ namespace proxy {
for (QList<QSslError>::const_iterator it(l.begin()); it!=l.end(); ++it)
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:
typedef std::map
<QNetworkReply*, std::pair<QNetworkAccessManager*, QNetworkProxy> >
@ -268,6 +273,7 @@ namespace proxy {
bool _direct;
QTimer _timeout1;
QTimer _timeout2;
List _proxies;
std::string _url;
#endif
};

Loading…
Cancel
Save