1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

NetworkUrlInterceptor: Add proper locking

This commit is contained in:
David Rosca 2018-01-06 11:59:41 +01:00
parent 394221c5c0
commit 9c17277dd5
7 changed files with 52 additions and 35 deletions

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2015-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -29,10 +29,34 @@ NetworkUrlInterceptor::NetworkUrlInterceptor(QObject *parent)
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{ {
if (m_sendDNT) m_mutex.lock();
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
info.setHttpHeader(QByteArrayLiteral("User-Agent"), mApp->userAgentManager()->userAgentForUrl(info.firstPartyUrl()).toUtf8()); if (m_sendDNT) {
info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1"));
}
const QString host = info.firstPartyUrl().host();
if (m_usePerDomainUserAgent) {
QString userAgent;
if (m_userAgentsList.contains(host)) {
userAgent = m_userAgentsList.value(host);
} else {
QHashIterator<QString, QString> i(m_userAgentsList);
while (i.hasNext()) {
i.next();
if (host.endsWith(i.key())) {
userAgent = i.value();
break;
}
}
}
if (!userAgent.isEmpty()) {
info.setHttpHeader(QByteArrayLiteral("User-Agent"), userAgent.toUtf8());
}
}
m_mutex.unlock();
foreach (UrlInterceptor *interceptor, m_interceptors) { foreach (UrlInterceptor *interceptor, m_interceptors) {
interceptor->interceptRequest(info); interceptor->interceptRequest(info);
@ -52,8 +76,15 @@ void NetworkUrlInterceptor::removeUrlInterceptor(UrlInterceptor *interceptor)
void NetworkUrlInterceptor::loadSettings() void NetworkUrlInterceptor::loadSettings()
{ {
m_mutex.lock();
Settings settings; Settings settings;
settings.beginGroup("Web-Browser-Settings"); settings.beginGroup("Web-Browser-Settings");
m_sendDNT = settings.value("DoNotTrack", false).toBool(); m_sendDNT = settings.value("DoNotTrack", false).toBool();
settings.endGroup(); settings.endGroup();
m_usePerDomainUserAgent = mApp->userAgentManager()->usePerDomainUserAgents();
m_userAgentsList = mApp->userAgentManager()->perDomainUserAgentsList();
m_mutex.unlock();
} }

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2015 David Rosca <nowrep@gmail.com> * Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -19,6 +19,7 @@
#ifndef NETWORKURLINTERCEPTOR_H #ifndef NETWORKURLINTERCEPTOR_H
#define NETWORKURLINTERCEPTOR_H #define NETWORKURLINTERCEPTOR_H
#include <QMutex>
#include <QWebEngineUrlRequestInterceptor> #include <QWebEngineUrlRequestInterceptor>
#include "qzcommon.h" #include "qzcommon.h"
@ -38,8 +39,11 @@ public:
void loadSettings(); void loadSettings();
private: private:
QMutex m_mutex;
QList<UrlInterceptor*> m_interceptors; QList<UrlInterceptor*> m_interceptors;
bool m_sendDNT; bool m_sendDNT = false;
bool m_usePerDomainUserAgent = false;
QHash<QString, QString> m_userAgentsList;
}; };
#endif // NETWORKURLINTERCEPTOR_H #endif // NETWORKURLINTERCEPTOR_H

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -26,12 +26,12 @@
#include "settings.h" #include "settings.h"
#include "datapaths.h" #include "datapaths.h"
#include "iconprovider.h" #include "iconprovider.h"
#include "useragentmanager.h"
#include "sessionmanager.h" #include "sessionmanager.h"
#include <QTimer> #include <QTimer>
#include <QSettings> #include <QSettings>
#include <QUrlQuery> #include <QUrlQuery>
#include <QWebEngineProfile>
#include <QWebEngineUrlRequestJob> #include <QWebEngineUrlRequestJob>
static QString authorString(const char* name, const QString &mail) static QString authorString(const char* name, const QString &mail)
@ -409,7 +409,7 @@ QString FalkonSchemeReply::configPage()
} }
QString page = cPage; QString page = cPage;
page.replace(QLatin1String("%USER-AGENT%"), mApp->userAgentManager()->userAgentForUrl(QUrl())); page.replace(QLatin1String("%USER-AGENT%"), mApp->webProfile()->httpUserAgent());
QString pluginsString; QString pluginsString;
const QList<Plugins::Plugin> &availablePlugins = mApp->plugins()->getAvailablePlugins(); const QList<Plugins::Plugin> &availablePlugins = mApp->plugins()->getAvailablePlugins();

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2015 David Rosca <nowrep@gmail.com> * Copyright (C) 2015-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -26,6 +26,8 @@ class UrlInterceptor : public QObject
{ {
public: public:
explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { } explicit UrlInterceptor(QObject *parent = Q_NULLPTR) : QObject(parent) { }
// Runs on IO thread!
virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0; virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0;
}; };

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -56,26 +56,6 @@ void UserAgentManager::loadSettings()
QWebEngineProfile::defaultProfile()->setHttpUserAgent(userAgent); QWebEngineProfile::defaultProfile()->setHttpUserAgent(userAgent);
} }
QString UserAgentManager::userAgentForUrl(const QUrl &url) const
{
const QString host = url.host();
if (m_usePerDomainUserAgent) {
if (m_userAgentsList.contains(host)) {
return m_userAgentsList.value(host);
}
QHashIterator<QString, QString> i(m_userAgentsList);
while (i.hasNext()) {
i.next();
if (host.endsWith(i.key())) {
return i.value();
}
}
}
return QWebEngineProfile::defaultProfile()->httpUserAgent();
}
QString UserAgentManager::globalUserAgent() const QString UserAgentManager::globalUserAgent() const
{ {
return m_globalUserAgent; return m_globalUserAgent;

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2010-2015 David Rosca <nowrep@gmail.com> * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -34,8 +34,6 @@ public:
void loadSettings(); void loadSettings();
QString userAgentForUrl(const QUrl &url) const;
QString globalUserAgent() const; QString globalUserAgent() const;
QString defaultUserAgent() const; QString defaultUserAgent() const;

View File

@ -1,6 +1,6 @@
/* ============================================================ /* ============================================================
* Falkon - Qt web browser * Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -21,6 +21,7 @@
#include "qztools.h" #include "qztools.h"
#include "mainapplication.h" #include "mainapplication.h"
#include "settings.h" #include "settings.h"
#include "networkmanager.h"
#include <QFormLayout> #include <QFormLayout>
#include <QLineEdit> #include <QLineEdit>
@ -177,6 +178,7 @@ void UserAgentDialog::accept()
settings.endGroup(); settings.endGroup();
m_manager->loadSettings(); m_manager->loadSettings();
mApp->networkManager()->loadSettings();
close(); close();
} }