diff options
author | Peter Varga <pvarga@inf.u-szeged.hu> | 2015-08-18 16:15:25 +0200 |
---|---|---|
committer | Peter Varga <pvarga@inf.u-szeged.hu> | 2015-09-08 09:15:26 +0000 |
commit | 95d5240475c2c88908a1df23453efa05ed64dd34 (patch) | |
tree | 1f6d2d33cdae00fd35779ad888d76bc47af8b11e /examples/webenginewidgets/demobrowser/browserapplication.cpp | |
parent | b715310a4fdc2a5e215c99bae042cd820e9a1e27 (diff) | |
download | qtwebengine-95d5240475c2c88908a1df23453efa05ed64dd34.tar.gz |
Fix favicon load after authentication in browser example
In the widget browser example QNetworkAccessManager downloads the
favicon for a webpage. In case of HTTP or proxy authentication
the credentials may not be cached when QNetworkAccessManager
tries to load the favicon. Therefore, store last credentials
and provide it to QNetworkAccessManager when it emits
authenticationRequired signal.
Change-Id: I2d057bfa7291a13cec30db9debaf30382415122b
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'examples/webenginewidgets/demobrowser/browserapplication.cpp')
-rw-r--r-- | examples/webenginewidgets/demobrowser/browserapplication.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/examples/webenginewidgets/demobrowser/browserapplication.cpp b/examples/webenginewidgets/demobrowser/browserapplication.cpp index 84ce35d9d..a85bce2c3 100644 --- a/examples/webenginewidgets/demobrowser/browserapplication.cpp +++ b/examples/webenginewidgets/demobrowser/browserapplication.cpp @@ -63,6 +63,7 @@ #include <QtNetwork/QLocalServer> #include <QtNetwork/QLocalSocket> #include <QtNetwork/QNetworkProxy> +#include <QtNetwork/QNetworkReply> #include <QtNetwork/QSslSocket> #include <QWebEngineProfile> @@ -491,6 +492,10 @@ QNetworkAccessManager *BrowserApplication::networkAccessManager() { if (!s_networkAccessManager) { s_networkAccessManager = new QNetworkAccessManager(); + connect(s_networkAccessManager, &QNetworkAccessManager::authenticationRequired, + BrowserApplication::instance(), &BrowserApplication::authenticationRequired); + connect(s_networkAccessManager, &QNetworkAccessManager::proxyAuthenticationRequired, + BrowserApplication::instance(), &BrowserApplication::proxyAuthenticationRequired); } return s_networkAccessManager; } @@ -549,3 +554,68 @@ void BrowserApplication::setPrivateBrowsing(bool privateBrowsing) } emit privateBrowsingChanged(privateBrowsing); } + +void BrowserApplication::setLastAuthenticator(QAuthenticator *authenticator) +{ + m_lastAuthenticator = QAuthenticator(*authenticator); +} + +void BrowserApplication::setLastProxyAuthenticator(QAuthenticator *authenticator) +{ + m_lastProxyAuthenticator = QAuthenticator(*authenticator); +} + +void BrowserApplication::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) +{ + if (m_lastAuthenticator.isNull()) + return; + + + Q_ASSERT(m_lastAuthenticator.option("key").isValid()); + QByteArray lastKey = m_lastAuthenticator.option("key").toByteArray(); + QByteArray key = BrowserApplication::authenticationKey(reply->url(), authenticator->realm()); + + if (lastKey == key) + *authenticator = m_lastAuthenticator; +} + +void BrowserApplication::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) +{ + if (m_lastProxyAuthenticator.isNull()) + return; + + QNetworkProxy::ProxyType proxyType = proxy.type(); + if (proxyType != QNetworkProxy::HttpProxy || proxyType != QNetworkProxy::HttpCachingProxy) + return; + + Q_ASSERT(m_lastProxyAuthenticator.option("host").isValid()); + QByteArray lastKey = m_lastProxyAuthenticator.option("key").toByteArray(); + QByteArray key = BrowserApplication::proxyAuthenticationKey(proxy, authenticator->realm()); + + if (lastKey == key) + *authenticator = m_lastAuthenticator; +} + +// TODO: Remove these functions (QTBUG-47967) +QByteArray BrowserApplication::authenticationKey(const QUrl &url, const QString &realm) +{ + QUrl copy = url; + copy.setFragment(realm); + return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery); +} + +QByteArray BrowserApplication::proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm) +{ + QString host = QString("%1:%2").arg(proxy.hostName()).arg(proxy.port()); + return BrowserApplication::proxyAuthenticationKey(proxy.user(), host, realm); +} + +QByteArray BrowserApplication::proxyAuthenticationKey(const QString &user, const QString &host, const QString &realm) +{ + QUrl key; + key.setScheme(QLatin1String("proxy-http")); + key.setUserName(user); + key.setHost(host); + key.setFragment(realm); + return "auth:" + key.toEncoded(); +} |