mirror of
https://github.com/zealdocs/zeal.git
synced 2024-11-22 21:53:03 +03:00
feat(browser): restore custom request interceptor
This enables blocking of external resources in docsets.
The original implementation of the request interceptor was
temporary removed in 373606a1ed
.
This commit is contained in:
parent
dc4d476d20
commit
66aff30221
@ -1,6 +1,7 @@
|
||||
add_library(Browser STATIC
|
||||
searchtoolbar.cpp
|
||||
settings.cpp
|
||||
urlrequestinterceptor.cpp
|
||||
webbridge.cpp
|
||||
webcontrol.cpp
|
||||
webpage.cpp
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
#include "urlrequestinterceptor.h"
|
||||
|
||||
#include <core/settings.h>
|
||||
|
||||
#include <QFileInfo>
|
||||
@ -49,6 +51,13 @@ Settings::Settings(Core::Settings *appSettings, QObject *parent)
|
||||
// Create a new off-the-record profile.
|
||||
m_webProfile = new QWebEngineProfile(this);
|
||||
|
||||
// Setup URL interceptor.
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
|
||||
m_webProfile->setUrlRequestInterceptor(new UrlRequestInterceptor(this));
|
||||
#else
|
||||
m_webProfile->setRequestInterceptor(new UrlRequestInterceptor(this));
|
||||
#endif
|
||||
|
||||
// Listen to settings changes.
|
||||
connect(m_appSettings, &Core::Settings::updated, this, &Settings::applySettings);
|
||||
applySettings();
|
||||
|
74
src/libs/browser/urlrequestinterceptor.cpp
Normal file
74
src/libs/browser/urlrequestinterceptor.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 Oleg Shparber
|
||||
** Copyright (C) 2019 Kay Gawlik
|
||||
** Contact: https://go.zealdocs.org/l/contact
|
||||
**
|
||||
** This file is part of Zeal.
|
||||
**
|
||||
** Zeal 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.
|
||||
**
|
||||
** Zeal 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 Zeal. If not, see <http://www.gnu.org/licenses/>.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "urlrequestinterceptor.h"
|
||||
|
||||
#include <core/networkaccessmanager.h>
|
||||
|
||||
#include <QLoggingCategory>
|
||||
|
||||
using namespace Zeal::Browser;
|
||||
|
||||
static Q_LOGGING_CATEGORY(log, "zeal.browser.urlrequestinterceptor")
|
||||
|
||||
UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent)
|
||||
: QWebEngineUrlRequestInterceptor(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
|
||||
{
|
||||
const QUrl requestUrl = info.requestUrl();
|
||||
const QUrl firstPartyUrl = info.firstPartyUrl();
|
||||
|
||||
// Block invalid requests.
|
||||
if (!requestUrl.isValid() || !firstPartyUrl.isValid()) {
|
||||
blockRequest(info);
|
||||
return;
|
||||
}
|
||||
|
||||
bool isFirstPartyUrlLocal = Core::NetworkAccessManager::isLocalUrl(firstPartyUrl);
|
||||
bool isRequestUrlLocal = Core::NetworkAccessManager::isLocalUrl(requestUrl);
|
||||
|
||||
// Direct links are controlled in the WebPage
|
||||
if (info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow local resources on local pages and external resources on external pages.
|
||||
if (isFirstPartyUrlLocal == isRequestUrlLocal) {
|
||||
return;
|
||||
}
|
||||
|
||||
blockRequest(info);
|
||||
}
|
||||
|
||||
void UrlRequestInterceptor::blockRequest(QWebEngineUrlRequestInfo &info)
|
||||
{
|
||||
qCDebug(log, "Blocked request: %s '%s' (resource_type=%d, navigation_type=%d).",
|
||||
info.requestMethod().data(),
|
||||
qPrintable(info.requestUrl().toString()),
|
||||
info.resourceType(), info.navigationType());
|
||||
|
||||
info.block(true);
|
||||
}
|
47
src/libs/browser/urlrequestinterceptor.h
Normal file
47
src/libs/browser/urlrequestinterceptor.h
Normal file
@ -0,0 +1,47 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 Oleg Shparber
|
||||
** Copyright (C) 2019 Kay Gawlik
|
||||
** Contact: https://go.zealdocs.org/l/contact
|
||||
**
|
||||
** This file is part of Zeal.
|
||||
**
|
||||
** Zeal 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.
|
||||
**
|
||||
** Zeal 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 Zeal. If not, see <http://www.gnu.org/licenses/>.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef ZEAL_BROWSER_URLREQUESTINTERCEPTOR_H
|
||||
#define ZEAL_BROWSER_URLREQUESTINTERCEPTOR_H
|
||||
|
||||
#include <QWebEngineUrlRequestInterceptor>
|
||||
|
||||
namespace Zeal {
|
||||
namespace Browser {
|
||||
|
||||
class UrlRequestInterceptor final : public QWebEngineUrlRequestInterceptor
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY(UrlRequestInterceptor)
|
||||
public:
|
||||
UrlRequestInterceptor(QObject *parent = nullptr);
|
||||
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
|
||||
|
||||
private:
|
||||
void blockRequest(QWebEngineUrlRequestInfo &info);
|
||||
};
|
||||
|
||||
} // namespace Browser
|
||||
} // namespace Zeal
|
||||
|
||||
#endif // ZEAL_BROWSER_URLREQUESTINTERCEPTOR_H
|
Loading…
Reference in New Issue
Block a user