Update V8 to version 4.6.55.
[chromium-blink-merge.git] / content / browser / appcache / appcache_manifest_parser.cc
blobbf936c23ebcbe4873b5998af9434561a6d706d51
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This is a port of ManifestParser.cc from WebKit/WebCore/loader/appcache.
7 /*
8 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "content/browser/appcache/appcache_manifest_parser.h"
34 #include "base/command_line.h"
35 #include "base/i18n/icu_string_conversions.h"
36 #include "base/logging.h"
37 #include "base/strings/utf_string_conversions.h"
38 #include "url/gurl.h"
40 namespace content {
42 namespace {
44 // Helper function used to identify 'isPattern' annotations.
45 bool HasPatternMatchingAnnotation(const wchar_t* line_p,
46 const wchar_t* line_end) {
47 // Skip whitespace separating the resource url from the annotation.
48 // Note: trailing whitespace has already been trimmed from the line.
49 while (line_p < line_end && (*line_p == '\t' || *line_p == ' '))
50 ++line_p;
51 if (line_p == line_end)
52 return false;
53 std::wstring annotation(line_p, line_end - line_p);
54 return annotation == L"isPattern";
59 enum Mode {
60 EXPLICIT,
61 INTERCEPT,
62 FALLBACK,
63 ONLINE_WHITELIST,
64 UNKNOWN_MODE,
67 enum InterceptVerb {
68 RETURN,
69 EXECUTE,
70 UNKNOWN_VERB,
73 AppCacheManifest::AppCacheManifest()
74 : online_whitelist_all(false),
75 did_ignore_intercept_namespaces(false) {
78 AppCacheManifest::~AppCacheManifest() {}
80 bool ParseManifest(const GURL& manifest_url, const char* data, int length,
81 ParseMode parse_mode, AppCacheManifest& manifest) {
82 // This is an implementation of the parsing algorithm specified in
83 // the HTML5 offline web application docs:
84 // http://www.w3.org/TR/html5/offline.html
85 // Do not modify it without consulting those docs.
86 // Though you might be tempted to convert these wstrings to UTF-8 or
87 // base::string16, this implementation seems simpler given the constraints.
89 const wchar_t kSignature[] = L"CACHE MANIFEST";
90 const size_t kSignatureLength = arraysize(kSignature) - 1;
91 const wchar_t kChromiumSignature[] = L"CHROMIUM CACHE MANIFEST";
92 const size_t kChromiumSignatureLength = arraysize(kChromiumSignature) - 1;
94 DCHECK(manifest.explicit_urls.empty());
95 DCHECK(manifest.fallback_namespaces.empty());
96 DCHECK(manifest.online_whitelist_namespaces.empty());
97 DCHECK(!manifest.online_whitelist_all);
98 DCHECK(!manifest.did_ignore_intercept_namespaces);
100 Mode mode = EXPLICIT;
102 std::wstring data_string;
103 base::UTF8ToWide(data, length, &data_string);
104 const wchar_t* p = data_string.c_str();
105 const wchar_t* end = p + data_string.length();
107 // Look for the magic signature: "^\xFEFF?CACHE MANIFEST[ \t]?"
108 // Example: "CACHE MANIFEST #comment" is a valid signature.
109 // Example: "CACHE MANIFEST;V2" is not.
111 // When the input data starts with a UTF-8 Byte-Order-Mark
112 // (0xEF, 0xBB, 0xBF), the UTF8ToWide() function converts it to a
113 // Unicode BOM (U+FEFF). Skip a converted Unicode BOM if it exists.
114 int bom_offset = 0;
115 if (!data_string.empty() && data_string[0] == 0xFEFF) {
116 bom_offset = 1;
117 ++p;
120 if (p >= end)
121 return false;
123 // Check for a supported signature and skip p past it.
124 if (0 == data_string.compare(bom_offset, kSignatureLength,
125 kSignature)) {
126 p += kSignatureLength;
127 } else if (0 == data_string.compare(bom_offset, kChromiumSignatureLength,
128 kChromiumSignature)) {
129 p += kChromiumSignatureLength;
130 } else {
131 return false;
134 // Character after "CACHE MANIFEST" must be whitespace.
135 if (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
136 return false;
138 // Skip to the end of the line.
139 while (p < end && *p != '\r' && *p != '\n')
140 ++p;
142 while (1) {
143 // Skip whitespace
144 while (p < end && (*p == '\n' || *p == '\r' || *p == ' ' || *p == '\t'))
145 ++p;
147 if (p == end)
148 break;
150 const wchar_t* line_start = p;
152 // Find the end of the line
153 while (p < end && *p != '\r' && *p != '\n')
154 ++p;
156 // Check if we have a comment
157 if (*line_start == '#')
158 continue;
160 // Get rid of trailing whitespace
161 const wchar_t* tmp = p - 1;
162 while (tmp > line_start && (*tmp == ' ' || *tmp == '\t'))
163 --tmp;
165 std::wstring line(line_start, tmp - line_start + 1);
167 if (line == L"CACHE:") {
168 mode = EXPLICIT;
169 } else if (line == L"FALLBACK:") {
170 mode = FALLBACK;
171 } else if (line == L"NETWORK:") {
172 mode = ONLINE_WHITELIST;
173 } else if (line == L"CHROMIUM-INTERCEPT:") {
174 mode = INTERCEPT;
175 } else if (*(line.end() - 1) == ':') {
176 mode = UNKNOWN_MODE;
177 } else if (mode == UNKNOWN_MODE) {
178 continue;
179 } else if (line == L"*" && mode == ONLINE_WHITELIST) {
180 manifest.online_whitelist_all = true;
181 continue;
182 } else if (mode == EXPLICIT || mode == ONLINE_WHITELIST) {
183 const wchar_t *line_p = line.c_str();
184 const wchar_t *line_end = line_p + line.length();
186 // Look for whitespace separating the URL from subsequent ignored tokens.
187 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
188 ++line_p;
190 base::string16 url16;
191 base::WideToUTF16(line.c_str(), line_p - line.c_str(), &url16);
192 GURL url = manifest_url.Resolve(url16);
193 if (!url.is_valid())
194 continue;
195 if (url.has_ref()) {
196 GURL::Replacements replacements;
197 replacements.ClearRef();
198 url = url.ReplaceComponents(replacements);
201 // Scheme component must be the same as the manifest URL's.
202 if (url.scheme() != manifest_url.scheme()) {
203 continue;
206 // See http://code.google.com/p/chromium/issues/detail?id=69594
207 // We willfully violate the HTML5 spec at this point in order
208 // to support the appcaching of cross-origin HTTPS resources.
209 // Per the spec, EXPLICIT cross-origin HTTS resources should be
210 // ignored here. We've opted for a milder constraint and allow
211 // caching unless the resource has a "no-store" header. That
212 // condition is enforced in AppCacheUpdateJob.
214 if (mode == EXPLICIT) {
215 manifest.explicit_urls.insert(url.spec());
216 } else {
217 bool is_pattern = HasPatternMatchingAnnotation(line_p, line_end);
218 manifest.online_whitelist_namespaces.push_back(
219 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, url, GURL(),
220 is_pattern));
222 } else if (mode == INTERCEPT) {
223 if (parse_mode != PARSE_MANIFEST_ALLOWING_INTERCEPTS) {
224 manifest.did_ignore_intercept_namespaces = true;
225 continue;
228 // Lines of the form,
229 // <urlnamespace> <intercept_type> <targeturl>
230 const wchar_t* line_p = line.c_str();
231 const wchar_t* line_end = line_p + line.length();
233 // Look for first whitespace separating the url namespace from
234 // the intercept type.
235 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
236 ++line_p;
238 if (line_p == line_end)
239 continue; // There was no whitespace separating the URLs.
241 base::string16 namespace_url16;
242 base::WideToUTF16(line.c_str(), line_p - line.c_str(), &namespace_url16);
243 GURL namespace_url = manifest_url.Resolve(namespace_url16);
244 if (!namespace_url.is_valid())
245 continue;
246 if (namespace_url.has_ref()) {
247 GURL::Replacements replacements;
248 replacements.ClearRef();
249 namespace_url = namespace_url.ReplaceComponents(replacements);
252 // The namespace URL must have the same scheme, host and port
253 // as the manifest's URL.
254 if (manifest_url.GetOrigin() != namespace_url.GetOrigin())
255 continue;
257 // Skip whitespace separating namespace from the type.
258 while (line_p < line_end && (*line_p == '\t' || *line_p == ' '))
259 ++line_p;
261 // Look for whitespace separating the type from the target url.
262 const wchar_t* type_start = line_p;
263 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
264 ++line_p;
266 // Look for a type value we understand, otherwise skip the line.
267 InterceptVerb verb = UNKNOWN_VERB;
268 std::wstring type(type_start, line_p - type_start);
269 if (type == L"return") {
270 verb = RETURN;
271 } else if (type == L"execute" &&
272 base::CommandLine::ForCurrentProcess()->HasSwitch(
273 kEnableExecutableHandlers)) {
274 verb = EXECUTE;
276 if (verb == UNKNOWN_VERB)
277 continue;
279 // Skip whitespace separating type from the target_url.
280 while (line_p < line_end && (*line_p == '\t' || *line_p == ' '))
281 ++line_p;
283 // Look for whitespace separating the URL from subsequent ignored tokens.
284 const wchar_t* target_url_start = line_p;
285 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
286 ++line_p;
288 base::string16 target_url16;
289 base::WideToUTF16(target_url_start, line_p - target_url_start,
290 &target_url16);
291 GURL target_url = manifest_url.Resolve(target_url16);
292 if (!target_url.is_valid())
293 continue;
295 if (target_url.has_ref()) {
296 GURL::Replacements replacements;
297 replacements.ClearRef();
298 target_url = target_url.ReplaceComponents(replacements);
300 if (manifest_url.GetOrigin() != target_url.GetOrigin())
301 continue;
303 bool is_pattern = HasPatternMatchingAnnotation(line_p, line_end);
304 manifest.intercept_namespaces.push_back(
305 AppCacheNamespace(APPCACHE_INTERCEPT_NAMESPACE, namespace_url,
306 target_url, is_pattern, verb == EXECUTE));
307 } else if (mode == FALLBACK) {
308 const wchar_t* line_p = line.c_str();
309 const wchar_t* line_end = line_p + line.length();
311 // Look for whitespace separating the two URLs
312 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
313 ++line_p;
315 if (line_p == line_end) {
316 // There was no whitespace separating the URLs.
317 continue;
320 base::string16 namespace_url16;
321 base::WideToUTF16(line.c_str(), line_p - line.c_str(), &namespace_url16);
322 GURL namespace_url = manifest_url.Resolve(namespace_url16);
323 if (!namespace_url.is_valid())
324 continue;
325 if (namespace_url.has_ref()) {
326 GURL::Replacements replacements;
327 replacements.ClearRef();
328 namespace_url = namespace_url.ReplaceComponents(replacements);
331 // Fallback namespace URL must have the same scheme, host and port
332 // as the manifest's URL.
333 if (manifest_url.GetOrigin() != namespace_url.GetOrigin()) {
334 continue;
337 // Skip whitespace separating fallback namespace from URL.
338 while (line_p < line_end && (*line_p == '\t' || *line_p == ' '))
339 ++line_p;
341 // Look for whitespace separating the URL from subsequent ignored tokens.
342 const wchar_t* fallback_start = line_p;
343 while (line_p < line_end && *line_p != '\t' && *line_p != ' ')
344 ++line_p;
346 base::string16 fallback_url16;
347 base::WideToUTF16(fallback_start, line_p - fallback_start,
348 &fallback_url16);
349 GURL fallback_url = manifest_url.Resolve(fallback_url16);
350 if (!fallback_url.is_valid())
351 continue;
352 if (fallback_url.has_ref()) {
353 GURL::Replacements replacements;
354 replacements.ClearRef();
355 fallback_url = fallback_url.ReplaceComponents(replacements);
358 // Fallback entry URL must have the same scheme, host and port
359 // as the manifest's URL.
360 if (manifest_url.GetOrigin() != fallback_url.GetOrigin()) {
361 continue;
364 bool is_pattern = HasPatternMatchingAnnotation(line_p, line_end);
366 // Store regardless of duplicate namespace URL. Only first match
367 // will ever be used.
368 manifest.fallback_namespaces.push_back(
369 AppCacheNamespace(APPCACHE_FALLBACK_NAMESPACE, namespace_url,
370 fallback_url, is_pattern));
371 } else {
372 NOTREACHED();
376 return true;
379 } // namespace content