1/*
2 localdomainfilter.cpp
3
4 This file is part of the KDE project
5 SPDX-FileCopyrightText: 2002 Lubos Lunak <llunak@suse.cz>
6 SPDX-FileCopyrightText: 2010 Dawit Alemayehu <adawit@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "localdomainurifilter.h"
12
13#include <KPluginFactory>
14#include <KProtocolInfo>
15
16#include <QHostInfo>
17#include <QLoggingCategory>
18
19namespace
20{
21Q_LOGGING_CATEGORY(category, "kf.kio.urifilters.localdomain", QtWarningMsg)
22}
23
24bool LocalDomainUriFilter::filterUri(KUriFilterData &data) const
25{
26 const QUrl url = data.uri();
27 const QString protocol = url.scheme();
28
29 // When checking for local domain just validate it is indeed a local domain,
30 // but do not modify the hostname! See bug#
31 if ((protocol.isEmpty() || !KProtocolInfo::isKnownProtocol(protocol)) && m_hostPortPattern.match(subject: data.typedString()).hasMatch()) {
32 QString host(data.typedString().left(n: data.typedString().indexOf(c: QLatin1Char('/'))));
33 const int pos = host.indexOf(c: QLatin1Char(':'));
34 if (pos > -1) {
35 host.truncate(pos); // Remove port number
36 }
37 if (exists(host)) {
38 qCDebug(category) << "QHostInfo found a host called" << host;
39 QString scheme(data.defaultUrlScheme());
40 if (scheme.isEmpty()) {
41 scheme = QStringLiteral("http://");
42 }
43 setFilteredUri(data, uri: QUrl(scheme + data.typedString()));
44 setUriType(data, type: KUriFilterData::NetProtocol);
45 return true;
46 }
47 }
48
49 return false;
50}
51
52bool LocalDomainUriFilter::exists(const QString &host) const
53{
54 qCDebug(category) << "Checking if a host called" << host << "exists";
55 QHostInfo hostInfo = resolveName(hostname: host, timeout: 1500);
56 return hostInfo.error() == QHostInfo::NoError;
57}
58
59K_PLUGIN_CLASS_WITH_JSON(LocalDomainUriFilter, "localdomainurifilter.json")
60
61#include "localdomainurifilter.moc"
62#include "moc_localdomainurifilter.cpp"
63

source code of kio/src/urifilters/localdomain/localdomainurifilter.cpp