1 // Copyright (c) 2011 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 "webkit/appcache/manifest_parser.h"
34 #include "base/i18n/icu_string_conversions.h"
35 #include "base/logging.h"
36 #include "base/utf_string_conversions.h"
37 #include "googleurl/src/gurl.h"
43 // Helper function used to identify 'isPattern' annotations.
44 bool HasPatternMatchingAnnotation(const wchar_t* line_p
,
45 const wchar_t* line_end
) {
46 // Skip whitespace separating the resource url from the annotation.
47 // Note: trailing whitespace has already been trimmed from the line.
48 while (line_p
< line_end
&& (*line_p
== '\t' || *line_p
== ' '))
50 if (line_p
== line_end
)
52 std::wstring
annotation(line_p
, line_end
- line_p
);
53 return annotation
== L
"isPattern";
66 Manifest::Manifest() : online_whitelist_all(false) {}
68 Manifest::~Manifest() {}
70 bool ParseManifest(const GURL
& manifest_url
, const char* data
, int length
,
72 // This is an implementation of the parsing algorithm specified in
73 // the HTML5 offline web application docs:
74 // http://www.w3.org/TR/html5/offline.html
75 // Do not modify it without consulting those docs.
76 // Though you might be tempted to convert these wstrings to UTF-8 or
77 // base::string16, this implementation seems simpler given the constraints.
79 const wchar_t kSignature
[] = L
"CACHE MANIFEST";
80 const size_t kSignatureLength
= arraysize(kSignature
) - 1;
81 const wchar_t kChromiumSignature
[] = L
"CHROMIUM CACHE MANIFEST";
82 const size_t kChromiumSignatureLength
= arraysize(kChromiumSignature
) - 1;
84 DCHECK(manifest
.explicit_urls
.empty());
85 DCHECK(manifest
.fallback_namespaces
.empty());
86 DCHECK(manifest
.online_whitelist_namespaces
.empty());
87 DCHECK(!manifest
.online_whitelist_all
);
91 std::wstring data_string
;
92 // TODO(jennb): cannot do UTF8ToWide(data, length, &data_string);
93 // until UTF8ToWide uses 0xFFFD Unicode replacement character.
94 base::CodepageToWide(std::string(data
, length
), base::kCodepageUTF8
,
95 base::OnStringConversionError::SUBSTITUTE
, &data_string
);
96 const wchar_t* p
= data_string
.c_str();
97 const wchar_t* end
= p
+ data_string
.length();
99 // Look for the magic signature: "^\xFEFF?CACHE MANIFEST[ \t]?"
100 // Example: "CACHE MANIFEST #comment" is a valid signature.
101 // Example: "CACHE MANIFEST;V2" is not.
103 // When the input data starts with a UTF-8 Byte-Order-Mark
104 // (0xEF, 0xBB, 0xBF), the UTF8ToWide() function converts it to a
105 // Unicode BOM (U+FEFF). Skip a converted Unicode BOM if it exists.
107 if (!data_string
.empty() && data_string
[0] == 0xFEFF) {
115 // Check for a supported signature and skip p past it.
116 if (0 == data_string
.compare(bom_offset
, kSignatureLength
,
118 p
+= kSignatureLength
;
119 } else if (0 == data_string
.compare(bom_offset
, kChromiumSignatureLength
,
120 kChromiumSignature
)) {
121 p
+= kChromiumSignatureLength
;
126 // Character after "CACHE MANIFEST" must be whitespace.
127 if (p
< end
&& *p
!= ' ' && *p
!= '\t' && *p
!= '\n' && *p
!= '\r')
130 // Skip to the end of the line.
131 while (p
< end
&& *p
!= '\r' && *p
!= '\n')
136 while (p
< end
&& (*p
== '\n' || *p
== '\r' || *p
== ' ' || *p
== '\t'))
142 const wchar_t* line_start
= p
;
144 // Find the end of the line
145 while (p
< end
&& *p
!= '\r' && *p
!= '\n')
148 // Check if we have a comment
149 if (*line_start
== '#')
152 // Get rid of trailing whitespace
153 const wchar_t* tmp
= p
- 1;
154 while (tmp
> line_start
&& (*tmp
== ' ' || *tmp
== '\t'))
157 std::wstring
line(line_start
, tmp
- line_start
+ 1);
159 if (line
== L
"CACHE:") {
161 } else if (line
== L
"FALLBACK:") {
163 } else if (line
== L
"NETWORK:") {
164 mode
= ONLINE_WHITELIST
;
165 } else if (line
== L
"CHROMIUM-INTERCEPT:") {
167 } else if (*(line
.end() - 1) == ':') {
169 } else if (mode
== UNKNOWN
) {
171 } else if (line
== L
"*" && mode
== ONLINE_WHITELIST
) {
172 manifest
.online_whitelist_all
= true;
174 } else if (mode
== EXPLICIT
|| mode
== ONLINE_WHITELIST
) {
175 const wchar_t *line_p
= line
.c_str();
176 const wchar_t *line_end
= line_p
+ line
.length();
178 // Look for whitespace separating the URL from subsequent ignored tokens.
179 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
182 base::string16 url16
;
183 WideToUTF16(line
.c_str(), line_p
- line
.c_str(), &url16
);
184 GURL url
= manifest_url
.Resolve(url16
);
188 GURL::Replacements replacements
;
189 replacements
.ClearRef();
190 url
= url
.ReplaceComponents(replacements
);
193 // Scheme component must be the same as the manifest URL's.
194 if (url
.scheme() != manifest_url
.scheme()) {
198 // See http://code.google.com/p/chromium/issues/detail?id=69594
199 // We willfully violate the HTML5 spec at this point in order
200 // to support the appcaching of cross-origin HTTPS resources.
201 // Per the spec, EXPLICIT cross-origin HTTS resources should be
202 // ignored here. We've opted for a milder constraint and allow
203 // caching unless the resource has a "no-store" header. That
204 // condition is enforced in AppCacheUpdateJob.
206 if (mode
== EXPLICIT
) {
207 manifest
.explicit_urls
.insert(url
.spec());
209 bool is_pattern
= HasPatternMatchingAnnotation(line_p
, line_end
);
210 manifest
.online_whitelist_namespaces
.push_back(
211 Namespace(NETWORK_NAMESPACE
, url
, GURL(), is_pattern
));
213 } else if (mode
== INTERCEPT
) {
214 // Lines of the form,
215 // <urlnamespace> <intercept_type> <targeturl>
216 const wchar_t* line_p
= line
.c_str();
217 const wchar_t* line_end
= line_p
+ line
.length();
219 // Look for first whitespace separating the url namespace from
220 // the intercept type.
221 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
224 if (line_p
== line_end
)
225 continue; // There was no whitespace separating the URLs.
227 base::string16 namespace_url16
;
228 WideToUTF16(line
.c_str(), line_p
- line
.c_str(), &namespace_url16
);
229 GURL namespace_url
= manifest_url
.Resolve(namespace_url16
);
230 if (!namespace_url
.is_valid())
232 if (namespace_url
.has_ref()) {
233 GURL::Replacements replacements
;
234 replacements
.ClearRef();
235 namespace_url
= namespace_url
.ReplaceComponents(replacements
);
238 // The namespace URL must have the same scheme, host and port
239 // as the manifest's URL.
240 if (manifest_url
.GetOrigin() != namespace_url
.GetOrigin())
243 // Skip whitespace separating namespace from the type.
244 while (line_p
< line_end
&& (*line_p
== '\t' || *line_p
== ' '))
247 // Look for whitespace separating the type from the target url.
248 const wchar_t* type_start
= line_p
;
249 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
252 // Look for a type value we understand, otherwise skip the line.
253 std::wstring
type(type_start
, line_p
- type_start
);
254 if (type
!= L
"return")
257 // Skip whitespace separating type from the target_url.
258 while (line_p
< line_end
&& (*line_p
== '\t' || *line_p
== ' '))
261 // Look for whitespace separating the URL from subsequent ignored tokens.
262 const wchar_t* target_url_start
= line_p
;
263 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
266 base::string16 target_url16
;
267 WideToUTF16(target_url_start
, line_p
- target_url_start
, &target_url16
);
268 GURL target_url
= manifest_url
.Resolve(target_url16
);
269 if (!target_url
.is_valid())
272 if (target_url
.has_ref()) {
273 GURL::Replacements replacements
;
274 replacements
.ClearRef();
275 target_url
= target_url
.ReplaceComponents(replacements
);
277 if (manifest_url
.GetOrigin() != target_url
.GetOrigin())
280 bool is_pattern
= HasPatternMatchingAnnotation(line_p
, line_end
);
281 manifest
.intercept_namespaces
.push_back(
282 Namespace(INTERCEPT_NAMESPACE
, namespace_url
,
283 target_url
, is_pattern
));
284 } else if (mode
== FALLBACK
) {
285 const wchar_t* line_p
= line
.c_str();
286 const wchar_t* line_end
= line_p
+ line
.length();
288 // Look for whitespace separating the two URLs
289 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
292 if (line_p
== line_end
) {
293 // There was no whitespace separating the URLs.
297 base::string16 namespace_url16
;
298 WideToUTF16(line
.c_str(), line_p
- line
.c_str(), &namespace_url16
);
299 GURL namespace_url
= manifest_url
.Resolve(namespace_url16
);
300 if (!namespace_url
.is_valid())
302 if (namespace_url
.has_ref()) {
303 GURL::Replacements replacements
;
304 replacements
.ClearRef();
305 namespace_url
= namespace_url
.ReplaceComponents(replacements
);
308 // Fallback namespace URL must have the same scheme, host and port
309 // as the manifest's URL.
310 if (manifest_url
.GetOrigin() != namespace_url
.GetOrigin()) {
314 // Skip whitespace separating fallback namespace from URL.
315 while (line_p
< line_end
&& (*line_p
== '\t' || *line_p
== ' '))
318 // Look for whitespace separating the URL from subsequent ignored tokens.
319 const wchar_t* fallback_start
= line_p
;
320 while (line_p
< line_end
&& *line_p
!= '\t' && *line_p
!= ' ')
323 base::string16 fallback_url16
;
324 WideToUTF16(fallback_start
, line_p
- fallback_start
, &fallback_url16
);
325 GURL fallback_url
= manifest_url
.Resolve(fallback_url16
);
326 if (!fallback_url
.is_valid())
328 if (fallback_url
.has_ref()) {
329 GURL::Replacements replacements
;
330 replacements
.ClearRef();
331 fallback_url
= fallback_url
.ReplaceComponents(replacements
);
334 // Fallback entry URL must have the same scheme, host and port
335 // as the manifest's URL.
336 if (manifest_url
.GetOrigin() != fallback_url
.GetOrigin()) {
340 bool is_pattern
= HasPatternMatchingAnnotation(line_p
, line_end
);
342 // Store regardless of duplicate namespace URL. Only first match
343 // will ever be used.
344 manifest
.fallback_namespaces
.push_back(
345 Namespace(FALLBACK_NAMESPACE
, namespace_url
,
346 fallback_url
, is_pattern
));
355 } // namespace appcache