Lines Matching +full:use +full:- +full:base +full:- +full:image
2 // Use of this source code is governed by a BSD-style license that can be
5 #include "net/base/mime_util.h"
14 #include "base/base64.h"
15 #include "base/check_op.h"
16 #include "base/containers/span.h"
17 #include "base/lazy_instance.h"
18 #include "base/rand_util.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_split.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h"
24 #include "net/base/platform_mime_util.h"
34 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
37 bool GetMimeTypeFromFile(const base::FilePath& file_path,
40 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext,
45 base::FilePath::StringType* extension) const;
57 friend struct base::LazyInstanceTraitsBase<MimeUtil>;
61 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext,
67 static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
73 // Comma-separated list of possible extensions for the type. The first
78 // How to use the MIME maps
79 // ------------------------
87 // Use this for mappings that are critical to the web platform. Mappings you
89 // from file extension -> MIME type. Thus file extensions listed here will
94 // Use this for mappings that must exist, but can be overridden by user
109 // 1) MIME type -> file extension. Implemented as
114 // a) As a special case application/octet-stream is mapped to nothing. Web
115 // sites are supposed to use this MIME type to indicate that the content
128 // 2) File extension -> MIME type. Implemented in GetMimeTypeFromExtension().
140 // 3) File extension -> Well known MIME type. Implemented as
156 {"application/x-chrome-extension", "crx"},
163 {"audio/x-m4a", "m4a"},
164 {"image/avif", "avif"},
165 {"image/gif", "gif"},
166 {"image/jpeg", "jpeg,jpg"},
167 {"image/png", "png"},
168 {"image/apng", "png,apng"},
169 {"image/svg+xml", "svg,svgz"},
170 {"image/webp", "webp"},
181 // registers csv as application/vnd.ms-excel instead of text/csv from RFC
188 // Must precede image/vnd.microsoft.icon .
189 {"image/x-icon", "ico"},
192 {"application/font-woff", "woff"},
197 {"application/octet-stream", "bin,exe,com"},
199 {"application/pkcs7-mime", "p7m,p7c,p7z"},
200 {"application/pkcs7-signature", "p7s"},
205 {"application/vnd.android.package-archive", "apk"},
207 {"application/vnd.ms-excel", "xls"},
208 {"application/vnd.ms-powerpoint", "ppt"},
210 "vnd.openxmlformats-officedocument.presentationml.presentation",
212 {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
214 {"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
216 {"application/x-gzip", "gz,tgz"},
217 {"application/x-mpegurl", "m3u8"},
218 {"application/x-shockwave-flash", "swf,swl"},
219 {"application/x-tar", "tar"},
220 {"application/x-x509-ca-cert", "cer,crt"},
224 {"image/bmp", "bmp"},
225 {"image/jpeg", "jfif,pjpeg,pjp"},
226 {"image/tiff", "tiff,tif"},
227 {"image/vnd.microsoft.icon", "ico"},
228 {"image/x-png", "png"},
229 {"image/x-xbitmap", "xbm"},
234 {"text/x-sh", "sh"},
250 base::EqualsCaseInsensitiveASCII( in FindMimeType()
263 static base::FilePath::StringType StringToFilePathStringType( in StringToFilePathStringType()
266 return base::UTF8ToWide(string_piece); in StringToFilePathStringType()
277 base::FilePath::StringType* result) { in FindPreferredExtension()
278 // There is no preferred extension for "application/octet-stream". in FindPreferredExtension()
279 if (mime_type == "application/octet-stream") in FindPreferredExtension()
287 extension_end ? extension_end - extensions : strlen(extensions); in FindPreferredExtension()
295 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType& ext, in GetMimeTypeFromExtension()
301 const base::FilePath::StringType& ext, in GetWellKnownMimeTypeFromExtension()
308 base::FilePath::StringType* extension) const { in GetPreferredExtensionForMimeType()
316 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath& file_path, in GetMimeTypeFromFile()
318 base::FilePath::StringType file_name_str = file_path.Extension(); in GetMimeTypeFromFile()
325 const base::FilePath::StringType& ext, in GetMimeTypeFromExtensionHelper()
337 base::FilePath::StringType::size_type nul_pos = in GetMimeTypeFromExtensionHelper()
339 if (nul_pos != base::FilePath::StringType::npos) in GetMimeTypeFromExtensionHelper()
343 // a mime type. That is, we first check a hard-coded list (that cannot be in GetMimeTypeFromExtensionHelper()
345 // Finally, we scan a secondary hard-coded list to catch types that we can in GetMimeTypeFromExtensionHelper()
348 base::FilePath path_ext(ext); in GetMimeTypeFromExtensionHelper()
374 // According rfc2045 keys of parameters are case-insensitive, while values may
375 // or may not be case-sensitive, but they are usually case-sensitive. So, this
376 // function matches values in *case-sensitive* manner, however note that this
388 base::StringPairs pattern_parameters; in MatchesMimeTypeParameters()
389 base::SplitStringIntoKeyValuePairs(mime_type_pattern.substr(semicolon + 1), in MatchesMimeTypeParameters()
391 base::StringPairs test_parameters; in MatchesMimeTypeParameters()
392 base::SplitStringIntoKeyValuePairs(mime_type.substr(test_semicolon + 1), in MatchesMimeTypeParameters()
398 pattern_parameter_map[base::ToLowerASCII(pair.first)] = pair.second; in MatchesMimeTypeParameters()
403 test_parameter_map[base::ToLowerASCII(pair.first)] = pair.second; in MatchesMimeTypeParameters()
414 if (parameter_pair.second != test_parameter_pair_it->second) in MatchesMimeTypeParameters()
424 // application/x-foo
428 // Also tests mime parameters -- all parameters in the pattern must be present
445 if (base::EqualsCaseInsensitiveASCII(base_pattern, base_type)) in MatchesMimeType()
452 if (base_type.length() < base_pattern.length() - 1) in MatchesMimeType()
459 if (!base::StartsWith(base_type, left, base::CompareCase::INSENSITIVE_ASCII)) in MatchesMimeType()
463 !base::EndsWith(base_type, right, base::CompareCase::INSENSITIVE_ASCII)) in MatchesMimeType()
471 base::StringPairs* params) { in ParseMimeType()
473 // the trailing trim set to catch media-type comments, which are not at all in ParseMimeType()
481 // Reject a mime-type if it does not include a slash. in ParseMimeType()
486 *mime_type = type_str.substr(type_val, type_end - type_val); in ParseMimeType()
491 // validate characters are HTTP token code points / HTTP quoted-string token in ParseMimeType()
494 params->clear(); in ParseMimeType()
514 auto param_name = base::MakeStringPiece(type_str.begin() + param_name_start, in ParseMimeType()
523 // pre-existing behavior. in ParseMimeType()
526 // seems to align more with the spec - not the content-type spec, but the in ParseMimeType()
529 // See https://encoding.spec.whatwg.org/#names-and-labels. in ParseMimeType()
534 // Nothing to do here - an unquoted string of only whitespace should be in ParseMimeType()
548 HttpUtil::IsLWS(type_str[value_end - 1])) { in ParseMimeType()
549 --value_end; in ParseMimeType()
552 param_value = type_str.substr(value_start, value_end - value_start); in ParseMimeType()
555 // a close quote. Do not trim whitespace for quoted-string. in ParseMimeType()
575 params->emplace_back(param_name, param_value); in ParseMimeType()
583 std::vector<std::string_view> components = base::SplitStringPiece( in ParseMimeTypeWithoutParameter()
584 type_string, "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); in ParseMimeTypeWithoutParameter()
587 components[0] = TrimWhitespaceASCII(components[0], base::TRIM_LEADING); in ParseMimeTypeWithoutParameter()
588 components[1] = TrimWhitespaceASCII(components[1], base::TRIM_TRAILING); in ParseMimeTypeWithoutParameter()
593 top_level_type->assign(std::string(components[0])); in ParseMimeTypeWithoutParameter()
596 subtype->assign(std::string(components[1])); in ParseMimeTypeWithoutParameter()
601 // See https://www.iana.org/assignments/media-types/media-types.xhtml
603 "application", "audio", "example", "font", "image",
608 std::string lower_type = base::ToLowerASCII(type_string); in IsValidTopLevelMimeType()
615 base::StartsWith(type_string, "x-", in IsValidTopLevelMimeType()
616 base::CompareCase::INSENSITIVE_ASCII); in IsValidTopLevelMimeType()
619 //----------------------------------------------------------------------------
621 //----------------------------------------------------------------------------
623 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, in GetMimeTypeFromExtension()
628 bool GetMimeTypeFromFile(const base::FilePath& file_path, in GetMimeTypeFromFile()
633 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, in GetWellKnownMimeTypeFromExtension()
639 base::FilePath::StringType* extension) { in GetPreferredExtensionForMimeType()
664 static const char* const kStandardImageTypes[] = {"image/avif",
665 "image/bmp",
666 "image/cis-cod",
667 "image/gif",
668 "image/ief",
669 "image/jpeg",
670 "image/webp",
671 "image/pict",
672 "image/pipeg",
673 "image/png",
674 "image/svg+xml",
675 "image/tiff",
676 "image/vnd.microsoft.icon",
677 "image/x-cmu-raster",
678 "image/x-cmx",
679 "image/x-icon",
680 "image/x-portable-anymap",
681 "image/x-portable-bitmap",
682 "image/x-portable-graymap",
683 "image/x-portable-pixmap",
684 "image/x-rgb",
685 "image/x-xbitmap",
686 "image/x-xpixmap",
687 "image/x-xwindowdump"};
703 "audio/x-m4a",
704 "audio/x-ms-wma",
705 "audio/vnd.rn-realaudio",
721 "video/sd-video",
723 "video/x-dv",
724 "video/x-m4v",
725 "video/x-mpeg",
726 "video/x-ms-asf",
727 "video/x-ms-wmv"
732 base::span<const char* const> standard_types;
734 static const StandardType kStandardTypes[] = {{"image/", kStandardImageTypes},
745 // * If |prefix_match = false| then |mime_type| is an exact (case-insensitive)
749 // (case-insensitive) string. For instance "Text/" would match "text/plain".
751 base::span<const MimeInfo> mappings, in GetExtensionsFromHardCodedMappings()
754 std::unordered_set<base::FilePath::StringType>* extensions) { in GetExtensionsFromHardCodedMappings()
758 if (base::StartsWith(cur_mime_type, mime_type, in GetExtensionsFromHardCodedMappings()
759 base::CompareCase::INSENSITIVE_ASCII) && in GetExtensionsFromHardCodedMappings()
761 for (std::string_view this_extension : base::SplitStringPiece( in GetExtensionsFromHardCodedMappings()
762 mapping.extensions, ",", base::TRIM_WHITESPACE, in GetExtensionsFromHardCodedMappings()
763 base::SPLIT_WANT_ALL)) { in GetExtensionsFromHardCodedMappings()
764 extensions->insert(StringToFilePathStringType(this_extension)); in GetExtensionsFromHardCodedMappings()
771 base::span<const char* const> standard_types, in GetExtensionsHelper()
773 std::unordered_set<base::FilePath::StringType>* extensions) { in GetExtensionsHelper()
779 // Also look up the extensions from hard-coded mappings in case that some in GetExtensionsHelper()
793 size_t old_target_size = target->size(); in UnorderedSetToVector()
794 target->resize(old_target_size + source->size()); in UnorderedSetToVector()
796 for (auto iter = source->begin(); iter != source->end(); ++iter, ++i) in UnorderedSetToVector()
804 // following characters are legal for boundaries: '()+_,-./:=?
817 std::vector<base::FilePath::StringType>* extensions) { in GetExtensionsForMimeType()
821 const std::string mime_type = base::ToLowerASCII(unsafe_mime_type); in GetExtensionsForMimeType()
822 std::unordered_set<base::FilePath::StringType> unique_extensions; in GetExtensionsForMimeType()
824 if (base::EndsWith(mime_type, "/*", base::CompareCase::INSENSITIVE_ASCII)) { in GetExtensionsForMimeType()
825 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1); in GetExtensionsForMimeType()
832 if (type->leading_mime_type && in GetExtensionsForMimeType()
833 leading_mime_type == type->leading_mime_type) { in GetExtensionsForMimeType()
838 GetExtensionsHelper(type->standard_types, in GetExtensionsForMimeType()
845 // Also look up the extensions from hard-coded mappings in case that some in GetExtensionsForMimeType()
873 // "_" / "," / "-" / "." / "/" / ":" / "=" / "?" in GenerateMimeMultipartBoundary()
877 result.append("----MultipartBoundary--"); in GenerateMimeMultipartBoundary()
878 while (result.size() < (kMimeBoundarySize - 4)) { in GenerateMimeMultipartBoundary()
879 char c = kMimeBoundaryCharacters[base::RandInt( in GenerateMimeMultipartBoundary()
880 0, kMimeBoundaryCharacters.size() - 1)]; in GenerateMimeMultipartBoundary()
883 result.append("----"); in GenerateMimeMultipartBoundary()
885 // Not a strict requirement - documentation only. in GenerateMimeMultipartBoundary()
898 post_data->append("--" + mime_boundary + "\r\n"); in AddMultipartValueForUpload()
899 // Next line is the Content-disposition. in AddMultipartValueForUpload()
900 post_data->append("Content-Disposition: form-data; name=\"" + in AddMultipartValueForUpload()
903 // If Content-type is specified, the next line is that. in AddMultipartValueForUpload()
904 post_data->append("Content-Type: " + content_type + "\r\n"); in AddMultipartValueForUpload()
907 post_data->append("\r\n" + value + "\r\n"); in AddMultipartValueForUpload()
918 post_data->append("--" + mime_boundary + "\r\n"); in AddMultipartValueForUploadWithFileName()
919 // Next line is the Content-disposition. in AddMultipartValueForUploadWithFileName()
920 post_data->append("Content-Disposition: form-data; name=\"" + value_name + in AddMultipartValueForUploadWithFileName()
923 // If Content-type is specified, the next line is that. in AddMultipartValueForUploadWithFileName()
924 post_data->append("Content-Type: " + content_type + "\r\n"); in AddMultipartValueForUploadWithFileName()
927 post_data->append("\r\n" + value + "\r\n"); in AddMultipartValueForUploadWithFileName()
933 post_data->append("--" + mime_boundary + "--\r\n"); in AddMultipartFinalDelimiterForUpload()
936 // TODO(toyoshim): We may prefer to implement a strict RFC2616 media-type
937 // (https://tools.ietf.org/html/rfc2616#section-3.7) parser.