From d748160cca44311814b793b77ee0966c2fe9cbf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Mon, 28 Jun 2010 11:48:58 +0000 Subject: [PATCH] new mthod ping and called from GUI-detection, refs #5 --- proxyface/autoproxy.hxx | 54 ++++++++++++++++++++++++----------------- proxyface/proxy.hxx | 47 +++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/proxyface/autoproxy.hxx b/proxyface/autoproxy.hxx index b40317a..97fb832 100644 --- a/proxyface/autoproxy.hxx +++ b/proxyface/autoproxy.hxx @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #ifndef Q_OS_WIN32 @@ -111,6 +112,32 @@ namespace proxy { //! Get list of proxies for a given URL. virtual List proxies(const std::string& url) = 0; #ifdef QT_NETWORK_LIB + //! Reset, stop all outstanding checks + void reset() { + _timeout1.stop(); + _timeout2.stop(); + for (Requests::iterator it(_requests.begin()); + it!=_requests.end(); ++it) + clean(it->second.first); + _requests.clear(); + } + //! Network Ping: Check access to a given address using default proxy. + void ping(const std::string& url, int timeout2=30000) { + reset(); + _timeout2.setSingleShot(true); + _timeout2.setInterval(timeout2); + _url = url; + _direct = false; // simulate second try + QNetworkProxy defaultProxy; + setupProxyCheck(defaultProxy, url); + _timeout2.start(); + } + void ping(const QUrl& url, int timeout2=30000) { + ping(url.toString().toStdString(), timeout2); + } + void ping(const QString& url, int timeout2=30000) { + ping(url.toStdString(), timeout2); + } //! If Qt Network is available you may check the proxies found /*! First checks for direct access to the target. If this is not possible, starts scan for proxies. @@ -127,11 +154,8 @@ namespace proxy { PROXYFACE_LOG; qDebug()<<"Search proxy for URL, direct and default" <<"url="<second.first); - _requests.clear(); _direct = true; // first try direct access QNetworkProxy directProxy(QNetworkProxy::NoProxy); setupProxyCheck(directProxy, url); @@ -238,21 +262,12 @@ namespace proxy { private Q_SLOTS: void timeout() { PROXYFACE_LOG; - _timeout1.stop(); - _timeout2.stop(); - std::string url; - for (Requests::iterator it(_requests.begin()); - it!=_requests.end(); ++it) { - url = it->first->url().toString().toStdString(); - clean(it->second.first); - } - _requests.clear(); - qDebug()<<"Proxy detection timed out"<<"url="<url()); - for (Requests::iterator it(_requests.begin()); - it!=_requests.end(); ++it) - clean(it->second.first); - _requests.clear(); + reset(); qDebug()<<"SUCCESS - Valid proxy found for url:" <<"url="<currentIndex()) { - case 0: { + case 0: try { _url->setEnabled(false); _port->setEnabled(false); type=QNetworkProxy::NoProxy; + _auto.proxy(_testUrl); + } catch (...) {} break; + case 1: { + type=QNetworkProxy::HttpProxy; + QNetworkProxy::setApplicationProxy + (QNetworkProxy(type, _url->text(), _port->value())); + _auto.ping(_testUrl); + } break; + case 2: { + type=QNetworkProxy::Socks5Proxy; + QNetworkProxy::setApplicationProxy + (QNetworkProxy(type, _url->text(), _port->value())); + _auto.ping(_testUrl); } break; - case 1: type=QNetworkProxy::HttpProxy; break; - case 2: type=QNetworkProxy::Socks5Proxy; break; } - QNetworkProxy::setApplicationProxy - (QNetworkProxy(type, _url->text(), _port->value())); - try { - _auto.proxy(_testUrl); - } catch (...) {} + if (!retry) detecting(); } Q_SIGNALS: - + + void detecting(); void proxyFound(const QUrl&, const QNetworkProxy&); void proxyError(QNetworkReply::NetworkError); void temporaryError(QNetworkReply::NetworkError, QString, QString); @@ -77,9 +90,13 @@ namespace gui { public Q_SLOTS: void proxyFoundSlot(const QUrl&, const QNetworkProxy& p) { - QNetworkProxy::setApplicationProxy(p); - _url->setText(p.hostName()); - _port->setValue(p.port()); + QNetworkProxy prx(p.type()==QNetworkProxy::DefaultProxy + ?QNetworkProxy::applicationProxy():p); + if (prx.type()==QNetworkProxy::DefaultProxy) return; + _url->setText(prx.hostName()); + _port->setValue(prx.port()); + if (p.type()==QNetworkProxy::DefaultProxy) return; + QNetworkProxy::setApplicationProxy(prx); } void on__type_currentIndexChanged(int index) {