diff options
Diffstat (limited to 'src/nam.cpp')
-rw-r--r-- | src/nam.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/nam.cpp b/src/nam.cpp new file mode 100644 index 0000000..6128c40 --- /dev/null +++ b/src/nam.cpp @@ -0,0 +1,54 @@ +#include "nam.h" + +#include <QCoreApplication> +#include <QDebug> + +SlxDisabledNetworkReply::SlxDisabledNetworkReply(QObject *parent, const QNetworkRequest &req, + QNetworkAccessManager::Operation op) + : QNetworkReply(parent) +{ + setRequest(req); + setUrl(req.url()); + setOperation(op); + setFinished(true); + qRegisterMetaType<QNetworkReply::NetworkError>(); + QString msg = QCoreApplication::translate("QNetworkAccessManager", + "Network access is disabled."); + setError(UnknownNetworkError, msg); + QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, + Q_ARG(QNetworkReply::NetworkError, UnknownNetworkError)); + QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); +} + +QNetworkReply* SlxNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op, + const QNetworkRequest &req, QIODevice *outgoingData) +{ + const QUrl url(req.url()); + //qDebug() << url; + bool ok; + if (url.isLocalFile()) { + ok = true; + } else if (url.scheme() == QLatin1String("qrc") || url.scheme() == QLatin1String("data")) { + ok = true; + } else if (url.host().isEmpty()) { + ok = true; + } else { + auto str = url.toDisplayString(QUrl::NormalizePathSegments); + if (_white.isValid() && _white.match(str).hasMatch()) { + // We have a whitelist, and it matches - allow + ok = true; + } else if (_black.isValid() && _black.match(str).hasMatch()) { + // we have a blacklist, and it matches - deny + ok = false; + } else { + // neither matched; if we have a whitelist: deny; otherwise: allow + ok = !_white.isValid(); + } + } + if (!ok) { + return new SlxDisabledNetworkReply(this, req, op); + } + auto cp(req); + cp.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, QVariant(false)); + return QNetworkAccessManager::createRequest(op, cp, outgoingData); +} |