ManifestParser.cpp [plain text]
#include "config.h"
#include "ManifestParser.h"
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "CharacterNames.h"
#include "KURL.h"
#include "TextEncoding.h"
namespace WebCore {
enum Mode { Explicit, Fallback, OnlineWhitelist };
bool parseManifest(const KURL& manifestURL, const char* data, int length, Manifest& manifest)
{
ASSERT(manifest.explicitURLs.isEmpty());
ASSERT(manifest.onlineWhitelistedURLs.isEmpty());
ASSERT(manifest.fallbackURLs.isEmpty());
Mode mode = Explicit;
String s = UTF8Encoding().decode(data, length);
if (s.isEmpty())
return false;
s.replace(0, 0xfffd);
if (!s.startsWith("CACHE MANIFEST")) {
return false;
}
const UChar* end = s.characters() + s.length();
const UChar* p = s.characters() + 14;
while (p < end) {
if (*p == ' ' || *p == '\t') {
p++;
} else
break;
}
if (p < end && *p != '\n' && *p != '\r') {
return false;
}
while (1) {
while (p < end && (*p == '\n' || *p == '\r' || *p == ' ' || *p == '\t'))
p++;
if (p == end)
break;
const UChar* lineStart = p;
while (p < end && *p != '\r' && *p != '\n')
p++;
if (*lineStart == '#')
continue;
const UChar* tmp = p - 1;
while (tmp > lineStart && (*tmp == ' ' || *tmp == '\t'))
tmp--;
String line(lineStart, tmp - lineStart + 1);
if (line == "CACHE:")
mode = Explicit;
else if (line == "FALLBACK:")
mode = Fallback;
else if (line == "NETWORK:")
mode = OnlineWhitelist;
else if (mode == Explicit || mode == OnlineWhitelist) {
KURL url(manifestURL, line.deprecatedString());
if (!url.isValid())
continue;
if (url.hasRef())
url.setRef(DeprecatedString());
if (!equalIgnoringCase(url.protocol(), manifestURL.protocol()))
continue;
if (mode == Explicit)
manifest.explicitURLs.add(url.string());
else
manifest.onlineWhitelistedURLs.add(url.string());
} else if (mode == Fallback) {
const UChar *p = line.characters();
const UChar *lineEnd = p + line.length();
while (p < lineEnd && *p != '\t' && *p != ' ')
p++;
if (p == lineEnd) {
continue;
}
KURL namespaceURL(manifestURL, DeprecatedString(reinterpret_cast<const DeprecatedChar*>(line.characters()), p - line.characters()));
if (!namespaceURL.isValid())
continue;
if (!protocolHostAndPortAreEqual(manifestURL, namespaceURL))
continue;
while (p < lineEnd && (*p == '\t' || *p == ' '))
p++;
KURL fallbackURL(DeprecatedString(reinterpret_cast<const DeprecatedChar*>(p), line.length() - (p - line.characters())));
if (!fallbackURL.isValid())
continue;
if (!equalIgnoringCase(fallbackURL.protocol(), manifestURL.protocol()))
continue;
manifest.fallbackURLs.add(namespaceURL.string(), fallbackURL.string());
} else
ASSERT_NOT_REACHED();
}
return true;
}
}
#endif // ENABLE(OFFLINE_WEB_APPLICATIONS)