11#include " ParseDomain.h"
2-
2+ #if QT_VERSION >= 0x051000
3+ #include " utils/qurltlds_p.h"
4+ #endif
35
46ParseDomain::ParseDomain (const QString &url) :
57 _url(QUrl::fromUserInput(url))
@@ -60,6 +62,51 @@ ParseDomain::ParseDomain(const QString &url) :
6062 }
6163}
6264
65+ #if QT_VERSION >= 0x051000
66+ bool ParseDomain::containsTLDEntry (QStringView entry, TLDMatchType match)
67+ {
68+ const QStringView matchSymbols[] = {
69+ u" " ,
70+ u" *" ,
71+ u" !" ,
72+ };
73+ const auto symbol = matchSymbols[match];
74+ int index = qt_hash (entry, qt_hash (symbol)) % tldCount;
75+ // select the right chunk from the big table
76+ short chunk = 0 ;
77+ uint chunkIndex = tldIndices[index], offset = 0 ;
78+ while (chunk < tldChunkCount && tldIndices[index] >= tldChunks[chunk]) {
79+ chunkIndex -= tldChunks[chunk];
80+ offset += tldChunks[chunk];
81+ chunk++;
82+ }
83+ // check all the entries from the given index
84+ while (chunkIndex < tldIndices[index+1 ] - offset) {
85+ const auto utf8 = tldData[chunk] + chunkIndex;
86+ if ((symbol.isEmpty () || QLatin1Char (*utf8) == symbol) && entry == QString::fromUtf8 (utf8 + symbol.size ()))
87+ return true ;
88+ chunkIndex += qstrlen (utf8) + 1 ; // +1 for the ending \0
89+ }
90+ return false ;
91+ }
92+
93+ bool ParseDomain::qIsEffectiveTLD (const QString &domain)
94+ {
95+ // for domain 'foo.bar.com':
96+ // 1. return if TLD table contains 'foo.bar.com'
97+ // 2. else if table contains '*.bar.com',
98+ // 3. test that table does not contain '!foo.bar.com'
99+ if (containsTLDEntry (domain, ExactMatch)) // 1
100+ return true ;
101+ const int dot = domain.indexOf (QLatin1Char (' .' ));
102+ if (dot >= 0 ) {
103+ if (containsTLDEntry (domain.mid (dot), SuffixMatch)) // 2
104+ return !containsTLDEntry (domain, ExceptionMatch); // 3
105+ }
106+ return false ;
107+ }
108+ #endif
109+
63110QString ParseDomain::getManuallyEnteredDomainName (const QString &service)
64111{
65112 if (!isWebsite ())
@@ -83,11 +130,20 @@ QString ParseDomain::getManuallyEnteredDomainName(const QString &service)
83130 */
84131QString ParseDomain::getTopLevel () const
85132{
86- QString host = _url.host ();
87- int pos = host.lastIndexOf (' .' );
88- if (-1 != pos)
89- {
90- return host.right (host.size () - pos);
91- }
92- return " " ;
133+ #if QT_VERSION >= 0x051000
134+ QString domain = _url.host ();
135+ const QString domainLower = domain.toLower ();
136+ QStringList sections = domainLower.split (QLatin1Char (' .' ));
137+ if (sections.isEmpty ())
138+ return QString ();
139+ QString level, tld;
140+ for (int j = sections.count () - 1 ; j >= 0 ; --j) {
141+ level.prepend (QLatin1Char (' .' ) + sections.at (j));
142+ if (qIsEffectiveTLD (level.right (level.size () - 1 )))
143+ tld = level;
144+ }
145+ return tld;
146+ #else
147+ return _url.topLevelDomain ();
148+ #endif
93149}
0 commit comments