Title: [207405] trunk
Revision
207405
Author
carlo...@webkit.org
Date
2016-10-17 03:52:02 -0700 (Mon, 17 Oct 2016)

Log Message

[SOUP] Downloads should always sniff contents
https://bugs.webkit.org/show_bug.cgi?id=163538

Reviewed by Michael Catanzaro.

Source/WebKit2:

It's quite common that downloads have weird filenames with no extension, so it would be better if the loader
sniff the contents to guess the mime type.

* NetworkProcess/Downloads/soup/DownloadSoup.cpp:
(WebKit::Download::startNetworkLoad): Pass true for shouldContentSniff parameter of ResourceHandle::create().

Tools:

Add a test case to check that the MIME type is correctly guessed for downloads.

* TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp:
(writeNextChunk):
(serverCallback):
(testDownloadMIMEType):
(beforeAll):
(writeNextChunkIdle): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (207404 => 207405)


--- trunk/Source/WebKit2/ChangeLog	2016-10-17 10:45:15 UTC (rev 207404)
+++ trunk/Source/WebKit2/ChangeLog	2016-10-17 10:52:02 UTC (rev 207405)
@@ -1,5 +1,18 @@
 2016-10-17  Carlos Garcia Campos  <cgar...@igalia.com>
 
+        [SOUP] Downloads should always sniff contents
+        https://bugs.webkit.org/show_bug.cgi?id=163538
+
+        Reviewed by Michael Catanzaro.
+
+        It's quite common that downloads have weird filenames with no extension, so it would be better if the loader
+        sniff the contents to guess the mime type.
+
+        * NetworkProcess/Downloads/soup/DownloadSoup.cpp:
+        (WebKit::Download::startNetworkLoad): Pass true for shouldContentSniff parameter of ResourceHandle::create().
+
+2016-10-17  Carlos Garcia Campos  <cgar...@igalia.com>
+
         [GTK] Switch to use API::DownloadClient instead of the C API client
         https://bugs.webkit.org/show_bug.cgi?id=163537
 

Modified: trunk/Source/WebKit2/NetworkProcess/Downloads/soup/DownloadSoup.cpp (207404 => 207405)


--- trunk/Source/WebKit2/NetworkProcess/Downloads/soup/DownloadSoup.cpp	2016-10-17 10:45:15 UTC (rev 207404)
+++ trunk/Source/WebKit2/NetworkProcess/Downloads/soup/DownloadSoup.cpp	2016-10-17 10:52:02 UTC (rev 207405)
@@ -212,7 +212,7 @@
     ASSERT(!m_downloadClient);
     ASSERT(!m_resourceHandle);
     m_downloadClient = std::make_unique<DownloadClient>(*this);
-    m_resourceHandle = ResourceHandle::create(0, m_request, m_downloadClient.get(), false, false);
+    m_resourceHandle = ResourceHandle::create(0, m_request, m_downloadClient.get(), false, true);
     didStart();
 }
 

Modified: trunk/Tools/ChangeLog (207404 => 207405)


--- trunk/Tools/ChangeLog	2016-10-17 10:45:15 UTC (rev 207404)
+++ trunk/Tools/ChangeLog	2016-10-17 10:52:02 UTC (rev 207405)
@@ -1,3 +1,19 @@
+2016-10-17  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        [SOUP] Downloads should always sniff contents
+        https://bugs.webkit.org/show_bug.cgi?id=163538
+
+        Reviewed by Michael Catanzaro.
+
+        Add a test case to check that the MIME type is correctly guessed for downloads.
+
+        * TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp:
+        (writeNextChunk):
+        (serverCallback):
+        (testDownloadMIMEType):
+        (beforeAll):
+        (writeNextChunkIdle): Deleted.
+
 2016-10-16  Daniel Bates  <daba...@apple.com>
 
         Fix Perl warning about masked variable following <http://trac.webkit.org/changeset/207382>

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp (207404 => 207405)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp	2016-10-17 10:45:15 UTC (rev 207404)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestDownloads.cpp	2016-10-17 10:52:02 UTC (rev 207405)
@@ -388,15 +388,17 @@
     soup_message_headers_append(message->response_headers, "Content-Disposition", contentDisposition.get());
 }
 
-static gboolean writeNextChunkIdle(SoupMessage* message)
-{
-    soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, "chunk", 5);
-    return FALSE;
-}
-
 static void writeNextChunk(SoupMessage* message)
 {
-    g_timeout_add(100, reinterpret_cast<GSourceFunc>(writeNextChunkIdle), message);
+    /* We need a big enough chunk for the sniffer to not block the load */
+    static const char* chunk = "Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!"
+        "Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!Testing!";
+    soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, chunk, strlen(chunk));
 }
 
 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
@@ -417,6 +419,9 @@
         return;
     }
 
+    if (g_str_equal(path, "/unknown"))
+        path = "/test.pdf";
+
     GUniquePtr<char> filePath(g_build_filename(Test::getResourcesDir().data(), path, nullptr));
     char* contents;
     gsize contentsLength;
@@ -612,6 +617,33 @@
     test->cancelDownloadAndWaitUntilFinished();
 }
 
+static void testDownloadMIMEType(DownloadTest* test, gconstpointer)
+{
+    GRefPtr<WebKitDownload> download = adoptGRef(test->downloadURIAndWaitUntilFinishes(kServer->getURIForPath("/unknown")));
+    g_assert(!webkit_download_get_web_view(download.get()));
+
+    Vector<DownloadTest::DownloadEvent>& events = test->m_downloadEvents;
+    g_assert_cmpint(events.size(), ==, 5);
+    g_assert_cmpint(events[0], ==, DownloadTest::Started);
+    g_assert_cmpint(events[1], ==, DownloadTest::ReceivedResponse);
+    g_assert_cmpint(events[2], ==, DownloadTest::CreatedDestination);
+    g_assert_cmpint(events[3], ==, DownloadTest::ReceivedData);
+    g_assert_cmpint(events[4], ==, DownloadTest::Finished);
+    events.clear();
+
+    WebKitURIRequest* request = webkit_download_get_request(download.get());
+    WEBKIT_IS_URI_REQUEST(request);
+    ASSERT_CMP_CSTRING(webkit_uri_request_get_uri(request), ==, kServer->getURIForPath("/unknown"));
+
+    WebKitURIResponse* response = webkit_download_get_response(download.get());
+    WEBKIT_IS_URI_RESPONSE(response);
+    g_assert_cmpstr(webkit_uri_response_get_mime_type(response), ==, "application/pdf");
+
+    g_assert(webkit_download_get_destination(download.get()));
+    g_assert_cmpfloat(webkit_download_get_estimated_progress(download.get()), ==, 1);
+    test->checkDestinationAndDeleteFile(download.get(), kServerSuggestedFilename);
+}
+
 void beforeAll()
 {
     kServer = new WebKitTestServer();
@@ -625,6 +657,7 @@
     DownloadErrorTest::add("Downloads", "remote-file-error", testDownloadRemoteFileError);
     WebViewDownloadTest::add("WebKitWebView", "download-uri", testWebViewDownloadURI);
     PolicyResponseDownloadTest::add("Downloads", "policy-decision-download", testPolicyResponseDownload);
+    DownloadTest::add("Downloads", "mime-type", testDownloadMIMEType);
 }
 
 void afterAll()
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to