Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Share the NetworkAccessManager across WebPages

Destroying the NetworkAccessManager seems to result in "terminate called
without an active exception" segfaults. The documentation states that an
application requires only a single single NetworkAccessManager.
  • Loading branch information...
commit be22bfe24468c394726c1f36bc5794c5592aa52c 1 parent 5011593
@mhoran mhoran authored
View
10 spec/driver_spec.rb
@@ -2018,9 +2018,19 @@ def which_for(character)
"401 Unauthorized."
end
end
+
+ get "/reset" do
+ headers "WWW-Authenticate" => 'Basic realm="Secure Area"'
+ status 401
+ "401 Unauthorized."
+ end
end
end
+ before do
+ visit('/reset')
+ end
+
it "can authenticate a request" do
driver.browser.authenticate('user', 'password')
visit("/")
View
3  src/Authenticate.cpp
@@ -1,6 +1,7 @@
#include "Authenticate.h"
#include "WebPage.h"
#include "NetworkAccessManager.h"
+#include "WebPageManager.h"
Authenticate::Authenticate(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
}
@@ -9,7 +10,7 @@ void Authenticate::start() {
QString username = arguments()[0];
QString password = arguments()[1];
- NetworkAccessManager* networkAccessManager = page()->networkAccessManager();
+ NetworkAccessManager* networkAccessManager = manager()->networkAccessManager();
networkAccessManager->setUserName(username);
networkAccessManager->setPassword(password);
View
2  src/Header.cpp
@@ -9,7 +9,7 @@ Header::Header(WebPageManager *manager, QStringList &arguments, QObject *parent)
void Header::start() {
QString key = arguments()[0];
QString value = arguments()[1];
- NetworkAccessManager* networkAccessManager = page()->networkAccessManager();
+ NetworkAccessManager* networkAccessManager = manager()->networkAccessManager();
if (key.toLower().replace("-", "_") == "user_agent") {
page()->setUserAgent(value);
} else {
View
4 src/NetworkAccessManager.cpp
@@ -46,8 +46,10 @@ void NetworkAccessManager::addHeader(QString key, QString value) {
m_headers.insert(key, value);
}
-void NetworkAccessManager::resetHeaders() {
+void NetworkAccessManager::reset() {
m_headers.clear();
+ m_userName = QString();
+ m_password = QString();
}
void NetworkAccessManager::setUserName(const QString &userName) {
View
5 src/NetworkAccessManager.h
@@ -1,3 +1,5 @@
+#ifndef __NETWORKACCESSMANAGER_H
+#define __NETWORKACCESSMANAGER_H
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
@@ -10,7 +12,7 @@ class NetworkAccessManager : public QNetworkAccessManager {
public:
NetworkAccessManager(QObject *parent = 0);
void addHeader(QString key, QString value);
- void resetHeaders();
+ void reset();
void setUserName(const QString &userName);
void setPassword(const QString &password);
void setUrlBlacklist(QStringList urlBlacklist);
@@ -34,3 +36,4 @@ class NetworkAccessManager : public QNetworkAccessManager {
void requestCreated(QByteArray &url, QNetworkReply *reply);
void finished(QUrl &, QNetworkReply *);
};
+#endif
View
2  src/SetProxy.cpp
@@ -18,6 +18,6 @@ void SetProxy::start()
arguments()[2],
arguments()[3]);
- page()->networkAccessManager()->setProxy(proxy);
+ manager()->networkAccessManager()->setProxy(proxy);
finish(true);
}
View
2  src/SetUrlBlacklist.cpp
@@ -8,7 +8,7 @@ SetUrlBlacklist::SetUrlBlacklist(WebPageManager *manager, QStringList &arguments
}
void SetUrlBlacklist::start() {
- NetworkAccessManager* networkAccessManager = page()->networkAccessManager();
+ NetworkAccessManager* networkAccessManager = manager()->networkAccessManager();
networkAccessManager->setUrlBlacklist(arguments());
finish(true);
}
View
18 src/WebPage.cpp
@@ -43,15 +43,13 @@ void WebPage::resetWindowSize() {
}
void WebPage::setCustomNetworkAccessManager() {
- NetworkAccessManager *manager = new NetworkAccessManager(this);
- manager->setCookieJar(m_manager->cookieJar());
- this->setNetworkAccessManager(manager);
- connect(manager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
- this, SLOT(handleSslErrorsForReply(QNetworkReply *, QList<QSslError>)));
- connect(manager, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
+ setNetworkAccessManager(m_manager->networkAccessManager());
+ connect(networkAccessManager(), SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
+ SLOT(handleSslErrorsForReply(QNetworkReply *, QList<QSslError>)));
+ connect(networkAccessManager(), SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
SIGNAL(requestCreated(QByteArray &, QNetworkReply *)));
- connect(manager, SIGNAL(finished(QUrl &, QNetworkReply *)),
- SLOT(replyFinished(QUrl &, QNetworkReply *)));
+ connect(networkAccessManager(), SIGNAL(finished(QUrl &, QNetworkReply *)),
+ SLOT(replyFinished(QUrl &, QNetworkReply *)));
}
void WebPage::replyFinished(QUrl &requestedUrl, QNetworkReply *reply) {
@@ -291,10 +289,6 @@ QString WebPage::contentType() {
return currentFrame()->property("contentType").toString();
}
-NetworkAccessManager *WebPage::networkAccessManager() {
- return qobject_cast<NetworkAccessManager *>(QWebPage::networkAccessManager());
-}
-
void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
QVariant contentMimeType = reply->header(QNetworkRequest::ContentTypeHeader);
if(!contentMimeType.isNull()) {
View
2  src/WebPage.h
@@ -8,7 +8,6 @@
#include <QtNetwork>
class WebPageManager;
-class NetworkAccessManager;
class InvocationResult;
class NetworkReplyProxy;
@@ -40,7 +39,6 @@ class WebPage : public QWebPage {
QString getWindowName();
bool matchesWindowSelector(QString);
void setFocus();
- NetworkAccessManager *networkAccessManager();
void unsupportedContentFinishedReply(QNetworkReply *reply);
QStringList pageHeaders();
QByteArray body();
View
8 src/WebPageManager.cpp
@@ -1,6 +1,7 @@
#include "WebPageManager.h"
#include "WebPage.h"
#include "NetworkCookieJar.h"
+#include "NetworkAccessManager.h"
WebPageManager::WebPageManager(QObject *parent) : QObject(parent) {
m_ignoreSslErrors = false;
@@ -9,9 +10,15 @@ WebPageManager::WebPageManager(QObject *parent) : QObject(parent) {
m_loggingEnabled = false;
m_ignoredOutput = new QFile(this);
m_timeout = -1;
+ m_networkAccessManager = new NetworkAccessManager(this);
+ m_networkAccessManager->setCookieJar(m_cookieJar);
createPage(this)->setFocus();
}
+NetworkAccessManager *WebPageManager::networkAccessManager() {
+ return m_networkAccessManager;
+}
+
void WebPageManager::append(WebPage *value) {
m_pages.append(value);
}
@@ -102,6 +109,7 @@ void WebPageManager::setTimeout(int timeout) {
void WebPageManager::reset() {
m_timeout = -1;
m_cookieJar->clearCookies();
+ m_networkAccessManager->reset();
m_pages.first()->deleteLater();
m_pages.clear();
createPage(this)->setFocus();
View
3  src/WebPageManager.h
@@ -9,6 +9,7 @@
class WebPage;
class NetworkCookieJar;
+class NetworkAccessManager;
class WebPageManager : public QObject {
Q_OBJECT
@@ -30,6 +31,7 @@ class WebPageManager : public QObject {
QDebug logger() const;
void enableLogging();
void replyFinished(QNetworkReply *reply);
+ NetworkAccessManager *networkAccessManager();
public slots:
void emitLoadStarted();
@@ -54,6 +56,7 @@ class WebPageManager : public QObject {
bool m_loggingEnabled;
QFile *m_ignoredOutput;
int m_timeout;
+ NetworkAccessManager *m_networkAccessManager;
};
#endif // _WEBPAGEMANAGER_H
Please sign in to comment.
Something went wrong with that request. Please try again.