|
|
|
@ -17,6 +17,9 @@ |
|
|
|
|
#include <QtCore/QDebug> |
|
|
|
|
#include <QtCore/QThread> |
|
|
|
|
#include <map> |
|
|
|
|
#ifndef PROXYFACE_LOG |
|
|
|
|
#define PROXYFACE_LOG qDebug()<<__PRETTY_FUNCTION__ |
|
|
|
|
#endif |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#include <list> |
|
|
|
@ -87,6 +90,7 @@ namespace proxy { |
|
|
|
|
//! Keep your instance as long as possible, because of caching.
|
|
|
|
|
Interface() { |
|
|
|
|
#ifdef QT_NETWORK_LIB |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
if (!connect(&_timeout1, SIGNAL(timeout()), SLOT(timeout()))) |
|
|
|
|
qFatal("connect failed"); |
|
|
|
|
if (!connect(&_timeout2, SIGNAL(timeout()), SLOT(timeout()))) |
|
|
|
@ -112,12 +116,13 @@ namespace proxy { |
|
|
|
|
@param timeout [ms] time to give up search */ |
|
|
|
|
void proxy(const std::string& url, |
|
|
|
|
int timeout1=5000, int timeout2=30000) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
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; |
|
|
|
|
clean(it->second.first); |
|
|
|
|
_requests.clear(); |
|
|
|
|
_direct = true; // first try direct access
|
|
|
|
|
QNetworkProxy directProxy(QNetworkProxy::NoProxy); |
|
|
|
@ -133,11 +138,13 @@ namespace proxy { |
|
|
|
|
//! If Qt Network is available you may check the proxies found
|
|
|
|
|
/*! @copydoc proxy(const std::string&, int) */ |
|
|
|
|
void proxy(const QUrl& url, int timeout1=5000, int timeout2=30000) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
proxy(url.toString().toStdString(), timeout1, timeout2); |
|
|
|
|
} |
|
|
|
|
//! If Qt Network is available you may check the proxies found
|
|
|
|
|
/*! @copydoc proxy(const std::string&, int) */ |
|
|
|
|
void proxy(const QString& url, int timeout1=5000, int timeout2=30000) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
proxy(url.toStdString(), timeout1, timeout2); |
|
|
|
|
} |
|
|
|
|
static QString toString(const QNetworkProxy& p) { |
|
|
|
@ -174,6 +181,7 @@ namespace proxy { |
|
|
|
|
void temporaryError(QNetworkReply::NetworkError, QString, QString); |
|
|
|
|
private: |
|
|
|
|
void setupProxyCheck(const QNetworkProxy& prxy, const std::string& url) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
qDebug()<<"Testing proxy for url:"<<toString(prxy)<<"url="<<url.data(); |
|
|
|
|
QNetworkAccessManager* manager(new QNetworkAccessManager); |
|
|
|
|
manager->setProxy(prxy); |
|
|
|
@ -197,15 +205,38 @@ namespace proxy { |
|
|
|
|
(QUrl(QString::fromStdString(url)))), |
|
|
|
|
std::make_pair(manager, prxy))); |
|
|
|
|
} |
|
|
|
|
void clean(QNetworkAccessManager* manager) { |
|
|
|
|
if (!disconnect(manager, SIGNAL(finished(QNetworkReply*)), |
|
|
|
|
this, SLOT(replyFinished(QNetworkReply*)))) |
|
|
|
|
qFatal("disconnect failed"); |
|
|
|
|
disconnect(manager, |
|
|
|
|
SIGNAL(authenticationRequired(QNetworkReply*, |
|
|
|
|
QAuthenticator*)), |
|
|
|
|
this, |
|
|
|
|
SLOT(authenticationRequired(QNetworkReply*, |
|
|
|
|
QAuthenticator*))); |
|
|
|
|
disconnect(manager, |
|
|
|
|
SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, |
|
|
|
|
QAuthenticator*)), |
|
|
|
|
this, |
|
|
|
|
SLOT(proxyAuthenticationRequired(const QNetworkProxy&, |
|
|
|
|
QAuthenticator*))); |
|
|
|
|
disconnect(manager, |
|
|
|
|
SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), |
|
|
|
|
this, |
|
|
|
|
SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&))); |
|
|
|
|
delete manager; |
|
|
|
|
} |
|
|
|
|
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(); |
|
|
|
|
delete it->second.first; |
|
|
|
|
clean(it->second.first); |
|
|
|
|
} |
|
|
|
|
_requests.clear(); |
|
|
|
|
qDebug()<<"Proxy detection timed out"<<"url="<<url.data(); |
|
|
|
@ -214,18 +245,14 @@ namespace proxy { |
|
|
|
|
" try autoproxy negotiation"; |
|
|
|
|
_direct = false; |
|
|
|
|
_url = url; |
|
|
|
|
#ifdef Q_OS_WIN32 |
|
|
|
|
_proxies = proxies(_url); // M$ cannot always do multithreading
|
|
|
|
|
threadFinished(); |
|
|
|
|
#else |
|
|
|
|
start(); // autoproxy detection in own thread
|
|
|
|
|
#endif |
|
|
|
|
} else { |
|
|
|
|
qDebug()<<"No proxy at all, giving up - offline?"; |
|
|
|
|
proxyError(QNetworkReply::TimeoutError); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
void replyFinished(QNetworkReply* reply) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
qDebug()<<"Proxydetection got reply with status:" |
|
|
|
|
<<reply->error()<<reply->errorString(); |
|
|
|
|
if (reply->error()!=QNetworkReply::NoError) { |
|
|
|
@ -239,24 +266,28 @@ namespace proxy { |
|
|
|
|
QUrl url(reply->url()); |
|
|
|
|
for (Requests::iterator it(_requests.begin()); |
|
|
|
|
it!=_requests.end(); ++it) |
|
|
|
|
delete it->second.first; |
|
|
|
|
clean(it->second.first); |
|
|
|
|
_requests.clear(); |
|
|
|
|
qDebug()<<"SUCCESS - Valid proxy found for url:" |
|
|
|
|
<<"url="<<url<<"proxy="<<toString(prxy); |
|
|
|
|
proxyFound(url, prxy); |
|
|
|
|
} |
|
|
|
|
void authenticationRequired(QNetworkReply*, QAuthenticator*) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
qDebug()<<"## "<<__PRETTY_FUNCTION__; |
|
|
|
|
} |
|
|
|
|
void proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
qDebug()<<"## "<<__PRETTY_FUNCTION__; |
|
|
|
|
} |
|
|
|
|
void sslErrors(QNetworkReply*, const QList<QSslError>& l) { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
qDebug()<<"## "<<__PRETTY_FUNCTION__; |
|
|
|
|
for (QList<QSslError>::const_iterator it(l.begin()); it!=l.end(); ++it) |
|
|
|
|
qDebug()<<" SSL-Error -> "<<it->errorString(); |
|
|
|
|
} |
|
|
|
|
void threadFinished() { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
for (List::const_iterator it(_proxies.begin()); |
|
|
|
|
it!=_proxies.end(); ++it) { |
|
|
|
|
QNetworkProxy prxy((it->type==DEFAULT?QNetworkProxy::DefaultProxy |
|
|
|
@ -274,6 +305,7 @@ namespace proxy { |
|
|
|
|
} |
|
|
|
|
protected: |
|
|
|
|
void run() { |
|
|
|
|
PROXYFACE_LOG; |
|
|
|
|
_proxies = proxies(_url); |
|
|
|
|
} |
|
|
|
|
private: |
|
|
|
|