File indexing completed on 2024-05-12 11:54:37
0001 /* 0002 localdomainfilter.cpp 0003 0004 This file is part of the KDE project 0005 SPDX-FileCopyrightText: 2002 Lubos Lunak <llunak@suse.cz> 0006 SPDX-FileCopyrightText: 2010 Dawit Alemayehu <adawit@kde.org> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "localdomainurifilter.h" 0012 0013 #include <KPluginFactory> 0014 #include <KProtocolInfo> 0015 0016 #include <QHostInfo> 0017 #include <QLoggingCategory> 0018 0019 namespace 0020 { 0021 Q_LOGGING_CATEGORY(category, "kf.kio.urifilters.localdomain", QtWarningMsg) 0022 } 0023 0024 /** 0025 * IMPORTANT: If you change anything here, make sure you run the kurifiltertest 0026 * regression test (this should be included as part of "make test"). 0027 */ 0028 LocalDomainUriFilter::LocalDomainUriFilter(QObject *parent, const QVariantList & /*args*/) 0029 : KUriFilterPlugin(QStringLiteral("localdomainurifilter"), parent) 0030 { 0031 static const char16_t pattern[] = uR"--([a-zA-Z0-9][a-zA-Z0-9+-]*(?:\:[0-9]{1,5})?(?:/[\w:@&=+$,-.!~*'()]*)*)--"; 0032 m_hostPortPattern = QRegularExpression(QRegularExpression::anchoredPattern(pattern)); 0033 } 0034 0035 bool LocalDomainUriFilter::filterUri(KUriFilterData &data) const 0036 { 0037 const QUrl url = data.uri(); 0038 const QString protocol = url.scheme(); 0039 0040 // When checking for local domain just validate it is indeed a local domain, 0041 // but do not modify the hostname! See bug# 0042 if ((protocol.isEmpty() || !KProtocolInfo::isKnownProtocol(protocol)) && m_hostPortPattern.match(data.typedString()).hasMatch()) { 0043 QString host(data.typedString().left(data.typedString().indexOf(QLatin1Char('/')))); 0044 const int pos = host.indexOf(QLatin1Char(':')); 0045 if (pos > -1) { 0046 host.truncate(pos); // Remove port number 0047 } 0048 if (exists(host)) { 0049 qCDebug(category) << "QHostInfo found a host called" << host; 0050 QString scheme(data.defaultUrlScheme()); 0051 if (scheme.isEmpty()) { 0052 scheme = QStringLiteral("http://"); 0053 } 0054 setFilteredUri(data, QUrl(scheme + data.typedString())); 0055 setUriType(data, KUriFilterData::NetProtocol); 0056 return true; 0057 } 0058 } 0059 0060 return false; 0061 } 0062 0063 bool LocalDomainUriFilter::exists(const QString &host) const 0064 { 0065 qCDebug(category) << "Checking if a host called" << host << "exists"; 0066 QHostInfo hostInfo = resolveName(host, 1500); 0067 return hostInfo.error() == QHostInfo::NoError; 0068 } 0069 0070 K_PLUGIN_CLASS_WITH_JSON(LocalDomainUriFilter, "localdomainurifilter.json") 0071 0072 #include "localdomainurifilter.moc" 0073 0074 #include "moc_localdomainurifilter.cpp"