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