MIMETypeRegistry.cpp [plain text]
#include "config.h"
#include "MIMETypeRegistry.h"
#include "MediaPlayer.h"
#include <wtf/HashMap.h>
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#if USE(CG)
#include "ImageSourceCG.h"
#include "UTIRegistry.h"
#include <wtf/RetainPtr.h>
#endif
#if USE(CG) && PLATFORM(COCOA)
#include "UTIUtilities.h"
#endif
#if USE(CG) && !PLATFORM(IOS)
#include <ApplicationServices/ApplicationServices.h>
#endif
#if PLATFORM(IOS)
#include <ImageIO/CGImageDestination.h>
#endif
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
#include "ArchiveFactory.h"
#endif
#if HAVE(AVASSETREADER)
#include "ContentType.h"
#include "ImageDecoderAVFObjC.h"
#endif
namespace WebCore {
static HashSet<String, ASCIICaseInsensitiveHash>* supportedImageResourceMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* supportedImageMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* supportedImageMIMETypesForEncoding;
static HashSet<String, ASCIICaseInsensitiveHash>* supportedJavaScriptMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* supportedNonImageMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* supportedMediaMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* pdfMIMETypes;
static HashSet<String, ASCIICaseInsensitiveHash>* unsupportedTextMIMETypes;
static void initializeSupportedImageMIMETypes()
{
supportedImageResourceMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
supportedImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
#if USE(CG)
static const char* const allowedImageMIMETypes[] = { "image/tiff", "image/gif", "image/jpeg", "image/vnd.microsoft.icon", "image/jp2", "image/png", "image/bmp" };
for (auto& mimeType : allowedImageMIMETypes) {
supportedImageMIMETypes->add(ASCIILiteral { mimeType });
supportedImageResourceMIMETypes->add(ASCIILiteral { mimeType });
}
#ifndef NDEBUG
for (auto& uti : allowedImageUTIs()) {
auto mimeType = MIMETypeForImageSourceType(uti);
if (!mimeType.isEmpty()) {
ASSERT(supportedImageMIMETypes->contains(mimeType));
ASSERT(supportedImageResourceMIMETypes->contains(mimeType));
}
}
#if PLATFORM(COCOA)
for (auto& mime : *supportedImageMIMETypes)
ASSERT_UNUSED(mime, allowedImageUTIs().contains(UTIFromMIMEType(mime)));
#endif
#endif
supportedImageMIMETypes->add("image/x-icon");
supportedImageResourceMIMETypes->add("image/x-icon");
supportedImageMIMETypes->add("image/pjpeg");
supportedImageResourceMIMETypes->add("image/pjpeg");
#if PLATFORM(IOS)
static const char* const malformedMIMETypes[] = {
"image/jpg", "image/jp_", "image/jpe_", "application/jpg", "application/x-jpg", "image/pipeg",
"image/vnd.switfview-jpeg", "image/x-xbitmap",
"image/gi_",
"application/png", "application/x-png",
"image/x-tif", "image/tif", "image/x-tiff", "application/tif", "application/x-tif", "application/tiff",
"application/x-tiff",
"image/x-bmp", "image/x-win-bitmap", "image/x-windows-bmp", "image/ms-bmp", "image/x-ms-bmp",
"application/bmp", "application/x-bmp", "application/x-win-bitmap",
};
for (auto& type : malformedMIMETypes) {
supportedImageMIMETypes->add(type);
supportedImageResourceMIMETypes->add(type);
}
#endif
#else
static const char* const types[] = {
"image/jpeg",
"image/png",
"image/gif",
"image/bmp",
"image/vnd.microsoft.icon", "image/x-icon", "image/x-xbitmap" };
for (auto& type : types) {
supportedImageMIMETypes->add(type);
supportedImageResourceMIMETypes->add(type);
}
#if USE(WEBP)
supportedImageMIMETypes->add("image/webp");
supportedImageResourceMIMETypes->add("image/webp");
#endif
#endif // USE(CG)
}
static void initializeSupportedImageMIMETypesForEncoding()
{
supportedImageMIMETypesForEncoding = new HashSet<String, ASCIICaseInsensitiveHash>;
#if USE(CG)
#if PLATFORM(COCOA)
RetainPtr<CFArrayRef> supportedTypes = adoptCF(CGImageDestinationCopyTypeIdentifiers());
CFIndex count = CFArrayGetCount(supportedTypes.get());
for (CFIndex i = 0; i < count; i++) {
CFStringRef supportedType = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i));
String mimeType = MIMETypeForImageSourceType(supportedType);
if (!mimeType.isEmpty())
supportedImageMIMETypesForEncoding->add(mimeType);
}
#else
supportedImageMIMETypesForEncoding->add("image/png");
supportedImageMIMETypesForEncoding->add("image/jpeg");
supportedImageMIMETypesForEncoding->add("image/gif");
#endif
#elif PLATFORM(GTK)
supportedImageMIMETypesForEncoding->add("image/png");
supportedImageMIMETypesForEncoding->add("image/jpeg");
supportedImageMIMETypesForEncoding->add("image/tiff");
supportedImageMIMETypesForEncoding->add("image/bmp");
supportedImageMIMETypesForEncoding->add("image/ico");
#elif USE(CAIRO)
supportedImageMIMETypesForEncoding->add("image/png");
#endif
}
static void initializeSupportedJavaScriptMIMETypes()
{
static const char* const types[] = {
"text/javascript",
"text/ecmascript",
"application/javascript",
"application/ecmascript",
"application/x-javascript",
"application/x-ecmascript",
"text/javascript1.0",
"text/javascript1.1",
"text/javascript1.2",
"text/javascript1.3",
"text/javascript1.4",
"text/javascript1.5",
"text/jscript",
"text/livescript",
"text/x-javascript",
"text/x-ecmascript"
};
supportedJavaScriptMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
for (auto* type : types)
supportedJavaScriptMIMETypes->add(type);
}
static void initializePDFMIMETypes()
{
const char* const types[] = {
"application/pdf",
"text/pdf"
};
pdfMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
for (auto& type : types)
pdfMIMETypes->add(type);
}
static void initializeSupportedNonImageMimeTypes()
{
static const char* const types[] = {
"text/html",
"text/xml",
"text/xsl",
"text/plain",
"text/",
"application/xml",
"application/xhtml+xml",
#if !PLATFORM(IOS)
"application/vnd.wap.xhtml+xml",
"application/rss+xml",
"application/atom+xml",
#endif
"application/json",
"image/svg+xml",
#if ENABLE(FTPDIR)
"application/x-ftp-directory",
#endif
"multipart/x-mixed-replace"
};
if (!supportedJavaScriptMIMETypes)
initializeSupportedJavaScriptMIMETypes();
supportedNonImageMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash> { *supportedJavaScriptMIMETypes };
for (auto& type : types)
supportedNonImageMIMETypes->add(type);
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
ArchiveFactory::registerKnownArchiveMIMETypes();
#endif
}
static const Vector<String>* typesForCommonExtension(const String& extension)
{
static const auto map = makeNeverDestroyed([] {
struct TypeExtensionPair {
const char* type;
const char* extension;
};
static const TypeExtensionPair commonMediaTypes[] = {
{ "application/ogg", "ogx" },
{ "audio/ogg", "ogg" },
{ "audio/ogg", "oga" },
{ "video/ogg", "ogv" },
{ "application/annodex", "anx" },
{ "audio/annodex", "axa" },
{ "video/annodex", "axv" },
{ "audio/speex", "spx" },
{ "video/webm", "webm" },
{ "audio/webm", "webm" },
{ "audio/mpeg", "m1a" },
{ "audio/mpeg", "m2a" },
{ "audio/mpeg", "m1s" },
{ "audio/mpeg", "mpa" },
{ "video/mpeg", "mpg" },
{ "video/mpeg", "m15" },
{ "video/mpeg", "m1s" },
{ "video/mpeg", "m1v" },
{ "video/mpeg", "m75" },
{ "video/mpeg", "mpa" },
{ "video/mpeg", "mpeg" },
{ "video/mpeg", "mpm" },
{ "video/mpeg", "mpv" },
{ "application/vnd.apple.mpegurl", "m3u8" },
{ "application/mpegurl", "m3u8" },
{ "application/x-mpegurl", "m3u8" },
{ "audio/mpegurl", "m3url" },
{ "audio/x-mpegurl", "m3url" },
{ "audio/mpegurl", "m3u" },
{ "audio/x-mpegurl", "m3u" },
{ "video/x-m4v", "m4v" },
{ "audio/x-m4a", "m4a" },
{ "audio/x-m4b", "m4b" },
{ "audio/x-m4p", "m4p" },
{ "audio/mp4", "m4a" },
{ "audio/mp3", "mp3" },
{ "audio/x-mp3", "mp3" },
{ "audio/x-mpeg", "mp3" },
{ "video/x-mpeg2", "mp2" },
{ "video/mpeg2", "vob" },
{ "video/mpeg2", "mod" },
{ "video/m2ts", "m2ts" },
{ "video/x-m2ts", "m2t" },
{ "video/x-m2ts", "ts" },
{ "audio/3gpp", "3gpp" },
{ "audio/3gpp2", "3g2" },
{ "application/x-mpeg", "amc" },
{ "audio/aac", "aac" },
{ "audio/aac", "adts" },
{ "audio/x-aac", "m4r" },
{ "audio/x-caf", "caf" },
{ "audio/x-gsm", "gsm" },
{ "audio/x-wav", "wav" },
{ "audio/vnd.wave", "wav" },
};
HashMap<String, Vector<String>, ASCIICaseInsensitiveHash> map;
for (auto& pair : commonMediaTypes) {
const char* type = pair.type;
const char* extension = pair.extension;
map.ensure(ASCIILiteral { extension }, [type, extension] {
Vector<String> synonyms;
String systemType = MIMETypeRegistry::getMIMETypeForExtension(extension);
if (!systemType.isEmpty() && type != systemType)
synonyms.append(systemType);
return synonyms;
}).iterator->value.append(ASCIILiteral { type });
}
return map;
}());
auto mapEntry = map.get().find(extension);
if (mapEntry == map.get().end())
return nullptr;
return &mapEntry->value;
}
String MIMETypeRegistry::getMediaMIMETypeForExtension(const String& extension)
{
auto* vector = typesForCommonExtension(extension);
if (vector)
return (*vector)[0];
return getMIMETypeForExtension(extension);
}
Vector<String> MIMETypeRegistry::getMediaMIMETypesForExtension(const String& extension)
{
auto* vector = typesForCommonExtension(extension);
if (vector)
return *vector;
String type = getMIMETypeForExtension(extension);
if (!type.isNull())
return { { type } };
return { };
}
static void initializeSupportedMediaMIMETypes()
{
supportedMediaMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
#if ENABLE(VIDEO)
MediaPlayer::getSupportedTypes(*supportedMediaMIMETypes);
#endif
}
static void initializeUnsupportedTextMIMETypes()
{
static const char* const types[] = {
"text/calendar",
"text/x-calendar",
"text/x-vcalendar",
"text/vcalendar",
"text/vcard",
"text/x-vcard",
"text/directory",
"text/ldif",
"text/qif",
"text/x-qif",
"text/x-csv",
"text/x-vcf",
#if !PLATFORM(IOS)
"text/rtf",
#else
"text/vnd.sun.j2me.app-descriptor",
#endif
};
unsupportedTextMIMETypes = new HashSet<String, ASCIICaseInsensitiveHash>;
for (auto& type : types)
unsupportedTextMIMETypes->add(ASCIILiteral { type });
}
String MIMETypeRegistry::getMIMETypeForPath(const String& path)
{
size_t pos = path.reverseFind('.');
if (pos != notFound) {
String extension = path.substring(pos + 1);
String result = getMIMETypeForExtension(extension);
if (result.length())
return result;
}
return defaultMIMEType();
}
bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedImageMIMETypes)
initializeSupportedImageMIMETypes();
return supportedImageMIMETypes->contains(getNormalizedMIMEType(mimeType));
}
bool MIMETypeRegistry::isSupportedImageVideoOrSVGMIMEType(const String& mimeType)
{
if (isSupportedImageMIMEType(mimeType) || equalLettersIgnoringASCIICase(mimeType, "image/svg+xml"))
return true;
#if HAVE(AVASSETREADER)
if (ImageDecoderAVFObjC::supportsContentType(ContentType(mimeType)))
return true;
#endif
return false;
}
bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedImageResourceMIMETypes)
initializeSupportedImageMIMETypes();
return supportedImageResourceMIMETypes->contains(getNormalizedMIMEType(mimeType));
}
bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
{
ASSERT(isMainThread());
if (mimeType.isEmpty())
return false;
if (!supportedImageMIMETypesForEncoding)
initializeSupportedImageMIMETypesForEncoding();
return supportedImageMIMETypesForEncoding->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!isMainThread()) {
bool isSupported = false;
callOnMainThreadAndWait([&isSupported, mimeType = mimeType.isolatedCopy()] {
isSupported = isSupportedJavaScriptMIMEType(mimeType);
});
return isSupported;
}
if (!supportedJavaScriptMIMETypes)
initializeSupportedNonImageMimeTypes();
return supportedJavaScriptMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedStyleSheetMIMEType(const String& mimeType)
{
return equalLettersIgnoringASCIICase(mimeType, "text/css");
}
bool MIMETypeRegistry::isSupportedFontMIMEType(const String& mimeType)
{
static const unsigned fontLength = 5;
if (!startsWithLettersIgnoringASCIICase(mimeType, "font/"))
return false;
auto subtype = StringView { mimeType }.substring(fontLength);
return equalLettersIgnoringASCIICase(subtype, "woff")
|| equalLettersIgnoringASCIICase(subtype, "woff2")
|| equalLettersIgnoringASCIICase(subtype, "otf")
|| equalLettersIgnoringASCIICase(subtype, "ttf")
|| equalLettersIgnoringASCIICase(subtype, "sfnt");
}
bool MIMETypeRegistry::isSupportedJSONMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (equalLettersIgnoringASCIICase(mimeType, "application/json"))
return true;
if (mimeType.endsWithIgnoringASCIICase("+json") && mimeType.length() >= 8) {
size_t slashPosition = mimeType.find('/');
if (slashPosition != notFound && slashPosition > 0 && slashPosition <= mimeType.length() - 6)
return true;
}
return false;
}
bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedNonImageMIMETypes)
initializeSupportedNonImageMimeTypes();
return supportedNonImageMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedMediaMIMETypes)
initializeSupportedMediaMIMETypes();
return supportedMediaMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedTextTrackMIMEType(const String& mimeType)
{
return equalLettersIgnoringASCIICase(mimeType, "text/vtt");
}
bool MIMETypeRegistry::isUnsupportedTextMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!unsupportedTextMIMETypes)
initializeUnsupportedTextMIMETypes();
return unsupportedTextMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isTextMIMEType(const String& mimeType)
{
return isSupportedJavaScriptMIMEType(mimeType)
|| isSupportedJSONMIMEType(mimeType) || (startsWithLettersIgnoringASCIICase(mimeType, "text/")
&& !equalLettersIgnoringASCIICase(mimeType, "text/html")
&& !equalLettersIgnoringASCIICase(mimeType, "text/xml")
&& !equalLettersIgnoringASCIICase(mimeType, "text/xsl"));
}
static inline bool isValidXMLMIMETypeChar(UChar c)
{
return isASCIIAlphanumeric(c) || c == '!' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' || c == '*' || c == '+'
|| c == '-' || c == '.' || c == '^' || c == '_' || c == '`' || c == '{' || c == '|' || c == '}' || c == '~';
}
bool MIMETypeRegistry::isXMLMIMEType(const String& mimeType)
{
if (equalLettersIgnoringASCIICase(mimeType, "text/xml") || equalLettersIgnoringASCIICase(mimeType, "application/xml") || equalLettersIgnoringASCIICase(mimeType, "text/xsl"))
return true;
if (!mimeType.endsWithIgnoringASCIICase("+xml"))
return false;
size_t slashPosition = mimeType.find('/');
if (slashPosition == notFound || !slashPosition || slashPosition == mimeType.length() - 5)
return false;
size_t mimeLength = mimeType.length();
for (size_t i = 0; i < mimeLength - 4; ++i) {
if (!isValidXMLMIMETypeChar(mimeType[i]) && i != slashPosition)
return false;
}
return true;
}
bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
{
return startsWithLettersIgnoringASCIICase(mimeType, "application/x-java-applet")
|| startsWithLettersIgnoringASCIICase(mimeType, "application/x-java-bean")
|| startsWithLettersIgnoringASCIICase(mimeType, "application/x-java-vm");
}
bool MIMETypeRegistry::isPDFMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!pdfMIMETypes)
initializePDFMIMETypes();
return pdfMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isPostScriptMIMEType(const String& mimeType)
{
return mimeType == "application/postscript";
}
bool MIMETypeRegistry::isPDFOrPostScriptMIMEType(const String& mimeType)
{
return isPDFMIMEType(mimeType) || isPostScriptMIMEType(mimeType);
}
bool MIMETypeRegistry::canShowMIMEType(const String& mimeType)
{
if (isSupportedImageMIMEType(mimeType) || isSupportedNonImageMIMEType(mimeType) || isSupportedMediaMIMEType(mimeType))
return true;
if (isSupportedJavaScriptMIMEType(mimeType) || isSupportedJSONMIMEType(mimeType))
return true;
if (startsWithLettersIgnoringASCIICase(mimeType, "text/"))
return !isUnsupportedTextMIMEType(mimeType);
return false;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getSupportedImageMIMETypes()
{
if (!supportedImageMIMETypes)
initializeSupportedImageMIMETypes();
return *supportedImageMIMETypes;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getSupportedImageResourceMIMETypes()
{
if (!supportedImageResourceMIMETypes)
initializeSupportedImageMIMETypes();
return *supportedImageResourceMIMETypes;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding()
{
if (!supportedImageMIMETypesForEncoding)
initializeSupportedImageMIMETypesForEncoding();
return *supportedImageMIMETypesForEncoding;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getSupportedNonImageMIMETypes()
{
if (!supportedNonImageMIMETypes)
initializeSupportedNonImageMimeTypes();
return *supportedNonImageMIMETypes;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getSupportedMediaMIMETypes()
{
if (!supportedMediaMIMETypes)
initializeSupportedMediaMIMETypes();
return *supportedMediaMIMETypes;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getPDFMIMETypes()
{
if (!pdfMIMETypes)
initializePDFMIMETypes();
return *pdfMIMETypes;
}
HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::getUnsupportedTextMIMETypes()
{
if (!unsupportedTextMIMETypes)
initializeUnsupportedTextMIMETypes();
return *unsupportedTextMIMETypes;
}
const String& defaultMIMEType()
{
static NeverDestroyed<const String> defaultMIMEType(MAKE_STATIC_STRING_IMPL("application/octet-stream"));
return defaultMIMEType;
}
#if !USE(CURL)
String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeType)
{
return mimeType;
}
#else
typedef HashMap<String, String, ASCIICaseInsensitiveHash> MIMETypeAssociationMap;
static const MIMETypeAssociationMap& mimeTypeAssociationMap()
{
static MIMETypeAssociationMap* mimeTypeMap = 0;
if (mimeTypeMap)
return *mimeTypeMap;
mimeTypeMap = new MIMETypeAssociationMap;
mimeTypeMap->add(ASCIILiteral("image/x-ms-bmp"), ASCIILiteral("image/bmp"));
mimeTypeMap->add(ASCIILiteral("image/x-windows-bmp"), ASCIILiteral("image/bmp"));
mimeTypeMap->add(ASCIILiteral("image/x-bmp"), ASCIILiteral("image/bmp"));
mimeTypeMap->add(ASCIILiteral("image/x-bitmap"), ASCIILiteral("image/bmp"));
mimeTypeMap->add(ASCIILiteral("image/x-ms-bitmap"), ASCIILiteral("image/bmp"));
mimeTypeMap->add(ASCIILiteral("image/jpg"), ASCIILiteral("image/jpeg"));
mimeTypeMap->add(ASCIILiteral("image/pjpeg"), ASCIILiteral("image/jpeg"));
mimeTypeMap->add(ASCIILiteral("image/x-png"), ASCIILiteral("image/png"));
mimeTypeMap->add(ASCIILiteral("image/vnd.rim.png"), ASCIILiteral("image/png"));
mimeTypeMap->add(ASCIILiteral("image/ico"), ASCIILiteral("image/vnd.microsoft.icon"));
mimeTypeMap->add(ASCIILiteral("image/icon"), ASCIILiteral("image/vnd.microsoft.icon"));
mimeTypeMap->add(ASCIILiteral("text/ico"), ASCIILiteral("image/vnd.microsoft.icon"));
mimeTypeMap->add(ASCIILiteral("application/ico"), ASCIILiteral("image/vnd.microsoft.icon"));
mimeTypeMap->add(ASCIILiteral("image/x-icon"), ASCIILiteral("image/vnd.microsoft.icon"));
mimeTypeMap->add(ASCIILiteral("audio/vnd.qcelp"), ASCIILiteral("audio/qcelp"));
mimeTypeMap->add(ASCIILiteral("audio/qcp"), ASCIILiteral("audio/qcelp"));
mimeTypeMap->add(ASCIILiteral("audio/vnd.qcp"), ASCIILiteral("audio/qcelp"));
mimeTypeMap->add(ASCIILiteral("audio/wav"), ASCIILiteral("audio/x-wav"));
mimeTypeMap->add(ASCIILiteral("audio/vnd.wave"), ASCIILiteral("audio/x-wav"));
mimeTypeMap->add(ASCIILiteral("audio/mid"), ASCIILiteral("audio/midi"));
mimeTypeMap->add(ASCIILiteral("audio/sp-midi"), ASCIILiteral("audio/midi"));
mimeTypeMap->add(ASCIILiteral("audio/x-mid"), ASCIILiteral("audio/midi"));
mimeTypeMap->add(ASCIILiteral("audio/x-midi"), ASCIILiteral("audio/midi"));
mimeTypeMap->add(ASCIILiteral("audio/x-mpeg"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/mp3"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/x-mp3"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/mpeg3"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/x-mpeg3"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/mpg3"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/mpg"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/x-mpg"), ASCIILiteral("audio/mpeg"));
mimeTypeMap->add(ASCIILiteral("audio/m4a"), ASCIILiteral("audio/mp4"));
mimeTypeMap->add(ASCIILiteral("audio/x-m4a"), ASCIILiteral("audio/mp4"));
mimeTypeMap->add(ASCIILiteral("audio/x-mp4"), ASCIILiteral("audio/mp4"));
mimeTypeMap->add(ASCIILiteral("audio/x-aac"), ASCIILiteral("audio/aac"));
mimeTypeMap->add(ASCIILiteral("audio/x-amr"), ASCIILiteral("audio/amr"));
mimeTypeMap->add(ASCIILiteral("audio/mpegurl"), ASCIILiteral("audio/x-mpegurl"));
mimeTypeMap->add(ASCIILiteral("audio/flac"), ASCIILiteral("audio/x-flac"));
mimeTypeMap->add(ASCIILiteral("video/3gp"), ASCIILiteral("video/3gpp"));
mimeTypeMap->add(ASCIILiteral("video/avi"), ASCIILiteral("video/x-msvideo"));
mimeTypeMap->add(ASCIILiteral("video/x-m4v"), ASCIILiteral("video/mp4"));
mimeTypeMap->add(ASCIILiteral("video/x-quicktime"), ASCIILiteral("video/quicktime"));
mimeTypeMap->add(ASCIILiteral("application/java"), ASCIILiteral("application/java-archive"));
mimeTypeMap->add(ASCIILiteral("application/x-java-archive"), ASCIILiteral("application/java-archive"));
mimeTypeMap->add(ASCIILiteral("application/x-zip-compressed"), ASCIILiteral("application/zip"));
mimeTypeMap->add(ASCIILiteral("text/cache-manifest"), ASCIILiteral("text/plain"));
return *mimeTypeMap;
}
String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeType)
{
auto it = mimeTypeAssociationMap().find(mimeType);
if (it != mimeTypeAssociationMap().end())
return it->value;
return mimeType;
}
#endif
String MIMETypeRegistry::appendFileExtensionIfNecessary(const String& filename, const String& mimeType)
{
if (filename.isEmpty())
return emptyString();
if (filename.reverseFind('.') != notFound)
return filename;
String preferredExtension = getPreferredExtensionForMIMEType(mimeType);
if (preferredExtension.isEmpty())
return filename;
return filename + "." + preferredExtension;
}
}