OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <iterator> | 6 #include <iterator> |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "net/base/mime_util.h" | 17 #include "net/base/mime_util.h" |
18 #include "net/base/platform_mime_util.h" | 18 #include "net/base/platform_mime_util.h" |
| 19 #include "net/http/http_util.h" |
19 | 20 |
20 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
21 #include "base/android/build_info.h" | 22 #include "base/android/build_info.h" |
22 #endif | 23 #endif |
23 | 24 |
24 using std::string; | 25 using std::string; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 struct MediaType { | 29 struct MediaType { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 bool IsSupportedMediaMimeType(const std::string& mime_type) const; | 63 bool IsSupportedMediaMimeType(const std::string& mime_type) const; |
63 bool IsSupportedNonImageMimeType(const std::string& mime_type) const; | 64 bool IsSupportedNonImageMimeType(const std::string& mime_type) const; |
64 bool IsUnsupportedTextMimeType(const std::string& mime_type) const; | 65 bool IsUnsupportedTextMimeType(const std::string& mime_type) const; |
65 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const; | 66 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const; |
66 | 67 |
67 bool IsSupportedMimeType(const std::string& mime_type) const; | 68 bool IsSupportedMimeType(const std::string& mime_type) const; |
68 | 69 |
69 bool MatchesMimeType(const std::string &mime_type_pattern, | 70 bool MatchesMimeType(const std::string &mime_type_pattern, |
70 const std::string &mime_type) const; | 71 const std::string &mime_type) const; |
71 | 72 |
72 bool IsMimeType(const std::string& type_string) const; | 73 bool ParseMimeTypeWithoutParameter(const std::string& type_string, |
| 74 std::string* top_level_type, |
| 75 std::string* subtype) const; |
| 76 |
| 77 bool IsValidTopLevelMimeType(const std::string& type_string) const; |
73 | 78 |
74 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | 79 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
75 | 80 |
76 void ParseCodecString(const std::string& codecs, | 81 void ParseCodecString(const std::string& codecs, |
77 std::vector<std::string>* codecs_out, | 82 std::vector<std::string>* codecs_out, |
78 bool strip); | 83 bool strip); |
79 | 84 |
80 bool IsStrictMediaMimeType(const std::string& mime_type) const; | 85 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
81 bool IsSupportedStrictMediaMimeType( | 86 bool IsSupportedStrictMediaMimeType( |
82 const std::string& mime_type, | 87 const std::string& mime_type, |
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 if (base_type.find(left) != 0) | 658 if (base_type.find(left) != 0) |
654 return false; | 659 return false; |
655 | 660 |
656 if (!right.empty() && | 661 if (!right.empty() && |
657 base_type.rfind(right) != base_type.length() - right.length()) | 662 base_type.rfind(right) != base_type.length() - right.length()) |
658 return false; | 663 return false; |
659 | 664 |
660 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); | 665 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); |
661 } | 666 } |
662 | 667 |
663 // See http://www.iana.org/assignments/media-types/index.html | 668 // See http://www.iana.org/assignments/media-types/media-types.xhtml |
664 static const char* legal_top_level_types[] = { | 669 static const char* legal_top_level_types[] = { |
665 "application/", | 670 "application", |
666 "audio/", | 671 "audio", |
667 "example/", | 672 "example", |
668 "image/", | 673 "image", |
669 "message/", | 674 "message", |
670 "model/", | 675 "model", |
671 "multipart/", | 676 "multipart", |
672 "text/", | 677 "text", |
673 "video/", | 678 "video", |
674 }; | 679 }; |
675 | 680 |
676 bool MimeUtil::IsMimeType(const std::string& type_string) const { | 681 bool MimeUtil::ParseMimeTypeWithoutParameter( |
677 // MIME types are always ASCII and case-insensitive (at least, the top-level | 682 const std::string& type_string, |
678 // and secondary types we care about). | 683 std::string* top_level_type, |
679 if (!base::IsStringASCII(type_string)) | 684 std::string* subtype) const { |
| 685 std::vector<std::string> components; |
| 686 base::SplitString(type_string, '/', &components); |
| 687 if (components.size() != 2 || |
| 688 !HttpUtil::IsToken(components[0]) || |
| 689 !HttpUtil::IsToken(components[1])) |
680 return false; | 690 return false; |
681 | 691 |
682 if (type_string == "*/*" || type_string == "*") | 692 if (top_level_type) |
683 return true; | 693 *top_level_type = components[0]; |
| 694 if (subtype) |
| 695 *subtype = components[1]; |
| 696 return true; |
| 697 } |
684 | 698 |
| 699 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { |
| 700 std::string lower_type = StringToLowerASCII(type_string); |
685 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { | 701 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { |
686 if (StartsWithASCII(type_string, legal_top_level_types[i], false) && | 702 if (lower_type.compare(legal_top_level_types[i]) == 0) |
687 type_string.length() > strlen(legal_top_level_types[i])) { | |
688 return true; | 703 return true; |
689 } | |
690 } | 704 } |
691 | 705 |
692 // If there's a "/" separator character, and the token before it is | 706 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false); |
693 // "x-" + (ascii characters), it is also a MIME type. | |
694 size_t slash = type_string.find('/'); | |
695 if (slash < 3 || | |
696 slash == std::string::npos || slash == type_string.length() - 1) { | |
697 return false; | |
698 } | |
699 | |
700 if (StartsWithASCII(type_string, "x-", false)) | |
701 return true; | |
702 | |
703 return false; | |
704 } | 707 } |
705 | 708 |
706 bool MimeUtil::AreSupportedMediaCodecs( | 709 bool MimeUtil::AreSupportedMediaCodecs( |
707 const std::vector<std::string>& codecs) const { | 710 const std::vector<std::string>& codecs) const { |
708 return AreSupportedCodecs(codecs_map_, codecs); | 711 return AreSupportedCodecs(codecs_map_, codecs); |
709 } | 712 } |
710 | 713 |
711 void MimeUtil::ParseCodecString(const std::string& codecs, | 714 void MimeUtil::ParseCodecString(const std::string& codecs, |
712 std::vector<std::string>* codecs_out, | 715 std::vector<std::string>* codecs_out, |
713 bool strip) { | 716 bool strip) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 | 801 |
799 bool IsSupportedMimeType(const std::string& mime_type) { | 802 bool IsSupportedMimeType(const std::string& mime_type) { |
800 return g_mime_util.Get().IsSupportedMimeType(mime_type); | 803 return g_mime_util.Get().IsSupportedMimeType(mime_type); |
801 } | 804 } |
802 | 805 |
803 bool MatchesMimeType(const std::string& mime_type_pattern, | 806 bool MatchesMimeType(const std::string& mime_type_pattern, |
804 const std::string& mime_type) { | 807 const std::string& mime_type) { |
805 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); | 808 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); |
806 } | 809 } |
807 | 810 |
808 bool IsMimeType(const std::string& type_string) { | 811 bool ParseMimeTypeWithoutParameter(const std::string& type_string, |
809 return g_mime_util.Get().IsMimeType(type_string); | 812 std::string* top_level_type, |
| 813 std::string* subtype) { |
| 814 return g_mime_util.Get().ParseMimeTypeWithoutParameter( |
| 815 type_string, top_level_type, subtype); |
| 816 } |
| 817 |
| 818 bool IsValidTopLevelMimeType(const std::string& type_string) { |
| 819 return g_mime_util.Get().IsValidTopLevelMimeType(type_string); |
810 } | 820 } |
811 | 821 |
812 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 822 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
813 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 823 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
814 } | 824 } |
815 | 825 |
816 bool IsStrictMediaMimeType(const std::string& mime_type) { | 826 bool IsStrictMediaMimeType(const std::string& mime_type) { |
817 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 827 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
818 } | 828 } |
819 | 829 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 post_data->append("\r\n" + value + "\r\n"); | 1070 post_data->append("\r\n" + value + "\r\n"); |
1061 } | 1071 } |
1062 | 1072 |
1063 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1073 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1064 std::string* post_data) { | 1074 std::string* post_data) { |
1065 DCHECK(post_data); | 1075 DCHECK(post_data); |
1066 post_data->append("--" + mime_boundary + "--\r\n"); | 1076 post_data->append("--" + mime_boundary + "--\r\n"); |
1067 } | 1077 } |
1068 | 1078 |
1069 } // namespace net | 1079 } // namespace net |
OLD | NEW |