OSDN Git Service

Fix MIME type handling for badly-behaved servers.
authorIain Merrick <husky@google.com>
Thu, 6 Jan 2011 16:31:24 +0000 (16:31 +0000)
committerIain Merrick <husky@google.com>
Fri, 7 Jan 2011 12:27:33 +0000 (12:27 +0000)
This fixes http://scrabb.ly, which is a particularly bad example as
there is no filename and no Content-Type header. This CL duplicates
the behaviour of the Apache HTTP stack by defaulting to text/html.

Bug: 3242012
Change-Id: Id0e131ee880466e1708367612ac0217d43db347b

WebKit/android/WebCoreSupport/WebResponse.cpp

index e564215..113fb05 100644 (file)
@@ -115,18 +115,25 @@ void WebResponse::setUrl(const string& url)
 const string& WebResponse::getMimeType()
 {
     if (!m_mime.length() && m_url.length()) {
-        WTF::String wtfMime(m_url.c_str(), m_url.length());
-        // Need to strip fragment and/or query if present.
-        size_t position = wtfMime.reverseFind('#');
-        if (position != WTF::notFound)
-            wtfMime.truncate(position);
-
-        position = wtfMime.reverseFind('?');
-        if (position != WTF::notFound)
-            wtfMime.truncate(position);
-
-        wtfMime = WebCore::MIMETypeRegistry::getMIMETypeForPath(wtfMime);
-        m_mime = std::string(wtfMime.utf8().data(), wtfMime.length());
+        // Use "text/html" as a default (matching the behaviour of the Apache
+        // HTTP stack -- see guessMimeType() in LoadListener.java).
+        m_mime = "text/html";
+
+        // Try to guess a better MIME type from the URL. We call
+        // getMIMETypeForExtension rather than getMIMETypeForPath because the
+        // latter defaults to "application/octet-stream" on failure.
+        WebCore::KURL kurl(WebCore::ParsedURLString, m_url.c_str());
+        WTF::String path = kurl.path();
+        size_t extensionPos = path.reverseFind('.');
+        if (extensionPos != WTF::notFound) {
+            // We found a file extension.
+            path.remove(0, extensionPos + 1);
+            WTF::String mime = WebCore::MIMETypeRegistry::getMIMETypeForExtension(path);
+            if (!mime.isEmpty()) {
+                // Great, we found a MIME type.
+                m_mime = std::string(mime.utf8().data(), mime.length());
+            }
+        }
     }
 
     return m_mime;