This source file includes following definitions.
- setContentType
- setContentTypeParameter
- skipSpaces
- parseParameterPart
- substringForRange
- parseContentType
- isValidContentType
- charset
- parameterValueForName
- parameterCount
- setContentType
- setContentTypeParameter
#include "config.h"
#include "platform/network/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 SubstringRange parseParameterPart(const String& input, unsigned& startIndex)
{
unsigned inputLength = input.length();
unsigned tokenStart = startIndex;
unsigned& tokenEnd = startIndex;
if (tokenEnd >= inputLength)
return SubstringRange();
bool quoted = input[tokenStart] == '\"';
bool escape = false;
while (tokenEnd < inputLength) {
UChar c = input[tokenEnd];
if (quoted && tokenStart != tokenEnd && c == '\"' && !escape)
return SubstringRange(tokenStart + 1, tokenEnd++ - tokenStart - 1);
if (!quoted && (c == ';' || c == '='))
return SubstringRange(tokenStart, tokenEnd - tokenStart);
escape = !escape && c == '\\';
++tokenEnd;
}
if (quoted)
return SubstringRange();
return SubstringRange(tokenStart, tokenEnd - tokenStart);
}
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) {
WTF_LOG_ERROR("Invalid Content-Type string '%s'", contentType.ascii().data());
return false;
}
size_t semiColonIndex = contentType.find(';', index);
if (semiColonIndex == kNotFound) {
receiver.setContentType(SubstringRange(index, contentTypeLength - index));
return true;
}
receiver.setContentType(SubstringRange(index, semiColonIndex - index));
index = semiColonIndex + 1;
while (true) {
skipSpaces(contentType, index);
SubstringRange keyRange = parseParameterPart(contentType, index);
if (!keyRange.second || index >= contentTypeLength) {
WTF_LOG_ERROR("Invalid Content-Type parameter name. (at %i)", index);
return false;
}
if (contentType[index++] != '=' || index >= contentTypeLength) {
WTF_LOG_ERROR("Invalid Content-Type malformed parameter (at %i).", index);
return false;
}
SubstringRange valueRange = parseParameterPart(contentType, index);
if (!valueRange.second) {
WTF_LOG_ERROR("Invalid Content-Type, invalid parameter value (at %i, for '%s').", index, substringForRange(contentType, keyRange).stripWhiteSpace().ascii().data());
return false;
}
if (index < contentTypeLength && contentType[index++] != ';') {
WTF_LOG_ERROR("Invalid Content-Type, invalid character at the end of key/value parameter (at %i).", index);
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));
}
}