diff options
-rw-r--r-- | src/nam.cpp | 42 | ||||
-rw-r--r-- | src/nam.h | 39 | ||||
-rw-r--r-- | src/slxbrowser.cpp | 2 |
3 files changed, 82 insertions, 1 deletions
diff --git a/src/nam.cpp b/src/nam.cpp new file mode 100644 index 0000000..2f3fdd1 --- /dev/null +++ b/src/nam.cpp @@ -0,0 +1,42 @@ +#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 { + ok = (_list.match(url.toDisplayString(QUrl::NormalizePathSegments)).hasMatch() == _isWhitelist); + } + if (!ok) { + return new SlxDisabledNetworkReply(this, req, op); + } + return QNetworkAccessManager::createRequest(op, req, outgoingData); +} diff --git a/src/nam.h b/src/nam.h new file mode 100644 index 0000000..0036904 --- /dev/null +++ b/src/nam.h @@ -0,0 +1,39 @@ +#ifndef NAM_H_ +#define NAM_H_ + +#include <QNetworkAccessManager> +#include <QRegularExpression> +#include <QNetworkReply> + +/** + * Block certain requests based on URL + */ +class SlxNetworkAccessManager : public QNetworkAccessManager +{ +Q_OBJECT +public: + SlxNetworkAccessManager(QRegularExpression list, bool isWhitelist, QObject *parent = nullptr) + : QNetworkAccessManager(parent), _list(list), _isWhitelist(isWhitelist) {} +protected: + QNetworkReply* createRequest(QNetworkAccessManager::Operation op, + const QNetworkRequest &originalReq, QIODevice *outgoingData = nullptr) override; +private: + QRegularExpression _list; + bool _isWhitelist; +}; + + +class SlxDisabledNetworkReply : public QNetworkReply +{ + Q_OBJECT +public: + SlxDisabledNetworkReply(QObject *parent, const QNetworkRequest &req, + QNetworkAccessManager::Operation op); + + ~SlxDisabledNetworkReply() {} + void abort() override { } +protected: + qint64 readData(char *, qint64) override { return -1; } +}; + +#endif diff --git a/src/slxbrowser.cpp b/src/slxbrowser.cpp index 13ed326..aee94b2 100644 --- a/src/slxbrowser.cpp +++ b/src/slxbrowser.cpp @@ -208,7 +208,7 @@ static QRegularExpression urlListToRegExp(const QStringList &list) for (const QString &str : list) { QString mangled; if (str.contains(QLatin1String("//"))) { - mangled = str; + mangled = str; } else if (str.contains(QLatin1Char('/')) || str.contains(QLatin1String("**"))) { mangled = "*//" + str; } else { |