1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-22 18:22:10 +02:00
falkonOfficial/src/network/networkmanager.cpp

235 lines
8.4 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2011 nowrep
*
* 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/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "networkmanager.h"
#include "qupzilla.h"
#include "autofillmodel.h"
#include "networkmanagerproxy.h"
#include "mainapplication.h"
#include "webpage.h"
2011-03-27 21:59:40 +02:00
#include "pluginproxy.h"
#include "adblockmanager.h"
#include "adblocknetwork.h"
2011-03-02 16:57:41 +01:00
2011-03-17 17:03:04 +01:00
NetworkManager::NetworkManager(QupZilla* mainClass, QObject* parent) :
2011-03-02 16:57:41 +01:00
NetworkManagerProxy(mainClass, parent)
2011-03-27 21:59:40 +02:00
,m_adblockNetwork(0)
2011-03-02 16:57:41 +01:00
,p_QupZilla(mainClass)
,m_ignoreAllWarnings(false)
2011-03-02 16:57:41 +01:00
{
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(authentication(QNetworkReply*, QAuthenticator* )));
connect(this, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>)));
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(setSSLConfiguration(QNetworkReply*)));
2011-03-02 16:57:41 +01:00
loadSettings();
}
void NetworkManager::loadSettings()
{
2011-03-04 13:59:07 +01:00
QSettings settings(mApp->getActiveProfil()+"settings.ini", QSettings::IniFormat);
2011-03-02 16:57:41 +01:00
settings.beginGroup("Web-Browser-Settings");
if (settings.value("AllowLocalCache", true).toBool()) {
m_diskCache = mApp->networkCache();
2011-03-04 13:59:07 +01:00
m_diskCache->setCacheDirectory(mApp->getActiveProfil()+"/networkcache");
2011-03-02 16:57:41 +01:00
m_diskCache->setMaximumCacheSize(settings.value("MaximumCacheSize",50).toInt() * 1024*1024); //MegaBytes
setCache(m_diskCache);
}
m_ignoreAllWarnings = settings.value("IgnoreAllSSLWarnings", false).toBool();
2011-03-02 16:57:41 +01:00
settings.endGroup();
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::AnyProtocol);
QSslConfiguration::setDefaultConfiguration(config);
2011-03-02 16:57:41 +01:00
}
void NetworkManager::setSSLConfiguration(QNetworkReply *reply)
{
if (!reply->sslConfiguration().isNull()) {
QSslCertificate cert = reply->sslConfiguration().peerCertificate();
if (!cert.isValid())
return;
QNetworkRequest request = reply->request();
QVariant v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100));
WebPage* webPage = (WebPage*)(v.value<void*>());
v = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 102));
WebView* webView = (WebView*)(v.value<void*>());
if (!webPage || !webView)
return;
2011-03-19 13:15:01 +01:00
if (webView->url().host() == reply->url().host())
webPage->setSSLCertificate( cert );
}
}
2011-03-19 13:15:01 +01:00
2011-03-17 17:03:04 +01:00
void NetworkManager::sslError(QNetworkReply* reply, QList<QSslError> errors)
2011-03-02 16:57:41 +01:00
{
if (m_ignoreAllWarnings) {
reply->ignoreSslErrors(errors);
return;
}
2011-03-02 16:57:41 +01:00
QString title = tr("SSL Certificate Error!");
QString text1 = tr("The page you trying to access has following errors in SSL Certificate:");
QStringList actions;
foreach (QSslError error, errors) {
if (m_certExceptions.contains(error.certificate()))
continue;
if (error.error() == QSslError::NoError) //Weird behavior on Windows
continue;
QSslCertificate cert = error.certificate();
actions.append(tr("<b>Organization: </b>") + cert.subjectInfo(QSslCertificate::Organization));
actions.append(tr("<b>Domain Name: </b>") + cert.subjectInfo(QSslCertificate::CommonName));
actions.append(tr("<b>Expiration Date: </b>") + cert.expiryDate().toString("hh:mm:ss dddd d. MMMM yyyy"));
actions.append(tr("<b>Error: </b>") + error.errorString());
}
QString text2 = tr("Would you like to make exception for this certificate?");
QString message = QString(QLatin1String("<b>%1</b><p>%2</p><ul><li>%3</li></ul><p>%4</p>")).arg(title, text1, actions.join(QLatin1String("</li><li>")), text2);
if (!actions.isEmpty()) {
QMessageBox::StandardButton button = QMessageBox::critical(p_QupZilla, tr("SSL Certificate Error"),
message, QMessageBox::Yes | QMessageBox::No);
if (button != QMessageBox::Yes)
return;
}
foreach (QSslError error, errors) {
if (m_certExceptions.contains(error.certificate()))
continue;
m_certExceptions.append(error.certificate());
}
reply->ignoreSslErrors(errors);
}
void NetworkManager::authentication(QNetworkReply* reply, QAuthenticator* auth)
{
QDialog* dialog = new QDialog(p_QupZilla);
dialog->setWindowTitle(tr("Authorization 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);
QCheckBox* save = new QCheckBox(dialog);
save->setText(tr("Save username and password on this site"));
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 %1. "
"The site says: \"%2\"").arg(reply->url().toEncoded(), auth->realm()));
formLa->addRow(label);
formLa->addRow(userLab, user);
formLa->addRow(passLab, pass);
formLa->addRow(save);
formLa->addWidget(box);
2011-03-04 13:59:07 +01:00
AutoFillModel* fill = mApp->autoFill();
2011-03-02 16:57:41 +01:00
if (fill->isStored(reply->url())) {
save->setChecked(true);
user->setText(fill->getUsername(reply->url()));
pass->setText(fill->getPassword(reply->url()));
}
emit wantsFocus(reply->url());
//Do not save when private browsing is enabled
2011-03-04 13:59:07 +01:00
if (mApp->webSettings()->testAttribute(QWebSettings::PrivateBrowsingEnabled))
2011-03-02 16:57:41 +01:00
save->setVisible(false);
if (!dialog->exec() == QDialog::Accepted)
return;
auth->setUser(user->text());
auth->setPassword(pass->text());
if (save->isChecked())
fill->addEntry(reply->url(), user->text(), pass->text());
}
2011-03-17 17:03:04 +01:00
QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice* outgoingData)
2011-03-02 16:57:41 +01:00
{
if (op == PostOperation && outgoingData) {
QByteArray outgoingDataByteArray = outgoingData->peek(1024 * 1024);
2011-03-04 13:59:07 +01:00
mApp->autoFill()->post(request, outgoingDataByteArray);
2011-03-02 16:57:41 +01:00
}
QNetworkRequest req = request;
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
2011-03-19 13:15:01 +01:00
2011-03-27 21:59:40 +02:00
// Adblock
if (op == QNetworkAccessManager::GetOperation) {
if (!m_adblockNetwork)
m_adblockNetwork = AdBlockManager::instance()->network();
QNetworkReply* reply = m_adblockNetwork->block(req);
if (reply)
return reply;
}
2011-03-27 21:59:40 +02:00
QNetworkReply* reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
2011-03-02 16:57:41 +01:00
return reply;
}
void NetworkManager::saveCertExceptions()
{
2011-03-04 13:59:07 +01:00
QFile file(mApp->getActiveProfil()+"sslexceptions.dat");
2011-03-02 16:57:41 +01:00
file.open(QIODevice::WriteOnly);
QDataStream stream(&file);
int count = m_certExceptions.count();
stream << count;
for (int i = 0; i < count; i++) {
stream << m_certExceptions.at(i).toPem();
}
file.close();
}
void NetworkManager::loadCertExceptions()
{
2011-03-04 13:59:07 +01:00
QFile file(mApp->getActiveProfil()+"sslexceptions.dat");
2011-03-02 16:57:41 +01:00
file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
int count;
stream >> count;
QByteArray cert;
for (int i = 0; i < count; i++) {
stream >> cert;
m_certExceptions.append(QSslCertificate::fromData(cert));
}
file.close();
}