2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
2017-01-26 12:34:55 +01:00
|
|
|
* QupZilla - Qt web browser
|
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2015-09-29 11:45:39 +02:00
|
|
|
#include "networkmanager.h"
|
2015-09-29 16:18:21 +02:00
|
|
|
#include "autofill.h"
|
|
|
|
#include "qztools.h"
|
2015-10-05 22:21:14 +02:00
|
|
|
#include "settings.h"
|
2015-10-05 22:03:53 +02:00
|
|
|
#include "cookiejar.h"
|
2015-10-05 22:21:14 +02:00
|
|
|
#include "acceptlanguage.h"
|
2015-09-29 16:18:21 +02:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "passwordmanager.h"
|
|
|
|
#include "sslerrordialog.h"
|
2015-10-05 19:47:42 +02:00
|
|
|
#include "networkurlinterceptor.h"
|
|
|
|
#include "schemehandlers/qupzillaschemehandler.h"
|
2015-09-29 16:18:21 +02:00
|
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QDialog>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QFormLayout>
|
|
|
|
#include <QAuthenticator>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkProxy>
|
2015-10-05 17:20:53 +02:00
|
|
|
#include <QWebEngineProfile>
|
2015-09-29 16:18:21 +02:00
|
|
|
#include <QWebEngineCertificateError>
|
2015-09-29 11:45:39 +02:00
|
|
|
|
|
|
|
NetworkManager::NetworkManager(QObject *parent)
|
|
|
|
: QNetworkAccessManager(parent)
|
|
|
|
{
|
2015-10-05 17:20:53 +02:00
|
|
|
// Create scheme handlers
|
2015-10-22 17:54:53 +02:00
|
|
|
mApp->webProfile()->installUrlSchemeHandler(QByteArrayLiteral("qupzilla"), new QupZillaSchemeHandler());
|
2015-10-05 17:20:53 +02:00
|
|
|
|
2015-10-05 19:47:42 +02:00
|
|
|
// Create url interceptor
|
|
|
|
m_urlInterceptor = new NetworkUrlInterceptor(this);
|
|
|
|
mApp->webProfile()->setRequestInterceptor(m_urlInterceptor);
|
|
|
|
|
2015-10-05 22:03:53 +02:00
|
|
|
// Create cookie jar
|
2015-10-22 14:44:41 +02:00
|
|
|
mApp->cookieJar();
|
2015-10-05 22:03:53 +02:00
|
|
|
|
2015-09-29 16:18:21 +02:00
|
|
|
connect(this, &QNetworkAccessManager::authenticationRequired, this, [this](QNetworkReply *reply, QAuthenticator *auth) {
|
|
|
|
authentication(reply->url(), auth);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(this, &QNetworkAccessManager::proxyAuthenticationRequired, this, [this](const QNetworkProxy &proxy, QAuthenticator *auth) {
|
|
|
|
proxyAuthentication(proxy.hostName(), auth);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NetworkManager::certificateError(const QWebEngineCertificateError &error, QWidget *parent)
|
|
|
|
{
|
|
|
|
const QString &host = error.url().host();
|
|
|
|
|
|
|
|
if (m_ignoredSslErrors.contains(host) && m_ignoredSslErrors.value(host) == error.error())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QString title = tr("SSL Certificate Error!");
|
|
|
|
QString text1 = tr("The page you are trying to access has the following errors in the SSL certificate:");
|
|
|
|
QString text2 = tr("Would you like to make an exception for this certificate?");
|
|
|
|
|
|
|
|
QString message = QSL("<b>%1</b><p>%2</p><ul><li>%3</li></ul><p>%4</p>").arg(title, text1, error.errorDescription(), text2);
|
|
|
|
|
|
|
|
SslErrorDialog dialog(parent);
|
|
|
|
dialog.setText(message);
|
|
|
|
dialog.exec();
|
|
|
|
|
|
|
|
switch (dialog.result()) {
|
|
|
|
case SslErrorDialog::Yes:
|
|
|
|
// TODO: Permanent exceptions
|
|
|
|
case SslErrorDialog::OnlyForThisSession:
|
|
|
|
m_ignoredSslErrors[error.url().host()] = error.error();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SslErrorDialog::No:
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkManager::authentication(const QUrl &url, QAuthenticator *auth, QWidget *parent)
|
|
|
|
{
|
|
|
|
QDialog* dialog = new QDialog(parent);
|
|
|
|
dialog->setWindowTitle(tr("Authorisation required"));
|
|
|
|
|
|
|
|
QFormLayout* formLa = new QFormLayout(dialog);
|
|
|
|
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
QLabel* userLab = new QLabel(dialog);
|
|
|
|
QLabel* passLab = new QLabel(dialog);
|
|
|
|
userLab->setText(tr("Username: "));
|
|
|
|
passLab->setText(tr("Password: "));
|
|
|
|
|
|
|
|
QLineEdit* user = new QLineEdit(dialog);
|
|
|
|
QLineEdit* pass = new QLineEdit(dialog);
|
|
|
|
pass->setEchoMode(QLineEdit::Password);
|
|
|
|
QCheckBox* save = new QCheckBox(dialog);
|
|
|
|
save->setText(tr("Save username and password for this site"));
|
|
|
|
|
|
|
|
QDialogButtonBox* box = new QDialogButtonBox(dialog);
|
|
|
|
box->addButton(QDialogButtonBox::Ok);
|
|
|
|
box->addButton(QDialogButtonBox::Cancel);
|
|
|
|
connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
|
|
|
|
connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
|
|
|
|
|
|
|
|
label->setText(tr("A username and password are being requested by %1. "
|
2015-10-15 17:57:03 +02:00
|
|
|
"The site says: \"%2\"").arg(url.host(), auth->realm().toHtmlEscaped()));
|
2015-09-29 16:18:21 +02:00
|
|
|
|
|
|
|
formLa->addRow(label);
|
|
|
|
formLa->addRow(userLab, user);
|
|
|
|
formLa->addRow(passLab, pass);
|
|
|
|
formLa->addRow(save);
|
|
|
|
formLa->addWidget(box);
|
|
|
|
|
|
|
|
AutoFill* fill = mApp->autoFill();
|
|
|
|
QString storedUser;
|
|
|
|
QString storedPassword;
|
|
|
|
bool shouldUpdateEntry = false;
|
|
|
|
|
|
|
|
if (fill->isStored(url)) {
|
|
|
|
const QVector<PasswordEntry> &data = fill->getFormData(url);
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
save->setChecked(true);
|
|
|
|
shouldUpdateEntry = true;
|
2015-10-23 11:55:14 +02:00
|
|
|
storedUser = data.at(0).username;
|
|
|
|
storedPassword = data.at(0).password;
|
2015-09-29 16:18:21 +02:00
|
|
|
user->setText(storedUser);
|
|
|
|
pass->setText(storedPassword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not save when private browsing is enabled
|
|
|
|
if (mApp->isPrivate()) {
|
|
|
|
save->setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dialog->exec() != QDialog::Accepted) {
|
2016-06-06 10:51:09 +02:00
|
|
|
*auth = QAuthenticator();
|
2015-10-15 15:38:44 +02:00
|
|
|
delete dialog;
|
2015-09-29 16:18:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auth->setUser(user->text());
|
|
|
|
auth->setPassword(pass->text());
|
|
|
|
|
|
|
|
if (save->isChecked()) {
|
|
|
|
if (shouldUpdateEntry) {
|
|
|
|
if (storedUser != user->text() || storedPassword != pass->text()) {
|
|
|
|
fill->updateEntry(url, user->text(), pass->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fill->addEntry(url, user->text(), pass->text());
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 15:38:44 +02:00
|
|
|
|
|
|
|
delete dialog;
|
2015-09-29 16:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkManager::proxyAuthentication(const QString &proxyHost, QAuthenticator *auth, QWidget *parent)
|
|
|
|
{
|
2015-10-15 15:38:44 +02:00
|
|
|
const QNetworkProxy proxy = QNetworkProxy::applicationProxy();
|
|
|
|
if (!proxy.user().isEmpty() && !proxy.password().isEmpty()) {
|
|
|
|
auth->setUser(proxy.user());
|
|
|
|
auth->setPassword(proxy.password());
|
2015-09-29 16:18:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog* dialog = new QDialog(parent);
|
|
|
|
dialog->setWindowTitle(tr("Proxy authorisation required"));
|
|
|
|
|
|
|
|
QFormLayout* formLa = new QFormLayout(dialog);
|
|
|
|
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
QLabel* userLab = new QLabel(dialog);
|
|
|
|
QLabel* passLab = new QLabel(dialog);
|
|
|
|
userLab->setText(tr("Username: "));
|
|
|
|
passLab->setText(tr("Password: "));
|
|
|
|
|
|
|
|
QLineEdit* user = new QLineEdit(dialog);
|
|
|
|
QLineEdit* pass = new QLineEdit(dialog);
|
|
|
|
pass->setEchoMode(QLineEdit::Password);
|
|
|
|
|
|
|
|
QDialogButtonBox* box = new QDialogButtonBox(dialog);
|
|
|
|
box->addButton(QDialogButtonBox::Ok);
|
|
|
|
box->addButton(QDialogButtonBox::Cancel);
|
|
|
|
connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
|
|
|
|
connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
|
|
|
|
|
|
|
|
label->setText(tr("A username and password are being requested by proxy %1. ").arg(proxyHost));
|
|
|
|
formLa->addRow(label);
|
|
|
|
formLa->addRow(userLab, user);
|
|
|
|
formLa->addRow(passLab, pass);
|
|
|
|
formLa->addWidget(box);
|
|
|
|
|
|
|
|
if (dialog->exec() != QDialog::Accepted) {
|
2016-06-06 10:51:09 +02:00
|
|
|
*auth = QAuthenticator();
|
2015-10-15 15:38:44 +02:00
|
|
|
delete dialog;
|
2015-09-29 16:18:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auth->setUser(user->text());
|
|
|
|
auth->setPassword(pass->text());
|
2015-10-15 15:38:44 +02:00
|
|
|
|
|
|
|
delete dialog;
|
2015-09-29 11:45:39 +02:00
|
|
|
}
|
2015-10-05 19:47:42 +02:00
|
|
|
|
|
|
|
void NetworkManager::installUrlInterceptor(UrlInterceptor *interceptor)
|
|
|
|
{
|
|
|
|
m_urlInterceptor->installUrlInterceptor(interceptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkManager::removeUrlInterceptor(UrlInterceptor *interceptor)
|
|
|
|
{
|
|
|
|
m_urlInterceptor->removeUrlInterceptor(interceptor);
|
|
|
|
}
|
2015-10-05 22:21:14 +02:00
|
|
|
|
|
|
|
void NetworkManager::loadSettings()
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Language");
|
|
|
|
QStringList langs = settings.value("acceptLanguage", AcceptLanguage::defaultLanguage()).toStringList();
|
|
|
|
settings.endGroup();
|
|
|
|
mApp->webProfile()->setHttpAcceptLanguage(AcceptLanguage::generateHeader(langs));
|
2015-10-05 22:32:55 +02:00
|
|
|
|
2015-10-15 14:39:18 +02:00
|
|
|
QNetworkProxy proxy;
|
|
|
|
settings.beginGroup("Web-Proxy");
|
|
|
|
proxy.setType(QNetworkProxy::ProxyType(settings.value("ProxyType", QNetworkProxy::NoProxy).toInt()));
|
|
|
|
proxy.setHostName(settings.value("HostName", QString()).toString());
|
|
|
|
proxy.setPort(settings.value("Port", 8080).toInt());
|
|
|
|
proxy.setUser(settings.value("Username", QString()).toString());
|
|
|
|
proxy.setPassword(settings.value("Password", QString()).toString());
|
|
|
|
settings.endGroup();
|
|
|
|
QNetworkProxy::setApplicationProxy(proxy);
|
|
|
|
|
2015-10-05 22:32:55 +02:00
|
|
|
m_urlInterceptor->loadSettings();
|
2015-10-05 22:21:14 +02:00
|
|
|
}
|
2016-03-20 19:37:22 +01:00
|
|
|
|
2017-01-26 12:34:55 +01:00
|
|
|
void NetworkManager::shutdown()
|
|
|
|
{
|
|
|
|
mApp->webProfile()->setRequestInterceptor(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-03-20 19:37:22 +01:00
|
|
|
QNetworkReply *NetworkManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
|
|
|
|
{
|
|
|
|
QNetworkRequest req = request;
|
|
|
|
req.setAttribute(QNetworkRequest::SpdyAllowedAttribute, true);
|
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
|
|
|
|
|
|
|
return QNetworkAccessManager::createRequest(op, req, outgoingData);
|
|
|
|
}
|