2011-06-08 05:36:45 -04:00
|
|
|
#include "NetworkAccessManager.h"
|
|
|
|
#include "WebPage.h"
|
|
|
|
#include <iostream>
|
2012-05-25 10:13:17 -04:00
|
|
|
#include <fstream>
|
2012-10-08 23:56:33 -04:00
|
|
|
#include "NoOpReply.h"
|
2012-11-18 02:48:12 -05:00
|
|
|
#include "NetworkReplyProxy.h"
|
2011-06-08 05:36:45 -04:00
|
|
|
|
|
|
|
NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
|
2012-05-25 10:13:17 -04:00
|
|
|
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
|
2012-10-21 22:35:20 -04:00
|
|
|
connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished(QNetworkReply *)));
|
2013-11-10 16:52:42 -05:00
|
|
|
disableKeyChainLookup();
|
2011-06-08 05:36:45 -04:00
|
|
|
}
|
|
|
|
|
2011-09-09 15:52:34 -04:00
|
|
|
QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
|
2011-06-25 07:31:19 -04:00
|
|
|
QNetworkRequest new_request(request);
|
2012-07-11 23:36:22 -04:00
|
|
|
QByteArray url = new_request.url().toEncoded();
|
2012-10-08 23:56:33 -04:00
|
|
|
if (this->isBlacklisted(new_request.url())) {
|
2012-11-18 02:48:12 -05:00
|
|
|
return new NetworkReplyProxy(new NoOpReply(new_request), this);
|
2012-10-08 23:56:33 -04:00
|
|
|
} else {
|
|
|
|
if (operation != QNetworkAccessManager::PostOperation && operation != QNetworkAccessManager::PutOperation) {
|
|
|
|
new_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant());
|
|
|
|
}
|
|
|
|
QHashIterator<QString, QString> item(m_headers);
|
|
|
|
while (item.hasNext()) {
|
|
|
|
item.next();
|
2013-01-06 21:59:07 -05:00
|
|
|
new_request.setRawHeader(item.key().toLatin1(), item.value().toLatin1());
|
2012-10-08 23:56:33 -04:00
|
|
|
}
|
2012-11-18 02:48:12 -05:00
|
|
|
QNetworkReply *reply = new NetworkReplyProxy(QNetworkAccessManager::createRequest(operation, new_request, outgoingData), this);
|
2012-10-08 23:56:33 -04:00
|
|
|
emit requestCreated(url, reply);
|
|
|
|
return reply;
|
2011-09-09 15:52:34 -04:00
|
|
|
}
|
2012-10-08 23:56:33 -04:00
|
|
|
};
|
2012-10-21 22:35:20 -04:00
|
|
|
|
|
|
|
void NetworkAccessManager::finished(QNetworkReply *reply) {
|
2012-11-22 00:21:40 -05:00
|
|
|
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
|
|
|
if (redirectUrl.isValid())
|
|
|
|
m_redirectMappings[reply->url().resolved(redirectUrl)] = reply->url();
|
|
|
|
else {
|
|
|
|
QUrl requestedUrl = reply->url();
|
|
|
|
while (m_redirectMappings.contains(requestedUrl))
|
|
|
|
requestedUrl = m_redirectMappings.take(requestedUrl);
|
2012-11-18 02:48:12 -05:00
|
|
|
emit finished(requestedUrl, reply);
|
2012-11-22 00:21:40 -05:00
|
|
|
}
|
2012-10-21 22:35:20 -04:00
|
|
|
}
|
2011-06-08 05:36:45 -04:00
|
|
|
|
|
|
|
void NetworkAccessManager::addHeader(QString key, QString value) {
|
|
|
|
m_headers.insert(key, value);
|
2012-10-21 22:35:20 -04:00
|
|
|
}
|
2011-06-08 05:36:45 -04:00
|
|
|
|
2013-03-18 08:11:59 -04:00
|
|
|
void NetworkAccessManager::reset() {
|
2012-03-22 10:14:30 -04:00
|
|
|
m_headers.clear();
|
2013-03-18 08:11:59 -04:00
|
|
|
m_userName = QString();
|
|
|
|
m_password = QString();
|
2012-10-21 22:35:20 -04:00
|
|
|
}
|
2012-03-22 10:14:30 -04:00
|
|
|
|
2012-05-25 10:13:17 -04:00
|
|
|
void NetworkAccessManager::setUserName(const QString &userName) {
|
|
|
|
m_userName = userName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAccessManager::setPassword(const QString &password) {
|
|
|
|
m_password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkAccessManager::provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) {
|
|
|
|
Q_UNUSED(reply);
|
2012-12-16 19:19:29 -05:00
|
|
|
if (m_userName != authenticator->user())
|
|
|
|
authenticator->setUser(m_userName);
|
|
|
|
if (m_password != authenticator->password())
|
|
|
|
authenticator->setPassword(m_password);
|
2012-05-25 10:13:17 -04:00
|
|
|
}
|
2012-10-21 22:35:20 -04:00
|
|
|
|
2012-10-08 23:56:33 -04:00
|
|
|
void NetworkAccessManager::setUrlBlacklist(QStringList urlBlacklist) {
|
|
|
|
m_urlBlacklist.clear();
|
|
|
|
|
|
|
|
QStringListIterator iter(urlBlacklist);
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
m_urlBlacklist << QUrl(iter.next());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool NetworkAccessManager::isBlacklisted(QUrl url) {
|
|
|
|
QListIterator<QUrl> iter(m_urlBlacklist);
|
|
|
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
QUrl blacklisted = iter.next();
|
|
|
|
|
|
|
|
if (blacklisted == url) {
|
|
|
|
return true;
|
|
|
|
} else if (blacklisted.path().isEmpty() && blacklisted.isParentOf(url)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-11-10 16:52:42 -05:00
|
|
|
/*
|
|
|
|
* This is a workaround for a Qt 5/OS X bug:
|
|
|
|
* https://bugreports.qt-project.org/browse/QTBUG-30434
|
|
|
|
*/
|
|
|
|
void NetworkAccessManager::disableKeyChainLookup() {
|
|
|
|
QNetworkProxy fixedProxy = proxy();
|
|
|
|
fixedProxy.setHostName(" ");
|
|
|
|
setProxy(fixedProxy);
|
|
|
|
}
|