summaryrefslogtreecommitdiffstats
path: root/src/nam.cpp
blob: b244505fefcf4fc2d48bc19ec1f1e4df95a102c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}