ContentTypeParser.cpp [plain text]
#include "config.h"
#include "ContentTypeParser.h"
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
static void skipSpaces(const String& input, size_t& startIndex)
{
while (startIndex < input.length() && input[startIndex] == ' ')
++startIndex;
}
static bool isTokenCharacter(char c)
{
return isASCII(c) && c > ' ' && c != '"' && c != '(' && c != ')' && c != ',' && c != '/' && (c < ':' || c > '@') && (c < '[' || c > ']');
}
static String parseToken(const String& input, size_t& startIndex)
{
if (startIndex >= input.length())
return String();
StringBuilder stringBuilder;
while (startIndex < input.length()) {
char currentCharacter = input[startIndex];
if (!isTokenCharacter(currentCharacter))
return stringBuilder.toString();
stringBuilder.append(currentCharacter);
++startIndex;
}
return stringBuilder.toString();
}
static String parseQuotedString(const String& input, size_t& startIndex)
{
if (startIndex >= input.length())
return String();
if (input[startIndex++] != '"' || startIndex >= input.length())
return String();
StringBuilder stringBuilder;
bool lastCharacterWasBackslash = false;
char currentCharacter;
while ((currentCharacter = input[startIndex++]) != '"' || lastCharacterWasBackslash) {
if (startIndex >= input.length())
return String();
if (currentCharacter == '\\' && !lastCharacterWasBackslash) {
lastCharacterWasBackslash = true;
continue;
}
if (lastCharacterWasBackslash)
lastCharacterWasBackslash = false;
stringBuilder.append(currentCharacter);
}
return stringBuilder.toString();
}
ContentTypeParser::ContentTypeParser(const String& contentType)
: m_contentType(contentType.stripWhiteSpace())
{
parse();
}
String ContentTypeParser::charset() const
{
return parameterValueForName("charset");
}
String ContentTypeParser::parameterValueForName(const String& name) const
{
return m_parameters.get(name);
}
size_t ContentTypeParser::parameterCount() const
{
return m_parameters.size();
}
void ContentTypeParser::parse()
{
DEFINE_STATIC_LOCAL(const String, contentTypeParameterName, ("Content-Type"));
if (!m_contentType.startsWith(contentTypeParameterName)) {
LOG_ERROR("Invalid Content-Type string '%s'", m_contentType.ascii().data());
return;
}
size_t contentTypeLength = m_contentType.length();
size_t index = contentTypeParameterName.length();
skipSpaces(m_contentType, index);
if (index >= contentTypeLength || m_contentType[index] != ':' || ++index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type string '%s'", m_contentType.ascii().data());
return;
}
size_t semiColonIndex = m_contentType.find(';', index);
if (semiColonIndex == notFound) {
m_mimeType = m_contentType.substring(index).stripWhiteSpace();
return;
}
m_mimeType = m_contentType.substring(index, semiColonIndex - index).stripWhiteSpace();
index = semiColonIndex + 1;
while (true) {
skipSpaces(m_contentType, index);
String key = parseToken(m_contentType, index);
if (key.isEmpty() || index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type parameter name.");
return;
}
if (m_contentType[index++] != '=' || index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type malformed parameter.");
return;
}
String value;
if (m_contentType[index] == '"')
value = parseQuotedString(m_contentType, index);
else
value = parseToken(m_contentType, index);
if (value.isNull()) {
LOG_ERROR("Invalid Content-Type, invalid parameter value.");
return;
}
if (index < contentTypeLength && m_contentType[index++] != ';') {
LOG_ERROR("Invalid Content-Type, invalid character at the end of key/value parameter.");
return;
}
m_parameters.set(key, value);
if (index >= contentTypeLength)
return;
}
}
}