2011-06-08 09:36:45 +00:00
|
|
|
#include "NetworkAccessManager.h"
|
|
|
|
#include "WebPage.h"
|
|
|
|
#include <iostream>
|
2012-05-25 14:13:17 +00:00
|
|
|
#include <fstream>
|
2012-10-09 03:56:33 +00:00
|
|
|
#include "NoOpReply.h"
|
2012-11-18 07:48:12 +00:00
|
|
|
#include "NetworkReplyProxy.h"
|
2011-06-08 09:36:45 +00:00
|
|
|
|
|
|
|
NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
|
2012-05-25 14:13:17 +00:00
|
|
|
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
|
2012-10-22 02:35:20 +00:00
|
|
|
connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished(QNetworkReply *)));
|
2013-11-10 21:52:42 +00:00
|
|
|
disableKeyChainLookup();
|
2011-06-08 09:36:45 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 19:52:34 +00:00
|
|
|
QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
|
2011-06-25 11:31:19 +00:00
|
|
|
QNetworkRequest new_request(request);
|
2012-07-12 03:36:22 +00:00
|
|
|
QByteArray url = new_request.url().toEncoded();
|
2012-10-09 03:56:33 +00:00
|
|
|
if (this->isBlacklisted(new_request.url())) {
|
2012-11-18 07:48:12 +00:00
|
|
|
return new NetworkReplyProxy(new NoOpReply(new_request), this);
|
2012-10-09 03:56:33 +00: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-07 02:59:07 +00:00
|
|
|
new_request.setRawHeader(item.key().toLatin1(), item.value().toLatin1());
|
2012-10-09 03:56:33 +00:00
|
|
|
}
|
2012-11-18 07:48:12 +00:00
|
|
|
QNetworkReply *reply = new NetworkReplyProxy(QNetworkAccessManager::createRequest(operation, new_request, outgoingData), this);
|
2012-10-09 03:56:33 +00:00
|
|
|
emit requestCreated(url, reply);
|
|
|
|
return reply;
|
2011-09-09 19:52:34 +00:00
|
|
|
}
|
2012-10-09 03:56:33 +00:00
|
|
|
};
|
2012-10-22 02:35:20 +00:00
|
|
|
|
|
|
|
void NetworkAccessManager::finished(QNetworkReply *reply) {
|
2012-11-22 05:21:40 +00: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 07:48:12 +00:00
|
|
|
emit finished(requestedUrl, reply);
|
2012-11-22 05:21:40 +00:00
|
|
|
}
|
2012-10-22 02:35:20 +00:00
|
|
|
}
|
2011-06-08 09:36:45 +00:00
|
|
|
|
|
|
|
void NetworkAccessManager::addHeader(QString key, QString value) {
|
|
|
|
m_headers.insert(key, value);
|
2012-10-22 02:35:20 +00:00
|
|
|
}
|
2011-06-08 09:36:45 +00:00
|
|
|
|
2013-03-18 12:11:59 +00:00
|
|
|
void NetworkAccessManager::reset() {
|
2012-03-22 14:14:30 +00:00
|
|
|
m_headers.clear();
|
2013-03-18 12:11:59 +00:00
|
|
|
m_userName = QString();
|
|
|
|
m_password = QString();
|
2012-10-22 02:35:20 +00:00
|
|
|
}
|
2012-03-22 14:14:30 +00:00
|
|
|
|
2012-05-25 14:13:17 +00: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-17 00:19:29 +00:00
|
|
|
if (m_userName != authenticator->user())
|
|
|
|
authenticator->setUser(m_userName);
|
|
|
|
if (m_password != authenticator->password())
|
|
|
|
authenticator->setPassword(m_password);
|
2012-05-25 14:13:17 +00:00
|
|
|
}
|
2012-10-22 02:35:20 +00:00
|
|
|
|
2012-10-09 03:56:33 +00:00
|
|
|
void NetworkAccessManager::setUrlBlacklist(QStringList urlBlacklist) {
|
|
|
|
m_urlBlacklist.clear();
|
|
|
|
|
|
|
|
QStringListIterator iter(urlBlacklist);
|
|
|
|
while (iter.hasNext()) {
|
2012-12-11 13:14:34 +00:00
|
|
|
m_urlBlacklist << iter.next();
|
2012-10-09 03:56:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool NetworkAccessManager::isBlacklisted(QUrl url) {
|
2012-12-11 13:14:34 +00:00
|
|
|
QString urlString = url.toString();
|
|
|
|
QStringListIterator iter(m_urlBlacklist);
|
2012-10-09 03:56:33 +00:00
|
|
|
|
|
|
|
while (iter.hasNext()) {
|
2012-12-11 13:14:34 +00:00
|
|
|
QRegExp blacklisted = QRegExp(iter.next());
|
|
|
|
blacklisted.setPatternSyntax(QRegExp::Wildcard);
|
2012-10-09 03:56:33 +00:00
|
|
|
|
2012-12-11 13:14:34 +00:00
|
|
|
if(urlString.contains(blacklisted)) {
|
2012-10-09 03:56:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-11-10 21:52:42 +00: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);
|
|
|
|
}
|