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 "ManifestParser.h"
30
#include "TextResourceDecoder.h"
31
#include <wtf/unicode/CharacterNames.h>
37
enum Mode { Explicit, Fallback, OnlineWhitelist, Unknown };
39
bool parseManifest(const KURL& manifestURL, const char* data, int length, Manifest& manifest)
41
ASSERT(manifest.explicitURLs.isEmpty());
42
ASSERT(manifest.onlineWhitelistedURLs.isEmpty());
43
ASSERT(manifest.fallbackURLs.isEmpty());
44
manifest.allowAllNetworkRequests = false;
48
RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/cache-manifest", "UTF-8");
49
String s = decoder->decode(data, length);
50
s.append(decoder->flush());
52
// Look for the magic signature: "^\xFEFF?CACHE MANIFEST[ \t]?" (the BOM is removed by TextResourceDecoder).
53
// Example: "CACHE MANIFEST #comment" is a valid signature.
54
// Example: "CACHE MANIFEST;V2" is not.
55
if (!s.startsWith("CACHE MANIFEST"))
58
const UChar* end = s.characters() + s.length();
59
const UChar* p = s.characters() + 14; // "CACHE MANIFEST" is 14 characters.
61
if (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
64
// Skip to the end of the line.
65
while (p < end && *p != '\r' && *p != '\n')
70
while (p < end && (*p == '\n' || *p == '\r' || *p == ' ' || *p == '\t'))
76
const UChar* lineStart = p;
78
// Find the end of the line
79
while (p < end && *p != '\r' && *p != '\n')
82
// Check if we have a comment
83
if (*lineStart == '#')
86
// Get rid of trailing whitespace
87
const UChar* tmp = p - 1;
88
while (tmp > lineStart && (*tmp == ' ' || *tmp == '\t'))
91
String line(lineStart, tmp - lineStart + 1);
95
else if (line == "FALLBACK:")
97
else if (line == "NETWORK:")
98
mode = OnlineWhitelist;
99
else if (line.endsWith(':'))
101
else if (mode == Unknown)
103
else if (mode == Explicit || mode == OnlineWhitelist) {
104
const UChar* p = line.characters();
105
const UChar* lineEnd = p + line.length();
107
// Look for whitespace separating the URL from subsequent ignored tokens.
108
while (p < lineEnd && *p != '\t' && *p != ' ')
111
if (mode == OnlineWhitelist && p - line.characters() == 1 && *line.characters() == '*') {
112
// Wildcard was found.
113
manifest.allowAllNetworkRequests = true;
117
KURL url(manifestURL, String(line.characters(), p - line.characters()));
122
if (url.hasFragmentIdentifier())
123
url.removeFragmentIdentifier();
125
if (!equalIgnoringCase(url.protocol(), manifestURL.protocol()))
128
if (mode == Explicit && manifestURL.protocolIs("https") && !protocolHostAndPortAreEqual(manifestURL, url))
131
if (mode == Explicit)
132
manifest.explicitURLs.add(url.string());
134
manifest.onlineWhitelistedURLs.append(url);
136
} else if (mode == Fallback) {
137
const UChar* p = line.characters();
138
const UChar* lineEnd = p + line.length();
140
// Look for whitespace separating the two URLs
141
while (p < lineEnd && *p != '\t' && *p != ' ')
145
// There was no whitespace separating the URLs.
149
KURL namespaceURL(manifestURL, String(line.characters(), p - line.characters()));
150
if (!namespaceURL.isValid())
152
if (namespaceURL.hasFragmentIdentifier())
153
namespaceURL.removeFragmentIdentifier();
155
if (!protocolHostAndPortAreEqual(manifestURL, namespaceURL))
158
// Skip whitespace separating fallback namespace from URL.
159
while (p < lineEnd && (*p == '\t' || *p == ' '))
162
// Look for whitespace separating the URL from subsequent ignored tokens.
163
const UChar* fallbackStart = p;
164
while (p < lineEnd && *p != '\t' && *p != ' ')
167
KURL fallbackURL(manifestURL, String(fallbackStart, p - fallbackStart));
168
if (!fallbackURL.isValid())
170
if (fallbackURL.hasFragmentIdentifier())
171
fallbackURL.removeFragmentIdentifier();
173
if (!protocolHostAndPortAreEqual(manifestURL, fallbackURL))
176
manifest.fallbackURLs.append(make_pair(namespaceURL, fallbackURL));
178
ASSERT_NOT_REACHED();