35 #include <QNetworkReply>
36 #include <QStringList>
39 using namespace MessageViewer;
42 return filter.startsWith(QLatin1Char(
'/')) && filter.endsWith(QLatin1Char(
'/'));
48 , m_unsupported(false)
49 , m_thirdPartyOption(false)
50 , m_thirdPartyOptionReversed(false)
52 m_regExp.setCaseSensitivity(Qt::CaseInsensitive);
53 m_regExp.setPatternSyntax(QRegExp::RegExp2);
55 QString parsedLine = filter;
59 parsedLine = parsedLine.mid(1, parsedLine.length() - 2);
63 const int optionsNumber = parsedLine.lastIndexOf(QLatin1Char(
'$'));
65 if (optionsNumber >= 0)
67 QStringList options(parsedLine.mid(optionsNumber + 1).split(QLatin1Char(
',')));
68 parsedLine = parsedLine.left(optionsNumber);
70 if (options.removeOne(QLatin1String(
"match-case")))
71 m_regExp.setCaseSensitivity(Qt::CaseSensitive);
73 if (options.removeOne(QLatin1String(
"third-party")))
74 m_thirdPartyOption =
true;
76 if (options.removeOne(QLatin1String(
"~third-party")))
78 m_thirdPartyOption =
true;
79 m_thirdPartyOptionReversed =
true;
82 Q_FOREACH(
const QString & option, options)
85 const QString domainKeyword(QLatin1String(
"domain="));
86 if (option.startsWith(domainKeyword))
88 options.removeOne(option);
89 const QStringList domainList = option.mid(domainKeyword.length()).split(QLatin1Char(
'|'));
90 Q_FOREACH(
const QString & domain, domainList)
92 if (domain.startsWith(QLatin1Char(
'~')))
93 m_whiteDomains.insert(domain.toLower());
95 m_blackDomains.insert(domain.toLower());
103 m_unsupported = (!options.isEmpty());
106 parsedLine = convertPatternToRegExp(parsedLine);
109 m_regExp.setPattern(parsedLine);
118 if (m_thirdPartyOption)
120 const QString referer = QString::fromLatin1(request.rawHeader(
"referer"));
121 const QString host = request.url().host();
123 bool isThirdParty = !referer.contains(host);
125 if (!m_thirdPartyOptionReversed && !isThirdParty)
128 if (m_thirdPartyOptionReversed && isThirdParty)
132 const bool regexpMatch = m_regExp.indexIn(encodedUrl) != -1;
134 if (regexpMatch && (!m_whiteDomains.isEmpty() || !m_blackDomains.isEmpty()))
136 Q_ASSERT(qobject_cast<QWebFrame*>(request.originatingObject()));
137 const QWebFrame *
const origin =
static_cast<QWebFrame * const
>(request.originatingObject());
139 const QString originDomain = origin->url().host();
141 if (!m_whiteDomains.isEmpty())
144 if (m_whiteDomains.contains(originDomain))
148 else if (m_blackDomains.contains(originDomain))
158 QString AdBlockRuleFallbackImpl::convertPatternToRegExp(
const QString &wildcardPattern)
160 QString pattern = wildcardPattern;
163 pattern.replace(QRegExp(QLatin1String(
"\\*+")), QLatin1String(
"*"));
166 pattern.replace(QRegExp(QLatin1String(
"\\^\\|$")), QLatin1String(
"^"));
169 pattern.replace(QRegExp(QLatin1String(
"^(\\*)")), QLatin1String(
""));
172 pattern.replace(QRegExp(QLatin1String(
"(\\*)$")), QLatin1String(
""));
175 pattern.replace(QRegExp(QLatin1String(
"(\\W)")), QLatin1String(
"\\\\1"));
178 pattern.replace(QRegExp(QLatin1String(
"^\\\\\\|\\\\\\|")), QLatin1String(
"^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?"));
181 pattern.replace(QRegExp(QLatin1String(
"\\\\\\^")), QLatin1String(
"(?:[^\\w\\d\\-.%]|$)"));
184 pattern.replace(QRegExp(QLatin1String(
"^\\\\\\|")), QLatin1String(
"^"));
187 pattern.replace(QRegExp(QLatin1String(
"\\\\\\|$")), QLatin1String(
"$"));
190 pattern.replace(QRegExp(QLatin1String(
"\\\\\\*")), QLatin1String(
".*"));
199 return m_regExp.pattern();
205 return QLatin1String(
"AdBlockRuleFallbackImpl");
QString ruleString() const
bool match(const QNetworkRequest &request, const QString &encodedUrl, const QString &encodedUrlLowerCase) const
AdBlockRuleFallbackImpl(const QString &filter)
static bool isRegExpFilter(const QString &filter)