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.
5 // This is a port of ManifestParser.cc from WebKit/WebCore/loader/appcache.
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
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"
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
== ' '))
51 if (line_p
== line_end
)
53 std::wstring
annotation(line_p
, line_end
- line_p
);
54 return annotation
== L
"isPattern";
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.
115 if (!data_string
.empty() && data_string
[0] == 0xFEFF) {
123 // Check for a supported signature and skip p past it.
124 if (0 == data_string
.compare(bom_offset
, kSignatureLength
,
126 p
+= kSignatureLength
;
127 } else if (0 == data_string
.compare(bom_offset
, kChromiumSignatureLength
,
128 kChromiumSignature
)) {
129 p
+= kChromiumSignatureLength
;
134 // Character after "CACHE MANIFEST" must be whitespace.
135 if (p
< end
&& *p
!= ' ' && *p
!= '\t' && *p
!= '\n' && *p
!= '\r')
138 // Skip to the end of the line.
139 while (p
< end
&& *p
!= '\r' && *p
!= '\n')
144 while (p
< end
&& (*p
== '\n' || *p
== '\r' || *p
== ' ' || *p
== '\t'))
150 const wchar_t* line_start
= p
;
152 // Find the end of the line
153 while (p
< end
&& *p
!= '\r' && *p
!= '\n')
156 // Check if we have a comment
157 if (*line_start
== '#')
160 // Get rid of trailing whitespace
161 const wchar_t* tmp
= p
- 1;
162 while (tmp
> line_start
&& (*tmp
== ' ' || *tmp
== '\t'))
165 std::wstring
line(line_start
, tmp
- line_start
+ 1);
167 if (line
== L
"CACHE:") {
169 } else if (line
== L
"FALLBACK:") {
171 } else if (line
== L
"NETWORK:") {
172 mode
= ONLINE_WHITELIST
;
173 } else if (line
== L
"CHROMIUM-INTERCEPT:") {
175 } else if (*(line
.end() - 1) == ':') {
177 } else if (mode
== UNKNOWN_MODE
) {
179 } else if (line
== L
"*" && mode
== ONLINE_WHITELIST
) {
180 manifest
.online_whitelist_all
= true;
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
!= ' ')
190 base::string16 url16
;
191 base::WideToUTF16(line
.c_str(), line_p
- line
.c_str(), &url16
);
192 GURL url
= manifest_url
.Resolve(url16
);
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()) {
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());
217 bool is_pattern
= HasPatternMatchingAnnotation(line_p
, line_end
);
218 manifest
.online_whitelist_namespaces
.push_back(
219 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE
, url
, GURL(),
222 } else if (mode
== INTERCEPT
) {
223 if (parse_mode
!= PARSE_MANIFEST_ALLOWING_INTERCEPTS
) {
224 manifest
.did_ignore_intercept_namespaces
= true;
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
!= ' ')
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())
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())
257 // Skip whitespace separating namespace from the type.
258 while (line_p
< line_end
&& (*line_p
== '\t' || *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
!= ' ')
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") {
271 } else if (type
== L
"execute" &&
272 base::CommandLine::ForCurrentProcess()->HasSwitch(
273 kEnableExecutableHandlers
)) {
276 if (verb
== UNKNOWN_VERB
)
279 // Skip whitespace separating type from the target_url.
280 while (line_p
< line_end
&& (*line_p
== '\t' || *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
!= ' ')
288 base::string16 target_url16
;
289 base::WideToUTF16(target_url_start
, line_p
- target_url_start
,
291 GURL target_url
= manifest_url
.Resolve(target_url16
);
292 if (!target_url
.is_valid())
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())
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
!= ' ')
315 if (line_p
== line_end
) {
316 // There was no whitespace separating the URLs.
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())
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()) {
337 // Skip whitespace separating fallback namespace from URL.
338 while (line_p
< line_end
&& (*line_p
== '\t' || *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
!= ' ')
346 base::string16 fallback_url16
;
347 base::WideToUTF16(fallback_start
, line_p
- fallback_start
,
349 GURL fallback_url
= manifest_url
.Resolve(fallback_url16
);
350 if (!fallback_url
.is_valid())
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()) {
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
));
379 } // namespace content