File indexing completed on 2024-10-13 03:38:34

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 bool LocalDomainUriFilter::filterUri(KUriFilterData &data) const
0025 {
0026     const QUrl url = data.uri();
0027     const QString protocol = url.scheme();
0028 
0029     // When checking for local domain just validate it is indeed a local domain,
0030     // but do not modify the hostname! See bug#
0031     if ((protocol.isEmpty() || !KProtocolInfo::isKnownProtocol(protocol)) && m_hostPortPattern.match(data.typedString()).hasMatch()) {
0032         QString host(data.typedString().left(data.typedString().indexOf(QLatin1Char('/'))));
0033         const int pos = host.indexOf(QLatin1Char(':'));
0034         if (pos > -1) {
0035             host.truncate(pos); // Remove port number
0036         }
0037         if (exists(host)) {
0038             qCDebug(category) << "QHostInfo found a host called" << host;
0039             QString scheme(data.defaultUrlScheme());
0040             if (scheme.isEmpty()) {
0041                 scheme = QStringLiteral("http://");
0042             }
0043             setFilteredUri(data, QUrl(scheme + data.typedString()));
0044             setUriType(data, KUriFilterData::NetProtocol);
0045             return true;
0046         }
0047     }
0048 
0049     return false;
0050 }
0051 
0052 bool LocalDomainUriFilter::exists(const QString &host) const
0053 {
0054     qCDebug(category) << "Checking if a host called" << host << "exists";
0055     QHostInfo hostInfo = resolveName(host, 1500);
0056     return hostInfo.error() == QHostInfo::NoError;
0057 }
0058 
0059 K_PLUGIN_CLASS_WITH_JSON(LocalDomainUriFilter, "localdomainurifilter.json")
0060 
0061 #include "localdomainurifilter.moc"
0062 #include "moc_localdomainurifilter.cpp"