KIO

fixhosturifilter.cpp
1/*
2 fixhostfilter.cpp
3
4 This file is part of the KDE project
5 SPDX-FileCopyrightText: 2007 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 "fixhosturifilter.h"
12
13#include <QHostInfo>
14
15#include <KPluginFactory>
16
17static bool isHttpUrl(const QString &scheme)
18{
19 /* clang-format off */
20 return scheme.compare(QLatin1String("http"), Qt::CaseInsensitive) == 0
21 || scheme.compare(QLatin1String("https"), Qt::CaseInsensitive) == 0
22 || scheme.compare(QLatin1String("webdav"), Qt::CaseInsensitive) == 0
23 || scheme.compare(QLatin1String("webdavs"), Qt::CaseInsensitive) == 0;
24 /* clang-format on */
25}
26
27static bool hasCandidateHostName(const QString &host)
28{
29 return host.contains(QLatin1Char('.')) && !host.startsWith(QLatin1String("www."), Qt::CaseInsensitive);
30}
31
32bool FixHostUriFilter::filterUri(KUriFilterData &data) const
33{
34 QUrl url = data.uri();
35
36 const QString protocol = url.scheme();
37 const bool isHttp = isHttpUrl(protocol);
38
39 if (isHttp || protocol == data.defaultUrlScheme()) {
40 const QString host = url.host();
41 if (hasCandidateHostName(host) && !isResolvable(host)) {
42 if (isHttp) {
43 url.setHost((QLatin1String("www.") + host));
44 if (exists(url.host())) {
45 setFilteredUri(data, url);
46 setUriType(data, KUriFilterData::NetProtocol);
47 return true;
48 }
49 }
50 }
51 }
52
53 return false;
54}
55
56bool FixHostUriFilter::isResolvable(const QString &host) const
57{
58 // Unlike exists, this function returns true if the lookup timeout out.
59 QHostInfo info = resolveName(host, 1500);
60 return info.error() == QHostInfo::NoError || info.error() == QHostInfo::UnknownError;
61}
62
63bool FixHostUriFilter::exists(const QString &host) const
64{
65 QHostInfo info = resolveName(host, 1500);
66 return info.error() == QHostInfo::NoError;
67}
68
69K_PLUGIN_CLASS_WITH_JSON(FixHostUriFilter, "fixhosturifilter.json")
70
71#include "fixhosturifilter.moc"
72
73#include "moc_fixhosturifilter.cpp"
This filter tries to automatically prepend www.
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
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.
@ 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.
std::pair< bool, CatalogObject > resolveName(const QString &name)
HostInfoError error() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
CaseInsensitive
QString host(ComponentFormattingOptions options) const const
QString scheme() const const
void setHost(const QString &host, ParsingMode mode)
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.