// Version for Qt < 4.8 has one issue, it will wrongly // count .co.uk (and others) as second-level domain static QString toSecondLevelDomain(const QUrl &url) { #if QT_VERSION >= 0x040800 const QString topLevelDomain = url.topLevelDomain(); const QString urlHost = url.host(); if (topLevelDomain.isEmpty() || urlHost.isEmpty()) { return QString(); } QString domain = urlHost.left(urlHost.size() - topLevelDomain.size()); if (domain.count(QL1C('.')) == 0) { return urlHost; } while (domain.count(QL1C('.')) != 0) { domain = domain.mid(domain.indexOf(QL1C('.')) + 1); } return domain + topLevelDomain; #else QString domain = url.host(); if (domain.count(QL1C('.')) == 0) { return QString(); } while (domain.count(QL1C('.')) != 1) { domain = domain.mid(domain.indexOf(QL1C('.')) + 1); } return domain; #endif }
static bool urlsShareSameDomain(const QUrl& url, const QUrl& firstPartyUrl) { QString firstPartyTLD = firstPartyUrl.topLevelDomain(); QString requestTLD = url.topLevelDomain(); if (firstPartyTLD != requestTLD) return false; QString firstPartyDomain = QString(firstPartyUrl.host().toLower()); QString requestDomain = QString(url.host().toLower()); removeTopLevelDomain(&firstPartyDomain, firstPartyTLD); removeTopLevelDomain(&requestDomain, requestTLD); if (firstPartyDomain.section(QLatin1Char('.'), -1) == requestDomain.section(QLatin1Char('.'), -1)) return true; return false; }
bool CookieJar::isDomainTheSame(const QUrl &first, const QUrl &second) { const QString firstTld(first.topLevelDomain()); const QString secondTld(second.topLevelDomain()); if (firstTld != secondTld) { return false; } QString firstDomain(QLatin1Char('.') + first.host().toLower()); firstDomain.remove((firstDomain.length() - firstTld.length()), firstTld.length()); QString secondDomain(QLatin1Char('.') + second.host().toLower()); secondDomain.remove((secondDomain.length() - secondTld.length()), secondTld.length()); if (firstDomain.section(QLatin1Char('.'), -1) == secondDomain.section(QLatin1Char('.'), -1)) { return true; } return false; }