ManifestParser.cpp [plain text]
#include "config.h"
#include "ManifestParser.h"
#include "KURL.h"
#include "TextResourceDecoder.h"
#include <wtf/unicode/CharacterNames.h>
using namespace std;
namespace WebCore {
enum Mode { Explicit, Fallback, OnlineWhitelist, Unknown };
bool parseManifest(const KURL& manifestURL, const char* data, int length, Manifest& manifest)
{
ASSERT(manifest.explicitURLs.isEmpty());
ASSERT(manifest.onlineWhitelistedURLs.isEmpty());
ASSERT(manifest.fallbackURLs.isEmpty());
manifest.allowAllNetworkRequests = false;
Mode mode = Explicit;
RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/cache-manifest", "UTF-8");
String s = decoder->decode(data, length);
s.append(decoder->flush());
if (!s.startsWith("CACHE MANIFEST"))
return false;
const UChar* end = s.characters() + s.length();
const UChar* p = s.characters() + 14;
if (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r')
return false;
while (p < end && *p != '\r' && *p != '\n')
p++;
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 (line.endsWith(':'))
mode = Unknown;
else if (mode == Unknown)
continue;
else if (mode == Explicit || mode == OnlineWhitelist) {
const UChar* p = line.characters();
const UChar* lineEnd = p + line.length();
while (p < lineEnd && *p != '\t' && *p != ' ')
p++;
if (mode == OnlineWhitelist && p - line.characters() == 1 && *line.characters() == '*') {
manifest.allowAllNetworkRequests = true;
continue;
}
KURL url(manifestURL, String(line.characters(), p - line.characters()));
if (!url.isValid())
continue;
if (url.hasFragmentIdentifier())
url.removeFragmentIdentifier();
if (!equalIgnoringCase(url.protocol(), manifestURL.protocol()))
continue;
if (mode == Explicit && manifestURL.protocolIs("https") && !protocolHostAndPortAreEqual(manifestURL, url))
continue;
if (mode == Explicit)
manifest.explicitURLs.add(url.string());
else
manifest.onlineWhitelistedURLs.append(url);
} 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, String(line.characters(), p - line.characters()));
if (!namespaceURL.isValid())
continue;
if (namespaceURL.hasFragmentIdentifier())
namespaceURL.removeFragmentIdentifier();
if (!protocolHostAndPortAreEqual(manifestURL, namespaceURL))
continue;
while (p < lineEnd && (*p == '\t' || *p == ' '))
p++;
const UChar* fallbackStart = p;
while (p < lineEnd && *p != '\t' && *p != ' ')
p++;
KURL fallbackURL(manifestURL, String(fallbackStart, p - fallbackStart));
if (!fallbackURL.isValid())
continue;
if (fallbackURL.hasFragmentIdentifier())
fallbackURL.removeFragmentIdentifier();
if (!protocolHostAndPortAreEqual(manifestURL, fallbackURL))
continue;
manifest.fallbackURLs.append(make_pair(namespaceURL, fallbackURL));
} else
ASSERT_NOT_REACHED();
}
return true;
}
}