Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1541)

Unified Diff: Source/core/platform/network/ParsedContentType.cpp

Issue 23043002: Relax Content-Type parameter parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Test fixes Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « LayoutTests/mhtml/relaxed-content-type-parameters-expected.txt ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/platform/network/ParsedContentType.cpp
diff --git a/Source/core/platform/network/ParsedContentType.cpp b/Source/core/platform/network/ParsedContentType.cpp
index d8db86a8f3b8e6df430639c5b1ab9db6e42a696e..d08a3dd65cd9169b5557854b5426475e2be0fe73 100644
--- a/Source/core/platform/network/ParsedContentType.cpp
+++ b/Source/core/platform/network/ParsedContentType.cpp
@@ -49,12 +49,7 @@ static void skipSpaces(const String& input, unsigned& 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)
+static SubstringRange parseParameterPart(const String& input, unsigned& startIndex)
{
unsigned inputLength = input.length();
unsigned tokenStart = startIndex;
@@ -63,40 +58,22 @@ static SubstringRange parseToken(const String& input, unsigned& startIndex)
if (tokenEnd >= inputLength)
return SubstringRange();
+ bool quoted = input[tokenStart] == '\"';
+ bool escape = false;
+
while (tokenEnd < inputLength) {
- if (!isTokenCharacter(input[tokenEnd]))
+ 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;
}
- 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)
+ if (quoted)
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);
+ return SubstringRange(tokenStart, tokenEnd - tokenStart);
}
static String substringForRange(const String& string, const SubstringRange& range)
@@ -172,34 +149,29 @@ bool parseContentType(const String& contentType, ReceiverType& receiver)
index = semiColonIndex + 1;
while (true) {
skipSpaces(contentType, index);
- SubstringRange keyRange = parseToken(contentType, index);
+ SubstringRange keyRange = parseParameterPart(contentType, index);
if (!keyRange.second || index >= contentTypeLength) {
- LOG_ERROR("Invalid Content-Type parameter name.");
+ LOG_ERROR("Invalid Content-Type parameter name. (at %i)", index);
return false;
}
// Should we tolerate spaces here?
if (contentType[index++] != '=' || index >= contentTypeLength) {
- LOG_ERROR("Invalid Content-Type malformed parameter.");
+ LOG_ERROR("Invalid Content-Type malformed parameter (at %i).", index);
return false;
}
// Should we tolerate spaces here?
- String value;
- SubstringRange valueRange;
- if (contentType[index] == '"')
- valueRange = parseQuotedString(contentType, index);
- else
- valueRange = parseToken(contentType, index);
+ SubstringRange valueRange = parseParameterPart(contentType, index);
if (!valueRange.second) {
- LOG_ERROR("Invalid Content-Type, invalid parameter value.");
+ LOG_ERROR("Invalid Content-Type, invalid parameter value (at %i, for '%s').", index, substringForRange(contentType, keyRange).stripWhiteSpace().ascii().data());
return false;
}
// Should we tolerate spaces here?
if (index < contentTypeLength && contentType[index++] != ';') {
- LOG_ERROR("Invalid Content-Type, invalid character at the end of key/value parameter.");
+ LOG_ERROR("Invalid Content-Type, invalid character at the end of key/value parameter (at %i).", index);
return false;
}
« no previous file with comments | « LayoutTests/mhtml/relaxed-content-type-parameters-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698