ParsedContentType.cpp [plain text]
#include "config.h"
#include "ParsedContentType.h"
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
class DummyParsedContentType {
public:
void setContentType(const SubstringRange&) const { }
void setContentTypeParameter(const SubstringRange&, const SubstringRange&) const { }
};
static void skipSpaces(const String& input, unsigned& 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 SubstringRange parseToken(const String& input, unsigned& startIndex)
{
unsigned inputLength = input.length();
unsigned tokenStart = startIndex;
unsigned& tokenEnd = startIndex;
if (tokenEnd >= inputLength)
return SubstringRange();
while (tokenEnd < inputLength) {
if (!isTokenCharacter(input[tokenEnd]))
return SubstringRange(tokenStart, tokenEnd - tokenStart);
++tokenEnd;
}
return SubstringRange(tokenStart, tokenEnd - tokenStart);
}
static SubstringRange parseQuotedString(const String& input, unsigned& startIndex)
{
unsigned inputLength = input.length();
unsigned quotedStringStart = startIndex + 1;
unsigned& quotedStringEnd = startIndex;
if (quotedStringEnd >= inputLength)
return SubstringRange();
if (input[quotedStringEnd++] != '"' || quotedStringEnd >= inputLength)
return SubstringRange();
bool lastCharacterWasBackslash = false;
char currentCharacter;
while ((currentCharacter = input[quotedStringEnd++]) != '"' || lastCharacterWasBackslash) {
if (quotedStringEnd >= inputLength)
return SubstringRange();
if (currentCharacter == '\\' && !lastCharacterWasBackslash) {
lastCharacterWasBackslash = true;
continue;
}
if (lastCharacterWasBackslash)
lastCharacterWasBackslash = false;
}
return SubstringRange(quotedStringStart, quotedStringEnd - quotedStringStart - 1);
}
static String substringForRange(const String& string, const SubstringRange& range)
{
return string.substring(range.first, range.second);
}
template <class ReceiverType>
bool parseContentType(const String& contentType, ReceiverType& receiver)
{
unsigned index = 0;
unsigned contentTypeLength = contentType.length();
skipSpaces(contentType, index);
if (index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type string '%s'", contentType.ascii().data());
return false;
}
size_t semiColonIndex = contentType.find(';', index);
if (semiColonIndex == notFound) {
receiver.setContentType(SubstringRange(index, contentTypeLength - index));
return true;
}
receiver.setContentType(SubstringRange(index, semiColonIndex - index));
index = semiColonIndex + 1;
while (true) {
skipSpaces(contentType, index);
SubstringRange keyRange = parseToken(contentType, index);
if (!keyRange.second || index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type parameter name.");
return false;
}
if (contentType[index++] != '=' || index >= contentTypeLength) {
LOG_ERROR("Invalid Content-Type malformed parameter.");
return false;
}
String value;
SubstringRange valueRange;
if (contentType[index] == '"')
valueRange = parseQuotedString(contentType, index);
else
valueRange = parseToken(contentType, index);
if (!valueRange.second) {
LOG_ERROR("Invalid Content-Type, invalid parameter value.");
return false;
}
if (index < contentTypeLength && contentType[index++] != ';') {
LOG_ERROR("Invalid Content-Type, invalid character at the end of key/value parameter.");
return false;
}
receiver.setContentTypeParameter(keyRange, valueRange);
if (index >= contentTypeLength)
return true;
}
return true;
}
bool isValidContentType(const String& contentType)
{
if (contentType.contains('\r') || contentType.contains('\n'))
return false;
DummyParsedContentType parsedContentType = DummyParsedContentType();
return parseContentType<DummyParsedContentType>(contentType, parsedContentType);
}
ParsedContentType::ParsedContentType(const String& contentType)
: m_contentType(contentType.stripWhiteSpace())
{
parseContentType<ParsedContentType>(m_contentType, *this);
}
String ParsedContentType::charset() const
{
return parameterValueForName("charset");
}
String ParsedContentType::parameterValueForName(const String& name) const
{
return m_parameters.get(name);
}
size_t ParsedContentType::parameterCount() const
{
return m_parameters.size();
}
void ParsedContentType::setContentType(const SubstringRange& contentRange)
{
m_mimeType = substringForRange(m_contentType, contentRange).stripWhiteSpace();
}
void ParsedContentType::setContentTypeParameter(const SubstringRange& key, const SubstringRange& value)
{
m_parameters.set(substringForRange(m_contentType, key), substringForRange(m_contentType, value));
}
}