1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <qglobal.h> |
41 | |
42 | #if QT_CONFIG(topleveldomain) |
43 | |
44 | #include "qplatformdefs.h" |
45 | #include "qurl.h" |
46 | #include "private/qurltlds_p.h" |
47 | #include "private/qtldurl_p.h" |
48 | #include "QtCore/qstring.h" |
49 | #include "QtCore/qvector.h" |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | enum TLDMatchType { |
54 | ExactMatch, |
55 | SuffixMatch, |
56 | ExceptionMatch, |
57 | }; |
58 | |
59 | static bool containsTLDEntry(QStringView entry, TLDMatchType match) |
60 | { |
61 | const QStringView matchSymbols[] = { |
62 | u"" , |
63 | u"*" , |
64 | u"!" , |
65 | }; |
66 | const auto symbol = matchSymbols[match]; |
67 | int index = qt_hash(entry, qt_hash(symbol)) % tldCount; |
68 | |
69 | // select the right chunk from the big table |
70 | short chunk = 0; |
71 | uint chunkIndex = tldIndices[index], offset = 0; |
72 | while (chunk < tldChunkCount && tldIndices[index] >= tldChunks[chunk]) { |
73 | chunkIndex -= tldChunks[chunk]; |
74 | offset += tldChunks[chunk]; |
75 | chunk++; |
76 | } |
77 | |
78 | // check all the entries from the given index |
79 | while (chunkIndex < tldIndices[index+1] - offset) { |
80 | const auto utf8 = tldData[chunk] + chunkIndex; |
81 | if ((symbol.isEmpty() || QLatin1Char(*utf8) == symbol) && entry == QString::fromUtf8(utf8 + symbol.size())) |
82 | return true; |
83 | chunkIndex += qstrlen(utf8) + 1; // +1 for the ending \0 |
84 | } |
85 | return false; |
86 | } |
87 | |
88 | /*! |
89 | \internal |
90 | |
91 | Return the top-level-domain per Qt's copy of the Mozilla public suffix list of |
92 | \a domain. |
93 | */ |
94 | |
95 | Q_CORE_EXPORT QString qTopLevelDomain(const QString &domain) |
96 | { |
97 | const QString domainLower = domain.toLower(); |
98 | QVector<QStringRef> sections = domainLower.splitRef(QLatin1Char('.'), QString::SkipEmptyParts); |
99 | if (sections.isEmpty()) |
100 | return QString(); |
101 | |
102 | QString level, tld; |
103 | for (int j = sections.count() - 1; j >= 0; --j) { |
104 | level.prepend(QLatin1Char('.') + sections.at(j)); |
105 | if (qIsEffectiveTLD(level.rightRef(level.size() - 1))) |
106 | tld = level; |
107 | } |
108 | return tld; |
109 | } |
110 | |
111 | /*! |
112 | \internal |
113 | |
114 | Return true if \a domain is a top-level-domain per Qt's copy of the Mozilla public suffix list. |
115 | */ |
116 | |
117 | Q_CORE_EXPORT bool qIsEffectiveTLD(const QStringRef &domain) |
118 | { |
119 | // for domain 'foo.bar.com': |
120 | // 1. return if TLD table contains 'foo.bar.com' |
121 | // 2. else if table contains '*.bar.com', |
122 | // 3. test that table does not contain '!foo.bar.com' |
123 | |
124 | if (containsTLDEntry(domain, ExactMatch)) // 1 |
125 | return true; |
126 | |
127 | const int dot = domain.indexOf(QLatin1Char('.')); |
128 | if (dot >= 0) { |
129 | if (containsTLDEntry(domain.mid(dot), SuffixMatch)) // 2 |
130 | return !containsTLDEntry(domain, ExceptionMatch); // 3 |
131 | } |
132 | return false; |
133 | } |
134 | |
135 | QT_END_NAMESPACE |
136 | |
137 | #endif |
138 | |