diff options
author | Iain Merrick <husky@google.com> | 2011-01-06 16:31:24 +0000 |
---|---|---|
committer | Iain Merrick <husky@google.com> | 2011-01-07 12:27:33 +0000 |
commit | fb57cd2392293fed70fd4a7aebabb5b52ef5c41e (patch) | |
tree | ed4d1b8b2fcbe643c77675f937f56635f51f95a2 /WebKit/android/WebCoreSupport | |
parent | 21d8d81a756ca7e60b5131e5f1006f52799179b0 (diff) | |
download | external_webkit-fb57cd2392293fed70fd4a7aebabb5b52ef5c41e.zip external_webkit-fb57cd2392293fed70fd4a7aebabb5b52ef5c41e.tar.gz external_webkit-fb57cd2392293fed70fd4a7aebabb5b52ef5c41e.tar.bz2 |
Fix MIME type handling for badly-behaved servers.
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
Diffstat (limited to 'WebKit/android/WebCoreSupport')
-rw-r--r-- | WebKit/android/WebCoreSupport/WebResponse.cpp | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/WebKit/android/WebCoreSupport/WebResponse.cpp b/WebKit/android/WebCoreSupport/WebResponse.cpp index e564215..113fb05 100644 --- a/WebKit/android/WebCoreSupport/WebResponse.cpp +++ b/WebKit/android/WebCoreSupport/WebResponse.cpp @@ -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; |