KIO

localdomainurifilter.cpp
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(data.typedString()).hasMatch()) {
32 QString host(data.typedString().left(data.typedString().indexOf(QLatin1Char('/'))));
33 const int pos = host.indexOf(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, QUrl(scheme + data.typedString()));
44 setUriType(data, 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(host, 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"
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
static bool isKnownProtocol(const QUrl &url)
Returns whether a protocol is installed that is able to handle url.
This class is a basic messaging class used to exchange filtering information between the filter plugi...
Definition kurifilter.h:153
QUrl uri() const
Returns the filtered or the original URL.
QString typedString() const
The string as typed by the user, before any URL processing is done.
@ NetProtocol
Any network protocol: http, ftp, nttp, pop3, etc...
Definition kurifilter.h:159
QString defaultUrlScheme() const
Returns the default protocol to use when filtering potentially valid url inputs.
This filter takes care of hostnames in the local search domain.
std::pair< bool, CatalogObject > resolveName(const QString &name)
HostInfoError error() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
bool hasMatch() const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(qsizetype n) const const
QString scheme() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.