File indexing completed on 2024-05-12 05:46:35
0001 /* 0002 localdomainfilter.cpp 0003 0004 This file is part of the KDE project 0005 Copyright (C) 2002 Lubos Lunak <llunak@suse.cz> 0006 Copyright (C) 2010 Dawit Alemayehu <adawit@kde.org> 0007 0008 This program is free software; you can redistribute it and/or modify 0009 it under the terms of the GNU General Public License as published by 0010 the Free Software Foundation; either version 2 of the License, or 0011 (at your option) any later version. 0012 0013 This program is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0016 GNU General Public License for more details. 0017 0018 You should have received a copy of the GNU General Public License 0019 along with this program. If not, see <http://www.gnu.org/licenses/>. 0020 */ 0021 0022 #include "localdomainurifilter.h" 0023 0024 #include <KProtocolInfo> 0025 #include <KPluginFactory> 0026 0027 #include <QHostInfo> 0028 #include <QLoggingCategory> 0029 0030 #define QL1C(x) QLatin1Char(x) 0031 #define QL1S(x) QLatin1String(x) 0032 0033 #define HOSTPORT_PATTERN "[a-zA-Z0-9][a-zA-Z0-9+-]*(?:\\:[0-9]{1,5})?(?:/[\\w:@&=+$,-.!~*'()]*)*" 0034 0035 namespace { 0036 QLoggingCategory category("org.kde.kurifilter-localdomain", QtWarningMsg); 0037 } 0038 0039 /** 0040 * IMPORTANT: If you change anything here, make sure you run the kurifiltertest 0041 * regression test (this should be included as part of "make test"). 0042 */ 0043 LocalDomainUriFilter::LocalDomainUriFilter(QObject *parent, const QVariantList & /*args*/) 0044 : KUriFilterPlugin(QStringLiteral("localdomainurifilter"), parent) 0045 , m_hostPortPattern(QL1S(HOSTPORT_PATTERN)) 0046 { 0047 } 0048 0049 bool LocalDomainUriFilter::filterUri(KUriFilterData &data) const 0050 { 0051 const QUrl url = data.uri(); 0052 const QString protocol = url.scheme(); 0053 0054 // When checking for local domain just validate it is indeed a local domain, 0055 // but do not modify the hostname! See bug# 0056 if ((protocol.isEmpty() || !KProtocolInfo::isKnownProtocol(protocol)) 0057 && m_hostPortPattern.exactMatch(data.typedString())) { 0058 QString host(data.typedString().left(data.typedString().indexOf(QL1C('/')))); 0059 const int pos = host.indexOf(QL1C(':')); 0060 if (pos > -1) { 0061 host.truncate(pos); // Remove port number 0062 } 0063 if (exists(host)) { 0064 qCDebug(category) << "QHostInfo found a host called" << host; 0065 QString scheme(data.defaultUrlScheme()); 0066 if (scheme.isEmpty()) { 0067 scheme = QStringLiteral("http://"); 0068 } 0069 setFilteredUri(data, QUrl(scheme + data.typedString())); 0070 setUriType(data, KUriFilterData::NetProtocol); 0071 return true; 0072 } 0073 } 0074 0075 return false; 0076 } 0077 0078 bool LocalDomainUriFilter::exists(const QString &host) const 0079 { 0080 qCDebug(category) << "Checking if a host called" << host << "exists"; 0081 QHostInfo hostInfo = resolveName(host, 1500); 0082 return hostInfo.error() == QHostInfo::NoError; 0083 } 0084 0085 K_PLUGIN_CLASS_WITH_JSON(LocalDomainUriFilter, "localdomainurifilter.json") 0086 0087 #include "localdomainurifilter.moc"