|
|
@ -12,165 +12,164 @@ |
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
#include "UBNetworkAccessManager.h" |
|
|
|
#include "UBNetworkAccessManager.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <QtGui> |
|
|
|
#include <QtGui> |
|
|
|
#include <QtNetwork> |
|
|
|
#include <QtNetwork> |
|
|
|
|
|
|
|
|
|
|
|
#include "frameworks/UBDesktopServices.h" |
|
|
|
#include "frameworks/UBDesktopServices.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "core/UBApplication.h" |
|
|
|
#include "core/UBApplication.h" |
|
|
|
#include "core/UBApplicationController.h" |
|
|
|
#include "core/UBApplicationController.h" |
|
|
|
#include "core/UBSettings.h" |
|
|
|
#include "core/UBSettings.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "ui_passworddialog.h" |
|
|
|
#include "ui_passworddialog.h" |
|
|
|
#include "ui_proxy.h" |
|
|
|
#include "ui_proxy.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "UBCookieJar.h" |
|
|
|
#include "UBCookieJar.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "core/memcheck.h" |
|
|
|
#include "core/memcheck.h" |
|
|
|
|
|
|
|
|
|
|
|
UBNetworkAccessManager *UBNetworkAccessManager::sNetworkAccessManager = 0; |
|
|
|
UBNetworkAccessManager *UBNetworkAccessManager::sNetworkAccessManager = 0; |
|
|
|
|
|
|
|
|
|
|
|
UBNetworkAccessManager *UBNetworkAccessManager::defaultAccessManager() |
|
|
|
UBNetworkAccessManager *UBNetworkAccessManager::defaultAccessManager() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!sNetworkAccessManager) { |
|
|
|
if (!sNetworkAccessManager) { |
|
|
|
sNetworkAccessManager = new UBNetworkAccessManager(qApp); |
|
|
|
sNetworkAccessManager = new UBNetworkAccessManager(qApp); |
|
|
|
sNetworkAccessManager->setCookieJar(new UBCookieJar(sNetworkAccessManager)); |
|
|
|
sNetworkAccessManager->setCookieJar(new UBCookieJar(sNetworkAccessManager)); |
|
|
|
} |
|
|
|
} |
|
|
|
return sNetworkAccessManager; |
|
|
|
return sNetworkAccessManager; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
UBNetworkAccessManager::UBNetworkAccessManager(QObject *parent) |
|
|
|
UBNetworkAccessManager::UBNetworkAccessManager(QObject *parent) |
|
|
|
: QNetworkAccessManager(parent) |
|
|
|
: QNetworkAccessManager(parent) |
|
|
|
, mProxyAuthenticationCount(0) |
|
|
|
, mProxyAuthenticationCount(0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), |
|
|
|
connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), |
|
|
|
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
|
|
SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); |
|
|
|
connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), |
|
|
|
connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), |
|
|
|
SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); |
|
|
|
SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); |
|
|
|
connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), |
|
|
|
connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)), |
|
|
|
SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&))); |
|
|
|
SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&))); |
|
|
|
|
|
|
|
|
|
|
|
QNetworkProxy* proxy = UBSettings::settings()->httpProxy(); |
|
|
|
QNetworkProxy* proxy = UBSettings::settings()->httpProxy(); |
|
|
|
|
|
|
|
|
|
|
|
if (proxy) |
|
|
|
if (proxy) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setProxy(*proxy); |
|
|
|
setProxy(*proxy); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true); |
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); |
|
|
|
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); |
|
|
|
QString location = UBSettings::uniboardDataDirectory() + "/web-cache"; |
|
|
|
QString location = UBSettings::uniboardDataDirectory() + "/web-cache"; |
|
|
|
diskCache->setCacheDirectory(location); |
|
|
|
diskCache->setCacheDirectory(location); |
|
|
|
setCache(diskCache); |
|
|
|
setCache(diskCache); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QNetworkReply* UBNetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData) |
|
|
|
QNetworkReply* UBNetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QNetworkRequest request = req; // copy so we can modify
|
|
|
|
QNetworkRequest request = req; // copy so we can modify
|
|
|
|
// this is a temporary hack until we properly use the pipelining flags from QtWebkit
|
|
|
|
// this is a temporary hack until we properly use the pipelining flags from QtWebkit
|
|
|
|
// pipeline everything! :)
|
|
|
|
// pipeline everything! :)
|
|
|
|
request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); |
|
|
|
request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); |
|
|
|
|
|
|
|
|
|
|
|
QNetworkReply* reply = QNetworkAccessManager::createRequest(op, request, outgoingData); |
|
|
|
QNetworkReply* reply = QNetworkAccessManager::createRequest(op, request, outgoingData); |
|
|
|
|
|
|
|
|
|
|
|
return reply; |
|
|
|
return reply; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QNetworkReply *UBNetworkAccessManager::get(const QNetworkRequest &request) |
|
|
|
QNetworkReply *UBNetworkAccessManager::get(const QNetworkRequest &request) |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << "request url: " << request.url(); |
|
|
|
qDebug() << "request url: " << request.url(); |
|
|
|
QTime loadStartTime; |
|
|
|
QTime loadStartTime; |
|
|
|
loadStartTime.start(); |
|
|
|
loadStartTime.start(); |
|
|
|
QNetworkReply *networkReply = QNetworkAccessManager::get(request); |
|
|
|
QNetworkReply *networkReply = QNetworkAccessManager::get(request); |
|
|
|
return networkReply; |
|
|
|
return networkReply; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UBNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth) |
|
|
|
void UBNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QWidget *mainWindow = QApplication::activeWindow(); |
|
|
|
QWidget *mainWindow = QApplication::activeWindow(); |
|
|
|
|
|
|
|
|
|
|
|
QDialog dialog(mainWindow); |
|
|
|
QDialog dialog(mainWindow); |
|
|
|
dialog.setWindowFlags(Qt::Sheet); |
|
|
|
dialog.setWindowFlags(Qt::Sheet); |
|
|
|
|
|
|
|
|
|
|
|
Ui::PasswordDialog passwordDialog; |
|
|
|
Ui::PasswordDialog passwordDialog; |
|
|
|
passwordDialog.setupUi(&dialog); |
|
|
|
passwordDialog.setupUi(&dialog); |
|
|
|
|
|
|
|
|
|
|
|
passwordDialog.iconLabel->setText(QString()); |
|
|
|
passwordDialog.iconLabel->setText(QString()); |
|
|
|
passwordDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32)); |
|
|
|
passwordDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32)); |
|
|
|
|
|
|
|
|
|
|
|
QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>"); |
|
|
|
QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>"); |
|
|
|
introMessage = introMessage.arg(Qt::escape(reply->url().toString())).arg(Qt::escape(reply->url().toString())); |
|
|
|
introMessage = introMessage.arg(Qt::escape(reply->url().toString())).arg(Qt::escape(reply->url().toString())); |
|
|
|
passwordDialog.introLabel->setText(introMessage); |
|
|
|
passwordDialog.introLabel->setText(introMessage); |
|
|
|
passwordDialog.introLabel->setWordWrap(true); |
|
|
|
passwordDialog.introLabel->setWordWrap(true); |
|
|
|
|
|
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted) |
|
|
|
if (dialog.exec() == QDialog::Accepted) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(auth && passwordDialog.userNameLineEdit) |
|
|
|
if(auth && passwordDialog.userNameLineEdit) |
|
|
|
auth->setUser(passwordDialog.userNameLineEdit->text()); |
|
|
|
auth->setUser(passwordDialog.userNameLineEdit->text()); |
|
|
|
if(auth && passwordDialog.passwordLineEdit) |
|
|
|
if(auth && passwordDialog.passwordLineEdit) |
|
|
|
auth->setPassword(passwordDialog.passwordLineEdit->text()); |
|
|
|
auth->setPassword(passwordDialog.passwordLineEdit->text()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UBNetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth) |
|
|
|
void UBNetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Q_UNUSED(proxy); |
|
|
|
Q_UNUSED(proxy); |
|
|
|
|
|
|
|
|
|
|
|
QString username = UBSettings::settings()->proxyUsername(); |
|
|
|
QString username = UBSettings::settings()->proxyUsername(); |
|
|
|
QString password = UBSettings::settings()->proxyPassword(); |
|
|
|
QString password = UBSettings::settings()->proxyPassword(); |
|
|
|
|
|
|
|
|
|
|
|
if (username.length() > 0 || password.length() > 0) |
|
|
|
if (username.length() > 0 || password.length() > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auth->setUser(username); |
|
|
|
auth->setUser(username); |
|
|
|
auth->setPassword(password); |
|
|
|
auth->setPassword(password); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mProxyAuthenticationCount++; |
|
|
|
mProxyAuthenticationCount++; |
|
|
|
|
|
|
|
|
|
|
|
if (mProxyAuthenticationCount == 3) |
|
|
|
if (mProxyAuthenticationCount == 3) |
|
|
|
{ |
|
|
|
{ |
|
|
|
UBApplication::showMessage(tr("Failed to log to Proxy")); |
|
|
|
UBApplication::showMessage(tr("Failed to log to Proxy")); |
|
|
|
disconnect(SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)) |
|
|
|
disconnect(SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)) |
|
|
|
, this, SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); |
|
|
|
, this, SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UBNetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error) |
|
|
|
void UBNetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// check if SSL certificate has been trusted already
|
|
|
|
// check if SSL certificate has been trusted already
|
|
|
|
QString replyHost = reply->url().host() + ":" + reply->url().port(); |
|
|
|
QString replyHost = reply->url().host() + ":" + reply->url().port(); |
|
|
|
if(!sslTrustedHostList.contains(replyHost)) |
|
|
|
if(!sslTrustedHostList.contains(replyHost)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QWidget *mainWindow = QApplication::activeWindow(); |
|
|
|
QWidget *mainWindow = QApplication::activeWindow(); |
|
|
|
|
|
|
|
|
|
|
|
QStringList errorStrings; |
|
|
|
QStringList errorStrings; |
|
|
|
for (int i = 0; i < error.count(); ++i) |
|
|
|
for (int i = 0; i < error.count(); ++i) |
|
|
|
errorStrings += error.at(i).errorString(); |
|
|
|
errorStrings += error.at(i).errorString(); |
|
|
|
|
|
|
|
|
|
|
|
QString errors = errorStrings.join(QLatin1String("\n")); |
|
|
|
QString errors = errorStrings.join(QLatin1String("\n")); |
|
|
|
|
|
|
|
|
|
|
|
int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(), |
|
|
|
int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(), |
|
|
|
tr("SSL Errors:\n\n%1\n\n%2\n\n" |
|
|
|
tr("SSL Errors:\n\n%1\n\n%2\n\n" |
|
|
|
"Do you want to ignore these errors for this host?").arg(reply->url().toString()).arg(errors), |
|
|
|
"Do you want to ignore these errors for this host?").arg(reply->url().toString()).arg(errors), |
|
|
|
QMessageBox::Yes | QMessageBox::No, |
|
|
|
QMessageBox::Yes | QMessageBox::No, |
|
|
|
QMessageBox::No); |
|
|
|
QMessageBox::No); |
|
|
|
|
|
|
|
|
|
|
|
if (ret == QMessageBox::Yes) |
|
|
|
if (ret == QMessageBox::Yes) |
|
|
|
{ |
|
|
|
{ |
|
|
|
reply->ignoreSslErrors(); |
|
|
|
reply->ignoreSslErrors(); |
|
|
|
sslTrustedHostList.append(replyHost); |
|
|
|
sslTrustedHostList.append(replyHost); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|