UserContentURLPattern.cpp [plain text]
#include "config.h"
#include "UserContentURLPattern.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/URL.h>
namespace WebCore {
bool UserContentURLPattern::matchesPatterns(const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist)
{
bool matchesWhitelist = whitelist.isEmpty();
if (!matchesWhitelist) {
for (auto& entry : whitelist) {
UserContentURLPattern contentPattern(entry);
if (contentPattern.matches(url)) {
matchesWhitelist = true;
break;
}
}
}
bool matchesBlacklist = false;
if (!blacklist.isEmpty()) {
for (auto& entry : blacklist) {
UserContentURLPattern contentPattern(entry);
if (contentPattern.matches(url)) {
matchesBlacklist = true;
break;
}
}
}
return matchesWhitelist && !matchesBlacklist;
}
bool UserContentURLPattern::parse(const String& pattern)
{
static NeverDestroyed<const String> schemeSeparator(MAKE_STATIC_STRING_IMPL("://"));
size_t schemeEndPos = pattern.find(schemeSeparator);
if (schemeEndPos == notFound)
return false;
m_scheme = pattern.left(schemeEndPos);
unsigned hostStartPos = schemeEndPos + schemeSeparator.get().length();
if (hostStartPos >= pattern.length())
return false;
int pathStartPos = 0;
if (equalLettersIgnoringASCIICase(m_scheme, "file"))
pathStartPos = hostStartPos;
else {
size_t hostEndPos = pattern.find('/', hostStartPos);
if (hostEndPos == notFound)
return false;
m_host = pattern.substring(hostStartPos, hostEndPos - hostStartPos);
m_matchSubdomains = false;
if (m_host == "*") {
m_host = emptyString();
m_matchSubdomains = true;
} else if (m_host.startsWith("*.")) {
m_host = m_host.substring(2); m_matchSubdomains = true;
}
if (m_host.find('*') != notFound)
return false;
pathStartPos = hostEndPos;
}
m_path = pattern.right(pattern.length() - pathStartPos);
return true;
}
bool UserContentURLPattern::matches(const URL& test) const
{
if (m_invalid)
return false;
if (!equalIgnoringASCIICase(test.protocol(), m_scheme))
return false;
if (!equalLettersIgnoringASCIICase(m_scheme, "file") && !matchesHost(test))
return false;
return matchesPath(test);
}
bool UserContentURLPattern::matchesHost(const URL& test) const
{
auto host = test.host();
if (equalIgnoringASCIICase(host, m_host))
return true;
if (!m_matchSubdomains)
return false;
if (!m_host.length())
return true;
if (!host.endsWithIgnoringASCIICase(m_host))
return false;
ASSERT(host.length() > m_host.length());
return host[host.length() - m_host.length() - 1] == '.';
}
struct MatchTester
{
const String m_pattern;
unsigned m_patternIndex;
const String m_test;
unsigned m_testIndex;
MatchTester(const String& pattern, const String& test)
: m_pattern(pattern)
, m_patternIndex(0)
, m_test(test)
, m_testIndex(0)
{
}
bool testStringFinished() const { return m_testIndex >= m_test.length(); }
bool patternStringFinished() const { return m_patternIndex >= m_pattern.length(); }
void eatWildcard()
{
while (!patternStringFinished()) {
if (m_pattern[m_patternIndex] != '*')
return;
m_patternIndex++;
}
}
void eatSameChars()
{
while (!patternStringFinished() && !testStringFinished()) {
if (m_pattern[m_patternIndex] == '*')
return;
if (m_pattern[m_patternIndex] != m_test[m_testIndex])
return;
m_patternIndex++;
m_testIndex++;
}
}
bool test()
{
eatSameChars();
if (testStringFinished()) {
eatWildcard();
if (patternStringFinished())
return true;
return false;
}
if (patternStringFinished())
return false;
if (m_pattern[m_patternIndex] != '*')
return false;
while (!testStringFinished()) {
MatchTester nextMatch(*this);
nextMatch.m_patternIndex++;
if (nextMatch.test())
return true;
m_testIndex++;
}
eatWildcard();
return patternStringFinished();
}
};
bool UserContentURLPattern::matchesPath(const URL& test) const
{
MatchTester match(m_path, test.path());
return match.test();
}
}