From e95b4a7d9ed67e6d6960e33e794e653aa0bd3887 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Thu, 13 Jan 2011 18:41:41 +0000 Subject: [PATCH] Resolve MIME types for CacheResult in the same way as WebResponse. Change-Id: I972bbdabc8491a94eabd676dd13f09fcccb6ba88 --- WebKit/android/WebCoreSupport/CacheResult.cpp | 10 ++++-- WebKit/android/WebCoreSupport/CacheResult.h | 4 ++- WebKit/android/WebCoreSupport/WebCache.cpp | 2 +- WebKit/android/WebCoreSupport/WebResponse.cpp | 46 +++++++++++++++------------ WebKit/android/WebCoreSupport/WebResponse.h | 2 ++ 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/WebKit/android/WebCoreSupport/CacheResult.cpp b/WebKit/android/WebCoreSupport/CacheResult.cpp index 8193ac1c7..91c0ede9f 100644 --- a/WebKit/android/WebCoreSupport/CacheResult.cpp +++ b/WebKit/android/WebCoreSupport/CacheResult.cpp @@ -26,8 +26,10 @@ #include "config.h" #include "CacheResult.h" +#include "WebResponse.h" #include "WebUrlLoaderClient.h" #include +#include using namespace base; using namespace disk_cache; @@ -47,10 +49,11 @@ enum { kResponseContentIndex }; -CacheResult::CacheResult(disk_cache::Entry* entry) +CacheResult::CacheResult(disk_cache::Entry* entry, String url) : m_entry(entry) , m_onResponseHeadersDoneCallback(this, &CacheResult::onResponseHeadersDone) , m_onReadNextChunkDoneCallback(this, &CacheResult::onReadNextChunkDone) + , m_url(url) { ASSERT(m_entry); } @@ -83,7 +86,10 @@ bool CacheResult::firstResponseHeader(const char* name, String* result, bool all String CacheResult::mimeType() const { string mimeType; - responseHeaders()->GetMimeType(&mimeType); + if (responseHeaders()) + responseHeaders()->GetMimeType(&mimeType); + if (!mimeType.length() && m_url.length()) + mimeType = WebResponse::resolveMimeType(std::string(m_url.utf8().data(), m_url.length())); return String(mimeType.c_str()); } diff --git a/WebKit/android/WebCoreSupport/CacheResult.h b/WebKit/android/WebCoreSupport/CacheResult.h index 02dced232..c39570c5f 100644 --- a/WebKit/android/WebCoreSupport/CacheResult.h +++ b/WebKit/android/WebCoreSupport/CacheResult.h @@ -38,7 +38,7 @@ namespace android { class CacheResult : public base::RefCountedThreadSafe { public: // Takes ownership of the Entry passed to the constructor. - CacheResult(disk_cache::Entry*); + CacheResult(disk_cache::Entry*, String url); ~CacheResult(); int64 contentSize() const; @@ -77,6 +77,8 @@ private: net::CompletionCallbackImpl m_onResponseHeadersDoneCallback; net::CompletionCallbackImpl m_onReadNextChunkDoneCallback; + + String m_url; }; } // namespace android diff --git a/WebKit/android/WebCoreSupport/WebCache.cpp b/WebKit/android/WebCoreSupport/WebCache.cpp index 0b9d15031..be9a700aa 100644 --- a/WebKit/android/WebCoreSupport/WebCache.cpp +++ b/WebKit/android/WebCoreSupport/WebCache.cpp @@ -197,7 +197,7 @@ scoped_refptr WebCache::getCacheResult(String url) if (!m_entry) return 0; - return new CacheResult(m_entry); + return new CacheResult(m_entry, url); } void WebCache::getEntryImpl() diff --git a/WebKit/android/WebCoreSupport/WebResponse.cpp b/WebKit/android/WebCoreSupport/WebResponse.cpp index 113fb05c4..52297b25b 100644 --- a/WebKit/android/WebCoreSupport/WebResponse.cpp +++ b/WebKit/android/WebCoreSupport/WebResponse.cpp @@ -114,31 +114,35 @@ void WebResponse::setUrl(const string& url) // TODO: can we return a WTF::String directly? Need to check all callsites. const string& WebResponse::getMimeType() { - if (!m_mime.length() && m_url.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()); - } - } - } + if (!m_mime.length() && m_url.length()) + m_mime = resolveMimeType(m_url); return m_mime; } +const string WebResponse::resolveMimeType(string url) +{ + // Use "text/html" as a default (matching the behaviour of the Apache + // HTTP stack -- see guessMimeType() in LoadListener.java). + string mimeType = "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, 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. + mimeType = std::string(mime.utf8().data(), mime.length()); + } + } + return mimeType; +} + bool WebResponse::getHeader(const string& header, string* result) const { map::const_iterator iter = m_headerFields.find(header); diff --git a/WebKit/android/WebCoreSupport/WebResponse.h b/WebKit/android/WebCoreSupport/WebResponse.h index 9f7d44812..e30779f04 100644 --- a/WebKit/android/WebCoreSupport/WebResponse.h +++ b/WebKit/android/WebCoreSupport/WebResponse.h @@ -59,6 +59,8 @@ public: WebCore::ResourceResponse createResourceResponse(); WebCore::ResourceError createResourceError(); + static const std::string resolveMimeType(std::string url); + private: net::Error m_error; std::string m_encoding; -- 2.11.0