https://bugs.kde.org/show_bug.cgi?id=390452

--- Comment #7 from Caspar Schutijser <cas...@schutijser.com> ---
Hello everyone,

At the bottom is a diff that contains my failed attempts at solving the
problem.
Here is a description of those attempts:

1) Connect a slot to the QNetworkAccessManager::finished signal in
EmbeddedWebView that sets the "X-DNS-Prefetch-Control: off" header. Reason it
fails: the setRawHeader() method is protected so we cannot call it from
EmbeddedWebView.

2) Set the "X-DNS-Prefetch-Control: off" header in MsgPartNetworkReply. Reason
it fails: either QtWebkit does not see the header or it does not use it the
same way it uses an equivalent meta http-equiv tag. Either way, DNS prefetching
is not disabled.

3) Connect a slot to the QWebView::loadFinished signal in SimplePartWidget that
prepends the HTML with the meta http-equiv HTML tag to disable DNS prefetching.
Reason it fails: the page is rendered before the loadFinished signal is emitted
and the page us replaced. As such, the DNS requests are still performed before
the "fixed" page is put in place.

Does anyone have a better approach?

--

diff --git a/src/Gui/EmbeddedWebView.cpp b/src/Gui/EmbeddedWebView.cpp
index 6c530595..73a79d0b 100644
--- a/src/Gui/EmbeddedWebView.cpp
+++ b/src/Gui/EmbeddedWebView.cpp
@@ -75,6 +75,8 @@ EmbeddedWebView::EmbeddedWebView(QWidget *parent,
QNetworkAccessManager *network
     setPage(new ErrorCheckingPage(this));
     page()->setNetworkAccessManager(networkManager);

+    connect(networkManager, &QNetworkAccessManager::finished, this,
&EmbeddedWebView::slotReplyFinished);
+
     QWebSettings *s = settings();
     s->setAttribute(QWebSettings::JavascriptEnabled, false);
     s->setAttribute(QWebSettings::JavaEnabled, false);
@@ -177,6 +179,12 @@ void EmbeddedWebView::handlePageLoadFinished()
     page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
 }

+void EmbeddedWebView::slotReplyFinished(QNetworkReply *reply)
+{
+    // XXX: setRawHeader() is protected so we're unable to use it here
+//    reply->setRawHeader(QByteArrayLiteral("X-DNS-Prefetch-Control"),
QByteArrayLiteral("off"));
+}
+
 void EmbeddedWebView::changeEvent(QEvent *e)
 {
     QWebView::changeEvent(e);
diff --git a/src/Gui/EmbeddedWebView.h b/src/Gui/EmbeddedWebView.h
index 9ac83d15..b8001ebd 100644
--- a/src/Gui/EmbeddedWebView.h
+++ b/src/Gui/EmbeddedWebView.h
@@ -78,6 +78,7 @@ private slots:
     void autoScroll();
     void slotLinkClicked(const QUrl &url);
     void handlePageLoadFinished();
+    void slotReplyFinished(QNetworkReply *reply);
 private:
     QWidget *m_scrollParent;
     int m_scrollParentPadding;
diff --git a/src/Gui/SimplePartWidget.cpp b/src/Gui/SimplePartWidget.cpp
index de4d9a2d..f3f58f98 100644
--- a/src/Gui/SimplePartWidget.cpp
+++ b/src/Gui/SimplePartWidget.cpp
@@ -71,6 +71,8 @@ SimplePartWidget::SimplePartWidget(QWidget *parent,
Imap::Network::MsgPartNetAcc
             QWebSettings *s = settings();
             s->setFontFamily(QWebSettings::StandardFont, font.family());
         }
+    } else {
+        connect(this, &QWebView::loadFinished, this,
&SimplePartWidget::slotMarkupPage);
     }
     load(url);

@@ -107,6 +109,21 @@ SimplePartWidget::SimplePartWidget(QWidget *parent,
Imap::Network::MsgPartNetAcc
     }
 }

+void SimplePartWidget::slotMarkupPage()
+{
+    // NOTICE "single shot", we get a recursion otherwise!
+    disconnect(this, &QWebView::loadFinished, this,
&SimplePartWidget::slotMarkupPage);
+
+    // If there's no data, don't try to "fix it up"
+    if (!m_partIndex.isValid() ||
!m_partIndex.data(Imap::Mailbox::RoleIsFetched).toBool())
+        return;
+
+    // and finally set the page
+    static QString header(QStringLiteral("<meta
http-equiv=\"x-dns-prefetch-control\" content=\"off\">"));
+    page()->mainFrame()->setHtml(header +
m_partIndex.data(Imap::Mailbox::RolePartUnicodeText).toString());
+    qDebug() << "replaced the text";
+}
+
 void SimplePartWidget::slotMarkupPlainText()
 {
     // NOTICE "single shot", we get a recursion otherwise!
diff --git a/src/Gui/SimplePartWidget.h b/src/Gui/SimplePartWidget.h
index 14162e0e..3b3cb677 100644
--- a/src/Gui/SimplePartWidget.h
+++ b/src/Gui/SimplePartWidget.h
@@ -66,6 +66,7 @@ public:
     void buildContextMenu(const QPoint &point, QMenu &menu) const;
 private slots:
     void slotFileNameRequested(QString *fileName);
+    void slotMarkupPage();
     void slotMarkupPlainText();
     void slotDownloadPart();
     void slotDownloadMessage();
diff --git a/src/Imap/Network/MsgPartNetworkReply.cpp
b/src/Imap/Network/MsgPartNetworkReply.cpp
index 1135650e..275f1555 100644
--- a/src/Imap/Network/MsgPartNetworkReply.cpp
+++ b/src/Imap/Network/MsgPartNetworkReply.cpp
@@ -44,6 +44,9 @@
MsgPartNetworkReply::MsgPartNetworkReply(MsgPartNetAccessManager *parent, const
     url.setPath(part.data(Imap::Mailbox::RolePartPathToPart).toString());
     setUrl(url);

+    qDebug() << "MsgPartNetworkReply: X-DNS-Prefetch-Control: off";
+    setRawHeader(QByteArrayLiteral("X-DNS-Prefetch-Control"),
QByteArrayLiteral("off"));
+
     setOpenMode(QIODevice::ReadOnly | QIODevice::Unbuffered);
     Q_ASSERT(part.isValid());

@@ -107,6 +110,8 @@ void MsgPartNetworkReply::slotMyDataChanged()
     } else {
         setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
     }
+    qDebug() << "MsgPartNetworkReply: X-DNS-Prefetch-Control: off";
+    setRawHeader(QByteArrayLiteral("X-DNS-Prefetch-Control"),
QByteArrayLiteral("off"));
     setFinished(true);
     emit readyRead();
     emit finished();

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to