This source file includes following definitions.
- matchesPatterns
- parse
- matches
- matchesHost
- matchesPath
#include "config.h"
#include "platform/URLPatternMatcher.h"
#include "platform/weborigin/KURL.h"
#include "wtf/StdLibExtras.h"
namespace WebCore {
bool URLPatternMatcher::matchesPatterns(const KURL& url, const Vector<String>& whitelist)
{
if (whitelist.isEmpty())
return true;
for (size_t i = 0; i < whitelist.size(); ++i) {
URLPatternMatcher contentPattern(whitelist[i]);
if (contentPattern.matches(url))
return true;
}
return false;
}
bool URLPatternMatcher::parse(const String& pattern)
{
DEFINE_STATIC_LOCAL(const String, schemeSeparator, ("://"));
size_t schemeEndPos = pattern.find(schemeSeparator);
if (schemeEndPos == kNotFound)
return false;
m_scheme = pattern.left(schemeEndPos);
unsigned hostStartPos = schemeEndPos + schemeSeparator.length();
if (hostStartPos >= pattern.length())
return false;
int pathStartPos = 0;
if (equalIgnoringCase(m_scheme, "file")) {
pathStartPos = hostStartPos;
} else {
size_t hostEndPos = pattern.find("/", hostStartPos);
if (hostEndPos == kNotFound)
return false;
m_host = pattern.substring(hostStartPos, hostEndPos - hostStartPos);
m_matchSubdomains = false;
if (m_host == "*") {
m_host = "";
m_matchSubdomains = true;
} else if (m_host.startsWith("*.")) {
m_host = m_host.substring(2);
m_matchSubdomains = true;
}
if (m_host.find("*") != kNotFound)
return false;
pathStartPos = hostEndPos;
}
m_path = pattern.right(pattern.length() - pathStartPos);
return true;
}
bool URLPatternMatcher::matches(const KURL& test) const
{
if (m_invalid)
return false;
if (!equalIgnoringCase(test.protocol(), m_scheme))
return false;
if (!equalIgnoringCase(m_scheme, "file") && !matchesHost(test))
return false;
return matchesPath(test);
}
bool URLPatternMatcher::matchesHost(const KURL& test) const
{
const String& host = test.host();
if (equalIgnoringCase(host, m_host))
return true;
if (!m_matchSubdomains)
return false;
if (!m_host.length())
return true;
if (!host.endsWith(m_host, false))
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 URLPatternMatcher::matchesPath(const KURL& test) const
{
MatchTester match(m_path, test.path());
return match.test();
}
}