From 14e0040a9fcb55348f44970d002431f0e4eb2a72 Mon Sep 17 00:00:00 2001 From: nowrep Date: Fri, 13 Jul 2012 11:04:14 +0200 Subject: [PATCH] AdBlock: Fixed $third-party option when Referer header is empty - should fix a lot of false positives with $third-party rules see #444 --- src/lib/adblock/adblockrule.cpp | 3 ++- src/lib/app/qupzilla.cpp | 10 ++++++++-- src/lib/network/networkmanager.cpp | 14 +++++++++++--- src/lib/popupwindow/popupwebview.cpp | 1 + src/lib/webview/tabbedwebview.cpp | 9 +++++++++ src/lib/webview/tabbedwebview.h | 2 ++ src/lib/webview/tabwidget.cpp | 2 +- src/lib/webview/webpage.cpp | 10 +++++++++- src/lib/webview/webpage.h | 8 +++++--- src/lib/webview/webtab.cpp | 2 +- src/plugins/GreaseMonkey/gm_plugin.cpp | 6 ++---- 11 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index 445acbcc5..12eb1e77b 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -302,7 +302,8 @@ bool AdBlockRule::matchDomain(const QString &domain) const bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const { - const QString &referer = request.rawHeader("Referer"); + const QString &referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString(); + if (referer.isEmpty()) { return false; } diff --git a/src/lib/app/qupzilla.cpp b/src/lib/app/qupzilla.cpp index e9b5a4201..4464230ff 100644 --- a/src/lib/app/qupzilla.cpp +++ b/src/lib/app/qupzilla.cpp @@ -193,7 +193,10 @@ void QupZilla::postLaunch() } if (addTab) { - m_tabWidget->addView(startUrl, Qz::NT_CleanSelectedTabAtTheEnd); + QNetworkRequest request(startUrl); + request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); + + m_tabWidget->addView(request, Qz::NT_CleanSelectedTabAtTheEnd); if (startUrl.isEmpty() || startUrl.toString() == "qupzilla:speeddial") { locationBar()->setFocus(); @@ -202,7 +205,10 @@ void QupZilla::postLaunch() if (m_tabWidget->getTabBar()->normalTabsCount() <= 0 && m_startBehaviour != Qz::BW_OtherRestoredWindow) { //Something went really wrong .. add one tab - m_tabWidget->addView(m_homepage, Qz::NT_SelectedTabAtTheEnd); + QNetworkRequest request(m_homepage); + request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); + + m_tabWidget->addView(request, Qz::NT_SelectedTabAtTheEnd); } aboutToHideEditMenu(); diff --git a/src/lib/network/networkmanager.cpp b/src/lib/network/networkmanager.cpp index 5f4ea2bc0..8f9d47002 100644 --- a/src/lib/network/networkmanager.cpp +++ b/src/lib/network/networkmanager.cpp @@ -361,6 +361,14 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op return reply; } + if (req.rawHeader("X-QupZilla-UserLoadAction") == QByteArray("1")) { + req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray()); + req.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()); + } + else { + req.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), req.rawHeader("Referer")); + } + if (m_doNotTrack) { req.setRawHeader("DNT", QByteArray("1")); } @@ -372,9 +380,9 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op req.setRawHeader("Accept-Language", m_acceptLanguage); req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); - if (req.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() == QNetworkRequest::PreferNetwork) { - req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - } +// if (req.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() == QNetworkRequest::PreferNetwork) { +// req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); +// } // Adblock if (op == QNetworkAccessManager::GetOperation) { diff --git a/src/lib/popupwindow/popupwebview.cpp b/src/lib/popupwindow/popupwebview.cpp index 6fbd41771..680e78fa6 100644 --- a/src/lib/popupwindow/popupwebview.cpp +++ b/src/lib/popupwindow/popupwebview.cpp @@ -67,6 +67,7 @@ void PopupWebView::openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag posi if (window) { QNetworkRequest req(urla); req.setRawHeader("Referer", url().toEncoded()); + req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); window->tabWidget()->addView(req, Qz::NT_SelectedTab); window->raise(); diff --git a/src/lib/webview/tabbedwebview.cpp b/src/lib/webview/tabbedwebview.cpp index f1858cb82..e897ae567 100644 --- a/src/lib/webview/tabbedwebview.cpp +++ b/src/lib/webview/tabbedwebview.cpp @@ -138,6 +138,14 @@ void TabbedWebView::loadProgress(int prog) } } +void TabbedWebView::userLoadAction(const QUrl &url) +{ + QNetworkRequest request(url); + request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); + + load(request); +} + void TabbedWebView::slotLoadStarted() { m_rssChecked = false; @@ -280,6 +288,7 @@ void TabbedWebView::openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag pos { QNetworkRequest req(urla); req.setRawHeader("Referer", url().toEncoded()); + req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); m_tabWidget->addView(req, position); } diff --git a/src/lib/webview/tabbedwebview.h b/src/lib/webview/tabbedwebview.h index bd3a996c7..250605160 100644 --- a/src/lib/webview/tabbedwebview.h +++ b/src/lib/webview/tabbedwebview.h @@ -65,6 +65,8 @@ public slots: void slotLoadStarted(); void loadProgress(int prog); + void userLoadAction(const QUrl &url); + void closeView(); void openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag position); void openNewTab(); diff --git a/src/lib/webview/tabwidget.cpp b/src/lib/webview/tabwidget.cpp index 53430256c..7823694d6 100644 --- a/src/lib/webview/tabwidget.cpp +++ b/src/lib/webview/tabwidget.cpp @@ -314,7 +314,6 @@ int TabWidget::addView(QNetworkRequest req, const QString &title, const Qz::NewT connect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString))); if (url.isValid()) { - req.setUrl(url); webView->load(req); } @@ -553,6 +552,7 @@ int TabWidget::duplicateTab(int index) QNetworkRequest req(url); req.setRawHeader("Referer", url.toEncoded()); + req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); int id = addView(req, title, Qz::NT_CleanNotSelectedTab); weTab(id)->setHistoryData(history); diff --git a/src/lib/webview/webpage.cpp b/src/lib/webview/webpage.cpp index e63650bff..8eef418bd 100644 --- a/src/lib/webview/webpage.cpp +++ b/src/lib/webview/webpage.cpp @@ -453,6 +453,8 @@ QSslCertificate WebPage::sslCertificate() bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type) { m_lastRequestType = type; + m_lastRequestUrl = request.url(); + const QString &scheme = request.url().scheme(); if (scheme == "mailto" || scheme == "ftp") { @@ -480,7 +482,13 @@ void WebPage::populateNetworkRequest(QNetworkRequest &request) QVariant variant = qVariantFromValue((void*) pagePointer); request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), variant); - request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), m_lastRequestType); + + if (m_lastRequestUrl == request.url()) { + request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), m_lastRequestType); + if (m_lastRequestType == NavigationTypeLinkClicked) { + request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1")); + } + } } QWebPage* WebPage::createWindow(QWebPage::WebWindowType type) diff --git a/src/lib/webview/webpage.h b/src/lib/webview/webpage.h index 7cf00d106..b07ba896a 100644 --- a/src/lib/webview/webpage.h +++ b/src/lib/webview/webpage.h @@ -124,15 +124,17 @@ private: QupZilla* p_QupZilla; NetworkManagerProxy* m_networkProxy; - QWebPage::NavigationType m_lastRequestType; TabbedWebView* m_view; SpeedDial* m_speedDial; + QFileSystemWatcher* m_fileWatcher; + QEventLoop* m_runningLoop; + QSslCertificate m_sslCert; QList m_rejectedSslCerts; QList m_adBlockedEntries; - QFileSystemWatcher* m_fileWatcher; - QEventLoop* m_runningLoop; + QWebPage::NavigationType m_lastRequestType; + QUrl m_lastRequestUrl; int m_loadProgress; bool m_blockAlerts; diff --git a/src/lib/webview/webtab.cpp b/src/lib/webview/webtab.cpp index cc97c9d71..c5a3c1178 100644 --- a/src/lib/webview/webtab.cpp +++ b/src/lib/webview/webtab.cpp @@ -93,7 +93,7 @@ WebTab::WebTab(QupZilla* mainClass, LocationBar* locationBar) connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar.data(), SLOT(showUrl(QUrl))); connect(m_view, SIGNAL(rssChanged(bool)), m_locationBar.data(), SLOT(showRSSIcon(bool))); connect(m_view, SIGNAL(privacyChanged(bool)), m_locationBar.data(), SLOT(setPrivacy(bool))); - connect(m_locationBar.data(), SIGNAL(loadUrl(QUrl)), m_view, SLOT(load(QUrl))); + connect(m_locationBar.data(), SIGNAL(loadUrl(QUrl)), m_view, SLOT(userLoadAction(QUrl))); } TabbedWebView* WebTab::view() const diff --git a/src/plugins/GreaseMonkey/gm_plugin.cpp b/src/plugins/GreaseMonkey/gm_plugin.cpp index 9dd0daf43..8cdca9233 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.cpp +++ b/src/plugins/GreaseMonkey/gm_plugin.cpp @@ -38,7 +38,7 @@ PluginSpec GM_Plugin::pluginSpec() spec.name = "GreaseMonkey"; spec.info = "Userscripts for QupZilla"; spec.description = "Provides support for userscripts (www.userscripts.org)"; - spec.version = "0.1.1"; + spec.version = "0.2.1"; spec.author = "David Rosca "; spec.icon = QPixmap(":gm/data/icon.png"); spec.hasSettings = true; @@ -81,9 +81,7 @@ QNetworkReply* GM_Plugin::createRequest(QNetworkAccessManager::Operation op, con { Q_UNUSED(outgoingData) - int type = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), -1).toInt(); - - if (op == QNetworkAccessManager::GetOperation && QWebPage::NavigationType(type) == QWebPage::NavigationTypeLinkClicked) { + if (op == QNetworkAccessManager::GetOperation && request.rawHeader("X-QupZilla-UserLoadAction") == QByteArray("1")) { const QString &urlString = request.url().toString(QUrl::RemoveFragment | QUrl::RemoveQuery); if (urlString.endsWith(".user.js")) {