From 35cc8801ac512c90510e7b391e795007941fb15d Mon Sep 17 00:00:00 2001 From: mmenke Date: Wed, 6 Jan 2016 09:28:43 -0800 Subject: [PATCH] Fix naming of http://*.tgz?* downloads without a content-disposition. They were being renamed *.gz when they had a gzip mime type, on platforms with a default file extension for gzips. BUG=136305 Review URL: https://codereview.chromium.org/1560203002 Cr-Commit-Position: refs/heads/master@{#367848} --- net/base/filename_util_internal.cc | 6 +- net/base/filename_util_unittest.cc | 11 +++ net/base/mime_util.cc | 126 ++++++++++++++--------------- 3 files changed, 74 insertions(+), 69 deletions(-) diff --git a/net/base/filename_util_internal.cc b/net/base/filename_util_internal.cc index cdfb03c7a2e57b..2f038ad4028028 100644 --- a/net/base/filename_util_internal.cc +++ b/net/base/filename_util_internal.cc @@ -43,12 +43,10 @@ base::FilePath::StringType GetCorrectedExtensionUnsafe( if (!extension.empty() && !ignore_extension) return extension; - base::FilePath::StringType preferred_mime_extension; - GetPreferredExtensionForMimeType(mime_type, &preferred_mime_extension); - // Don't do anything if there's not a preferred extension for the mime // type. - if (preferred_mime_extension.empty()) + base::FilePath::StringType preferred_mime_extension; + if (!GetPreferredExtensionForMimeType(mime_type, &preferred_mime_extension)) return extension; // If the existing extension is in the list of valid extensions for the diff --git a/net/base/filename_util_unittest.cc b/net/base/filename_util_unittest.cc index e8db4a15a51246..5e3fb7e1c9f91f 100644 --- a/net/base/filename_util_unittest.cc +++ b/net/base/filename_util_unittest.cc @@ -1026,8 +1026,19 @@ TEST(FilenameUtilTest, GenerateFileName) { "image/jpeg", L"download", L"image" JPEG_EXT}, {__LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", " .foo", "", L"download", L"-.foo"}, + + // Note that the next 4 tests will not fail on all platforms on regression. + // They only fail if application/[x-]gzip has a default extension, which + // can vary across platforms (And even by OS install). {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "", "", "application/gzip", L"", L"goat.tar.gz"}, + {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "", + "", "application/x-gzip", L"", L"goat.tar.gz"}, + {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "", + "application/gzip", L"", L"goat.tgz"}, + {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "", + "application/x-gzip", L"", L"goat.tgz"}, + #if defined(OS_CHROMEOS) {// http://crosbug.com/26028 __LINE__, "http://www.example.com/fooa%cc%88.txt", "", "", "", diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc index 0d511201fd0cdf..d1e202b5251ec0 100644 --- a/net/base/mime_util.cc +++ b/net/base/mime_util.cc @@ -59,54 +59,54 @@ class MimeUtil : public PlatformMimeUtil { static base::LazyInstance::Leaky g_mime_util = LAZY_INSTANCE_INITIALIZER; -static const MimeInfo primary_mappings[] = { - { "text/html", "html,htm,shtml,shtm" }, - { "text/css", "css" }, - { "text/xml", "xml" }, - { "image/gif", "gif" }, - { "image/jpeg", "jpeg,jpg" }, - { "image/webp", "webp" }, - { "image/png", "png" }, - { "video/mp4", "mp4,m4v" }, - { "audio/x-m4a", "m4a" }, - { "audio/mp3", "mp3" }, - { "video/ogg", "ogv,ogm" }, - { "audio/ogg", "ogg,oga,opus" }, - { "video/webm", "webm" }, - { "audio/webm", "webm" }, - { "audio/wav", "wav" }, - { "audio/flac", "flac" }, - { "application/xhtml+xml", "xhtml,xht,xhtm" }, - { "application/x-chrome-extension", "crx" }, - { "multipart/related", "mhtml,mht" } -}; - -static const MimeInfo secondary_mappings[] = { - { "application/octet-stream", "exe,com,bin" }, - { "application/gzip", "gz" }, - { "application/pdf", "pdf" }, - { "application/postscript", "ps,eps,ai" }, - { "application/javascript", "js" }, - { "application/font-woff", "woff" }, - { "image/bmp", "bmp" }, - { "image/x-icon", "ico" }, - { "image/vnd.microsoft.icon", "ico" }, - { "image/jpeg", "jfif,pjpeg,pjp" }, - { "image/tiff", "tiff,tif" }, - { "image/x-xbitmap", "xbm" }, - { "image/svg+xml", "svg,svgz" }, - { "image/x-png", "png"}, - { "message/rfc822", "eml" }, - { "text/plain", "txt,text" }, - { "text/html", "ehtml" }, - { "application/rss+xml", "rss" }, - { "application/rdf+xml", "rdf" }, - { "text/xml", "xsl,xbl,xslt" }, - { "application/vnd.mozilla.xul+xml", "xul" }, - { "application/x-shockwave-flash", "swf,swl" }, - { "application/pkcs7-mime", "p7m,p7c,p7z" }, - { "application/pkcs7-signature", "p7s" }, - { "application/x-mpegurl", "m3u8" }, +static const MimeInfo kPrimaryMappings[] = { + {"text/html", "html,htm,shtml,shtm"}, + {"text/css", "css"}, + {"text/xml", "xml"}, + {"image/gif", "gif"}, + {"image/jpeg", "jpeg,jpg"}, + {"image/webp", "webp"}, + {"image/png", "png"}, + {"video/mp4", "mp4,m4v"}, + {"audio/x-m4a", "m4a"}, + {"audio/mp3", "mp3"}, + {"video/ogg", "ogv,ogm"}, + {"audio/ogg", "ogg,oga,opus"}, + {"video/webm", "webm"}, + {"audio/webm", "webm"}, + {"audio/wav", "wav"}, + {"audio/flac", "flac"}, + {"application/xhtml+xml", "xhtml,xht,xhtm"}, + {"application/x-chrome-extension", "crx"}, + {"multipart/related", "mhtml,mht"}}; + +static const MimeInfo kSecondaryMappings[] = { + {"application/octet-stream", "exe,com,bin"}, + {"application/gzip", "gz,tgz"}, + {"application/x-gzip", "gz,tgz"}, + {"application/pdf", "pdf"}, + {"application/postscript", "ps,eps,ai"}, + {"application/javascript", "js"}, + {"application/font-woff", "woff"}, + {"image/bmp", "bmp"}, + {"image/x-icon", "ico"}, + {"image/vnd.microsoft.icon", "ico"}, + {"image/jpeg", "jfif,pjpeg,pjp"}, + {"image/tiff", "tiff,tif"}, + {"image/x-xbitmap", "xbm"}, + {"image/svg+xml", "svg,svgz"}, + {"image/x-png", "png"}, + {"message/rfc822", "eml"}, + {"text/plain", "txt,text"}, + {"text/html", "ehtml"}, + {"application/rss+xml", "rss"}, + {"application/rdf+xml", "rdf"}, + {"text/xml", "xsl,xbl,xslt"}, + {"application/vnd.mozilla.xul+xml", "xul"}, + {"application/x-shockwave-flash", "swf,swl"}, + {"application/pkcs7-mime", "p7m,p7c,p7z"}, + {"application/pkcs7-signature", "p7s"}, + {"application/x-mpegurl", "m3u8"}, }; const char* FindMimeType(const MimeInfo* mappings, @@ -174,7 +174,7 @@ bool MimeUtil::GetMimeTypeFromExtensionHelper( base::FilePath path_ext(ext); const string ext_narrow_str = path_ext.AsUTF8Unsafe(); const char* mime_type = FindMimeType( - primary_mappings, arraysize(primary_mappings), ext_narrow_str); + kPrimaryMappings, arraysize(kPrimaryMappings), ext_narrow_str); if (mime_type) { *result = mime_type; return true; @@ -183,7 +183,7 @@ bool MimeUtil::GetMimeTypeFromExtensionHelper( if (include_platform_types && GetPlatformMimeTypeFromExtension(ext, result)) return true; - mime_type = FindMimeType(secondary_mappings, arraysize(secondary_mappings), + mime_type = FindMimeType(kSecondaryMappings, arraysize(kSecondaryMappings), ext_narrow_str); if (mime_type) { *result = mime_type; @@ -490,15 +490,13 @@ void GetExtensionsHelper( // Also look up the extensions from hard-coded mappings in case that some // supported extensions are not registered in the system registry, like ogg. - GetExtensionsFromHardCodedMappings(primary_mappings, - arraysize(primary_mappings), - leading_mime_type, - extensions); - - GetExtensionsFromHardCodedMappings(secondary_mappings, - arraysize(secondary_mappings), - leading_mime_type, - extensions); + GetExtensionsFromHardCodedMappings(kPrimaryMappings, + arraysize(kPrimaryMappings), + leading_mime_type, extensions); + + GetExtensionsFromHardCodedMappings(kSecondaryMappings, + arraysize(kSecondaryMappings), + leading_mime_type, extensions); } // Note that the elements in the source set will be appended to the target @@ -547,14 +545,12 @@ void GetExtensionsForMimeType( // Also look up the extensions from hard-coded mappings in case that some // supported extensions are not registered in the system registry, like ogg. - GetExtensionsFromHardCodedMappings(primary_mappings, - arraysize(primary_mappings), - mime_type, + GetExtensionsFromHardCodedMappings(kPrimaryMappings, + arraysize(kPrimaryMappings), mime_type, &unique_extensions); - GetExtensionsFromHardCodedMappings(secondary_mappings, - arraysize(secondary_mappings), - mime_type, + GetExtensionsFromHardCodedMappings(kSecondaryMappings, + arraysize(kSecondaryMappings), mime_type, &unique_extensions); }