2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "UserContentURLPattern.h"
29 #include <wtf/StdLibExtras.h>
33 bool UserContentURLPattern::matchesPatterns(const KURL& url, const Vector<String>* whitelist, const Vector<String>* blacklist)
35 // In order for a URL to be a match it has to be present in the whitelist and not present in the blacklist.
36 // If there is no whitelist at all, then all URLs are assumed to be in the whitelist.
37 bool matchesWhitelist = !whitelist || whitelist->isEmpty();
38 if (!matchesWhitelist) {
39 for (unsigned i = 0; i < whitelist->size(); ++i) {
40 UserContentURLPattern contentPattern(whitelist->at(i));
41 if (contentPattern.matches(url)) {
42 matchesWhitelist = true;
48 bool matchesBlacklist = false;
50 for (unsigned i = 0; i < blacklist->size(); ++i) {
51 UserContentURLPattern contentPattern(blacklist->at(i));
52 if (contentPattern.matches(url)) {
53 matchesBlacklist = true;
59 return matchesWhitelist && !matchesBlacklist;
62 bool UserContentURLPattern::parse(const String& pattern)
64 DEFINE_STATIC_LOCAL(const String, schemeSeparator, ("://"));
66 size_t schemeEndPos = pattern.find(schemeSeparator);
67 if (schemeEndPos == notFound)
70 m_scheme = pattern.left(schemeEndPos);
72 unsigned hostStartPos = schemeEndPos + schemeSeparator.length();
73 if (hostStartPos >= pattern.length())
78 if (equalIgnoringCase(m_scheme, "file"))
79 pathStartPos = hostStartPos;
81 size_t hostEndPos = pattern.find("/", hostStartPos);
82 if (hostEndPos == notFound)
85 m_host = pattern.substring(hostStartPos, hostEndPos - hostStartPos);
86 m_matchSubdomains = false;
89 // The pattern can be just '*', which means match all domains.
91 m_matchSubdomains = true;
92 } else if (m_host.startsWith("*.")) {
93 // The first component can be '*', which means to match all subdomains.
94 m_host = m_host.substring(2); // Length of "*."
95 m_matchSubdomains = true;
98 // No other '*' can occur in the host.
99 if (m_host.find("*") != notFound)
102 pathStartPos = hostEndPos;
105 m_path = pattern.right(pattern.length() - pathStartPos);
110 bool UserContentURLPattern::matches(const KURL& test) const
115 if (!equalIgnoringCase(test.protocol(), m_scheme))
118 if (!equalIgnoringCase(m_scheme, "file") && !matchesHost(test))
121 return matchesPath(test);
124 bool UserContentURLPattern::matchesHost(const KURL& test) const
126 const String& host = test.host();
127 if (equalIgnoringCase(host, m_host))
130 if (!m_matchSubdomains)
133 // If we're matching subdomains, and we have no host, that means the pattern
134 // was <scheme>://*/<whatever>, so we match anything.
135 if (!m_host.length())
138 // Check if the domain is a subdomain of our host.
139 if (!host.endsWith(m_host, false))
142 ASSERT(host.length() > m_host.length());
144 // Check that the character before the suffix is a period.
145 return host[host.length() - m_host.length() - 1] == '.';
150 const String m_pattern;
151 unsigned m_patternIndex;
154 unsigned m_testIndex;
156 MatchTester(const String& pattern, const String& test)
164 bool testStringFinished() const { return m_testIndex >= m_test.length(); }
165 bool patternStringFinished() const { return m_patternIndex >= m_pattern.length(); }
169 while (!patternStringFinished()) {
170 if (m_pattern[m_patternIndex] != '*')
178 while (!patternStringFinished() && !testStringFinished()) {
179 if (m_pattern[m_patternIndex] == '*')
181 if (m_pattern[m_patternIndex] != m_test[m_testIndex])
190 // Eat all the matching chars.
193 // If the string is finished, then the pattern must be empty too, or contains
195 if (testStringFinished()) {
197 if (patternStringFinished())
202 // Pattern is empty but not string, this is not a match.
203 if (patternStringFinished())
206 // If we don't encounter a *, then we're hosed.
207 if (m_pattern[m_patternIndex] != '*')
210 while (!testStringFinished()) {
211 MatchTester nextMatch(*this);
212 nextMatch.m_patternIndex++;
213 if (nextMatch.test())
218 // We reached the end of the string. Let's see if the pattern contains only
221 return patternStringFinished();
225 bool UserContentURLPattern::matchesPath(const KURL& test) const
227 MatchTester match(m_path, test.path());
231 } // namespace WebCore