2 * Copyright (C) 2008 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 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 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 "AccessItemRule.h"
29 #include "PlatformString.h"
30 #include "ParserUtilities.h"
35 AccessItemRule::AccessItemRule(const String& rule)
37 parseAccessItemRule(rule);
40 static inline bool skipLWS(const UChar*& ptr, const UChar* end)
42 // LWS as defined by RFC 2616:
43 // LWS = [CRLF] 1*( SP | HT )
45 if (ptr + 1 < end && *ptr == '\r' && *(ptr + 1) == '\n')
48 const UChar* start = ptr;
49 while (ptr < end && (*ptr == ' ' || *ptr == '\t'))
54 void AccessItemRule::parseAccessItemRule(const String& rule)
56 // Parse the rule according to Section 4.2 (Access-Control HTTP Response Header) of the
57 // Access Control for Cross-site Requests spec.
58 // W3C Working Draft 14 February 2008
60 // Access-Control = "Access-Control" ":" 1#rule
61 // rule = "allow" 1*(LWS pattern) [LWS "exclude" 1*(LWS pattern)]
62 // pattern = "<" access item ">"
67 const UChar* ptr = rule.characters();
68 const UChar* end = ptr + rule.length();
70 // Skip leading whitespace
75 if (!skipString(ptr, end, "allow"))
78 parsePatternList(ptr, end, m_allowList);
79 if (m_allowList.isEmpty())
82 if (!skipString(ptr, end, "exclude")) {
88 parsePatternList(ptr, end, m_excludeList);
89 if (m_excludeList.isEmpty()) {
98 void AccessItemRule::parsePatternList(const UChar*& ptr, const UChar* end, Vector<AccessItem>& list)
101 if (!skipLWS(ptr, end) || ptr == end) {
111 bool sawEndTag = false;
112 const UChar* start = ptr;
125 AccessItem accessItem(String(start, ptr - start));
126 if (!accessItem.isValid()) {
131 list.append(accessItem);
136 void AccessItemRule::invalidate()
139 m_excludeList.clear();
143 void AccessItemRule::show()
145 printf(" AccessItemRule::show\n");
147 printf(" AllowList count: %d\n", static_cast<int>(m_allowList.size()));
148 for (size_t i = 0; i < m_allowList.size(); ++i)
149 m_allowList[i].show();
151 printf(" ExludeList count: %d\n", static_cast<int>(m_excludeList.size()));
152 for (size_t i = 0; i < m_excludeList.size(); ++i)
153 m_excludeList[i].show();
157 } // namespace WebCore