Title: [241291] trunk
Revision
241291
Author
[email protected]
Date
2019-02-12 02:04:38 -0800 (Tue, 12 Feb 2019)

Log Message

Implement serializing in MIME type parser
https://bugs.webkit.org/show_bug.cgi?id=193909

Patch by Rob Buis <[email protected]> on 2019-02-12
Reviewed by Darin Adler.

Source/WebCore:

Implement serializing in MIME type parser [1], to preserve the parameter
order the Vector m_parameterNames is introduced, since HashMaps do not
guarantee any order.

Test: ParsedContentType.Serialize

[1] https://mimesniff.spec.whatwg.org/#serializing-a-mime-type

* platform/network/ParsedContentType.cpp:
(WebCore::skipSpaces):
(WebCore::parseQuotedString):
(WebCore::ParsedContentType::parseContentType):
(WebCore::ParsedContentType::parameterValueForName const):
(WebCore::ParsedContentType::parameterCount const):
(WebCore::ParsedContentType::setContentType):
(WebCore::ParsedContentType::setContentTypeParameter):
(WebCore::ParsedContentType::serialize const):
* platform/network/ParsedContentType.h:

Tools:

Add various tests involving upper case, non-ASCII, control/null characters,
various HTTP whitespace, single quotes and an unpaired surrogate.

* TestWebKitAPI/Tests/WebCore/ParsedContentType.cpp:
(TestWebKitAPI::escapeNonASCIIPrintableCharacters):
(TestWebKitAPI::serializeIfValid):
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (241290 => 241291)


--- trunk/Source/WebCore/ChangeLog	2019-02-12 09:29:46 UTC (rev 241290)
+++ trunk/Source/WebCore/ChangeLog	2019-02-12 10:04:38 UTC (rev 241291)
@@ -1,3 +1,29 @@
+2019-02-12  Rob Buis  <[email protected]>
+
+        Implement serializing in MIME type parser
+        https://bugs.webkit.org/show_bug.cgi?id=193909
+
+        Reviewed by Darin Adler.
+
+        Implement serializing in MIME type parser [1], to preserve the parameter
+        order the Vector m_parameterNames is introduced, since HashMaps do not
+        guarantee any order.
+
+        Test: ParsedContentType.Serialize
+
+        [1] https://mimesniff.spec.whatwg.org/#serializing-a-mime-type
+
+        * platform/network/ParsedContentType.cpp:
+        (WebCore::skipSpaces):
+        (WebCore::parseQuotedString):
+        (WebCore::ParsedContentType::parseContentType):
+        (WebCore::ParsedContentType::parameterValueForName const):
+        (WebCore::ParsedContentType::parameterCount const):
+        (WebCore::ParsedContentType::setContentType):
+        (WebCore::ParsedContentType::setContentTypeParameter):
+        (WebCore::ParsedContentType::serialize const):
+        * platform/network/ParsedContentType.h:
+
 2019-02-08  Chris Fleizach  <[email protected]>
 
         AXObjectCache::childrenChanged shouldn't update layout or style during another style recalc

Modified: trunk/Source/WebCore/platform/network/ParsedContentType.cpp (241290 => 241291)


--- trunk/Source/WebCore/platform/network/ParsedContentType.cpp	2019-02-12 09:29:46 UTC (rev 241290)
+++ trunk/Source/WebCore/platform/network/ParsedContentType.cpp	2019-02-12 10:04:38 UTC (rev 241291)
@@ -32,13 +32,15 @@
 #include "config.h"
 #include "ParsedContentType.h"
 
+#include "HTTPParsers.h"
 #include <wtf/text/CString.h>
+#include <wtf/text/StringBuilder.h>
 
 namespace WebCore {
 
 static void skipSpaces(const String& input, unsigned& startIndex)
 {
-    while (startIndex < input.length() && input[startIndex] == ' ')
+    while (startIndex < input.length() && isHTTPSpace(input[startIndex]))
         ++startIndex;
 }
 
@@ -96,8 +98,11 @@
     if (quotedStringEnd >= inputLength)
         return WTF::nullopt;
 
-    if (input[quotedStringEnd++] != '"' || quotedStringEnd >= inputLength)
-        return WTF::nullopt;
+    if (input[quotedStringEnd++] != '"' || quotedStringEnd >= inputLength) {
+        if (mode == Mode::Rfc2045)
+            return WTF::nullopt;
+        return SubstringRange(quotedStringStart, quotedStringEnd - quotedStringStart);
+    }
 
     bool lastCharacterWasBackslash = false;
     char currentCharacter;
@@ -114,7 +119,9 @@
         if (lastCharacterWasBackslash)
             lastCharacterWasBackslash = false;
     }
-    return SubstringRange(quotedStringStart, quotedStringEnd - quotedStringStart - 1);
+    if (input[quotedStringEnd - 1] == '"')
+        return SubstringRange(quotedStringStart, quotedStringEnd - quotedStringStart - 1);
+    return SubstringRange(quotedStringStart, quotedStringEnd - quotedStringStart);
 }
 
 static String substringForRange(const String& string, const SubstringRange& range)
@@ -190,7 +197,7 @@
 
 bool ParsedContentType::parseContentType(Mode mode)
 {
-    if (m_contentType.find(containsNewline) != notFound)
+    if (mode == Mode::Rfc2045 && m_contentType.find(containsNewline) != notFound)
         return false;
     unsigned index = 0;
     unsigned contentTypeLength = m_contentType.length();
@@ -311,12 +318,12 @@
 
 String ParsedContentType::parameterValueForName(const String& name) const
 {
-    return m_parameters.get(name);
+    return m_parameterValues.get(name);
 }
 
 size_t ParsedContentType::parameterCount() const
 {
-    return m_parameters.size();
+    return m_parameterValues.size();
 }
 
 void ParsedContentType::setContentType(const SubstringRange& contentRange, Mode mode)
@@ -323,7 +330,7 @@
 {
     m_mimeType = substringForRange(m_contentType, contentRange).stripWhiteSpace();
     if (mode == Mode::MimeSniff)
-        m_mimeType.convertToASCIILowercase();
+        m_mimeType = m_mimeType.convertToASCIILowercase();
 }
 
 static bool isNonTokenCharacter(UChar ch)
@@ -338,12 +345,33 @@
 
 void ParsedContentType::setContentTypeParameter(const String& keyName, const String& keyValue, Mode mode)
 {
+    String name = keyName;
     if (mode == Mode::MimeSniff) {
-        if (m_parameters.contains(keyName) || keyName.find(isNonTokenCharacter) != notFound || keyValue.find(isNonQuotedStringTokenCharacter) != notFound)
+        if (m_parameterValues.contains(keyName) || keyName.find(isNonTokenCharacter) != notFound || keyValue.find(isNonQuotedStringTokenCharacter) != notFound)
             return;
-        keyName.convertToASCIILowercase();
+        name = name.convertToASCIILowercase();
     }
-    m_parameters.set(keyName, keyValue);
+    m_parameterValues.set(name, keyValue);
+    m_parameterNames.append(name);
 }
 
+String ParsedContentType::serialize() const
+{
+    StringBuilder builder;
+    builder.append(m_mimeType);
+    for (auto& name : m_parameterNames) {
+        builder.append(';');
+        builder.append(name);
+        builder.append('=');
+        String value = m_parameterValues.get(name);
+        if (value.isEmpty() || value.find(isNonTokenCharacter) != notFound) {
+            builder.append('"');
+            builder.append(value);
+            builder.append('"');
+        } else
+            builder.append(value);
+    }
+    return builder.toString();
 }
+
+}

Modified: trunk/Source/WebCore/platform/network/ParsedContentType.h (241290 => 241291)


--- trunk/Source/WebCore/platform/network/ParsedContentType.h	2019-02-12 09:29:46 UTC (rev 241290)
+++ trunk/Source/WebCore/platform/network/ParsedContentType.h	2019-02-12 10:04:38 UTC (rev 241291)
@@ -47,7 +47,7 @@
 // FIXME: add support for comments.
 class ParsedContentType {
 public:
-    static Optional<ParsedContentType> create(const String&, Mode = Mode::MimeSniff);
+    WEBCORE_EXPORT static Optional<ParsedContentType> create(const String&, Mode = Mode::MimeSniff);
     ParsedContentType(ParsedContentType&&) = default;
 
     String mimeType() const { return m_mimeType; }
@@ -57,6 +57,8 @@
     String parameterValueForName(const String&) const;
     size_t parameterCount() const;
 
+    WEBCORE_EXPORT String serialize() const;
+
 private:
     ParsedContentType(const String&);
     ParsedContentType(const ParsedContentType&) = delete;
@@ -67,7 +69,8 @@
 
     typedef HashMap<String, String> KeyValuePairs;
     String m_contentType;
-    KeyValuePairs m_parameters;
+    KeyValuePairs m_parameterValues;
+    Vector<String> m_parameterNames;
     String m_mimeType;
 };
 

Modified: trunk/Tools/ChangeLog (241290 => 241291)


--- trunk/Tools/ChangeLog	2019-02-12 09:29:46 UTC (rev 241290)
+++ trunk/Tools/ChangeLog	2019-02-12 10:04:38 UTC (rev 241291)
@@ -1,3 +1,18 @@
+2019-02-12  Rob Buis  <[email protected]>
+
+        Implement serializing in MIME type parser
+        https://bugs.webkit.org/show_bug.cgi?id=193909
+
+        Reviewed by Darin Adler.
+
+        Add various tests involving upper case, non-ASCII, control/null characters,
+        various HTTP whitespace, single quotes and an unpaired surrogate.
+
+        * TestWebKitAPI/Tests/WebCore/ParsedContentType.cpp:
+        (TestWebKitAPI::escapeNonASCIIPrintableCharacters):
+        (TestWebKitAPI::serializeIfValid):
+        (TestWebKitAPI::TEST):
+
 2019-02-11  Myles C. Maxfield  <[email protected]>
 
         [Cocoa] Ask platform for generic font family mappings

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ParsedContentType.cpp (241290 => 241291)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ParsedContentType.cpp	2019-02-12 09:29:46 UTC (rev 241290)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ParsedContentType.cpp	2019-02-12 10:04:38 UTC (rev 241291)
@@ -27,6 +27,7 @@
 
 #include "Test.h"
 #include <WebCore/ParsedContentType.h>
+#include <wtf/text/StringBuilder.h>
 
 using namespace WebCore;
 
@@ -98,4 +99,148 @@
     EXPECT_FALSE(isValidContentType("text/plain;test=\"value\"foo;foo=bar", Mode::Rfc2045));
 }
 
+// The word "escape" here just means a format easy to read in test results.
+// It doesn't have to be a format suitable for other users or even one
+// that is completely unambiguous.
+static const char* escapeNonASCIIPrintableCharacters(StringView string)
+{
+    static char resultBuffer[100];
+    size_t i = 0;
+    auto append = [&i] (char character) {
+        if (i < sizeof(resultBuffer))
+            resultBuffer[i++] = character;
+    };
+    auto appendNibble = [append] (char nibble) {
+        if (nibble)
+            append(lowerNibbleToASCIIHexDigit(nibble));
+    };
+    auto appendLastNibble = [append] (char nibble) {
+        append(lowerNibbleToASCIIHexDigit(nibble));
+    };
+    for (auto character : string.codePoints()) {
+        if (isASCIIPrintable(character))
+            append(character);
+        else {
+            append('{');
+            for (unsigned i = 32 - 4; i; i -= 4)
+                appendNibble(character >> i);
+            appendLastNibble(character);
+            append('}');
+        }
+    }
+    if (i == sizeof(resultBuffer))
+        return "";
+    resultBuffer[i] = '\0';
+    return resultBuffer;
+}
+
+static const char* serializeIfValid(const String& input)
+{
+    if (Optional<ParsedContentType> parsedContentType = ParsedContentType::create(input))
+        return escapeNonASCIIPrintableCharacters(parsedContentType->serialize());
+    return "NOTVALID";
+}
+
+TEST(ParsedContentType, Serialize)
+{
+    EXPECT_STREQ(serializeIfValid(""), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid(";"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/\0"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/ "), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/plain"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain\0"), "text/plain");
+    EXPECT_STREQ(serializeIfValid(" text/plain"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\ttext/plain"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\ntext/plain"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\rtext/plain"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\btext/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/plain "), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain\t"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain\n"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain\r"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain\b"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid(" text/plain "), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\ttext/plain\t"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\ntext/plain\n"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\rtext/plain\r"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("\btext/plain\b"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/PLAIN"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text /plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/ plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text / plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("te xt/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/pla in"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\t/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/\tplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\t/\tplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("te\txt/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/pla\tin"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\n/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/\nplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\n/\nplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("te\nxt/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/pla\nin"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\r/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/\rplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text\r/\rplain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("te\rxt/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/pla\rin"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("/plain"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("†/†"), "NOTVALID");
+    EXPECT_STREQ(serializeIfValid("text/\xD8\x88\x12\x34"), "NOTVALID");
+
+    EXPECT_STREQ(serializeIfValid("text/plain;"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain; test"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;\ttest"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;\ntest"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;\rtest"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;\btest"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test "), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\t"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\n"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\r"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\b"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test="), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=;test=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;TEST=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=VALUE"), "text/plain;test=VALUE");
+    EXPECT_STREQ(serializeIfValid("text/plain; test=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;\ttest=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;\ntest=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;\rtest=value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;\btest=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;\0test=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test =value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\t=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\n=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\r=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test\b=value"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test= value"), "text/plain;test=\" value\"");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\tvalue"), "text/plain;test=\"{9}value\"");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\nvalue"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\rvalue"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\bvalue"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value "), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value\t"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value\n"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value\r"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value\b"), "text/plain");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"value\""), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"value"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\""), "text/plain;test=\"\"");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"value\"foo"), "text/plain;test=value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"value\"foo;foo=bar"), "text/plain;test=value;foo=bar");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"val\\ue\""), "text/plain;test=\"val\\ue\"");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=\"val\\\"ue\""), "text/plain;test=\"val\\\"ue\"");
+    EXPECT_STREQ(serializeIfValid("text/plain;test='value'"), "text/plain;test='value'");
+    EXPECT_STREQ(serializeIfValid("text/plain;test='value"), "text/plain;test='value");
+    EXPECT_STREQ(serializeIfValid("text/plain;test=value'"), "text/plain;test=value'");
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to