示例#1
0
const string WebResponse::resolveMimeType(const string& url, const string& old_mime)
{
    // Use "text/html" as a default (matching the behaviour of the Apache
    // HTTP stack -- see guessMimeType() in LoadListener.java).
    string mimeType = old_mime.length() ? old_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, 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);
        // TODO: Should use content-disposition instead of url if it is there
        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;
}