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 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 if (base_type.find(left) != 0) | 622 if (base_type.find(left) != 0) |
618 return false; | 623 return false; |
619 | 624 |
620 if (!right.empty() && | 625 if (!right.empty() && |
621 base_type.rfind(right) != base_type.length() - right.length()) | 626 base_type.rfind(right) != base_type.length() - right.length()) |
622 return false; | 627 return false; |
623 | 628 |
624 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); | 629 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); |
625 } | 630 } |
626 | 631 |
627 // See http://www.iana.org/assignments/media-types/index.html | 632 // See http://www.iana.org/assignments/media-types/media-types.xhtml |
628 static const char* legal_top_level_types[] = { | 633 static const char* legal_top_level_types[] = { |
629 "application/", | 634 "application", |
630 "audio/", | 635 "audio", |
631 "example/", | 636 "example", |
632 "image/", | 637 "image", |
633 "message/", | 638 "message", |
634 "model/", | 639 "model", |
635 "multipart/", | 640 "multipart", |
636 "text/", | 641 "text", |
637 "video/", | 642 "video", |
638 }; | 643 }; |
639 | 644 |
640 bool MimeUtil::IsMimeType(const std::string& type_string) const { | 645 bool MimeUtil::ParseMimeTypeWithoutParameter( |
641 // MIME types are always ASCII and case-insensitive (at least, the top-level | 646 const std::string& type_string, |
642 // and secondary types we care about). | 647 std::string* top_level_type, |
643 if (!IsStringASCII(type_string)) | 648 std::string* subtype) const { |
| 649 std::vector<std::string> components; |
| 650 base::SplitString(type_string, '/', &components); |
| 651 if (components.size() != 2 || |
| 652 !HttpUtil::IsToken(components[0]) || |
| 653 !HttpUtil::IsToken(components[1])) |
644 return false; | 654 return false; |
645 | 655 |
646 if (type_string == "*/*" || type_string == "*") | 656 if (top_level_type) |
647 return true; | 657 *top_level_type = components[0]; |
| 658 if (subtype) |
| 659 *subtype = components[1]; |
| 660 return true; |
| 661 } |
648 | 662 |
| 663 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { |
| 664 std::string lower_type = StringToLowerASCII(type_string); |
649 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { | 665 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { |
650 if (StartsWithASCII(type_string, legal_top_level_types[i], false) && | 666 if (lower_type.compare(legal_top_level_types[i]) == 0) |
651 type_string.length() > strlen(legal_top_level_types[i])) { | |
652 return true; | 667 return true; |
653 } | |
654 } | 668 } |
655 | 669 |
656 // If there's a "/" separator character, and the token before it is | 670 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false); |
657 // "x-" + (ascii characters), it is also a MIME type. | |
658 size_t slash = type_string.find('/'); | |
659 if (slash < 3 || | |
660 slash == std::string::npos || slash == type_string.length() - 1) { | |
661 return false; | |
662 } | |
663 | |
664 if (StartsWithASCII(type_string, "x-", false)) | |
665 return true; | |
666 | |
667 return false; | |
668 } | 671 } |
669 | 672 |
670 bool MimeUtil::AreSupportedMediaCodecs( | 673 bool MimeUtil::AreSupportedMediaCodecs( |
671 const std::vector<std::string>& codecs) const { | 674 const std::vector<std::string>& codecs) const { |
672 return AreSupportedCodecs(codecs_map_, codecs); | 675 return AreSupportedCodecs(codecs_map_, codecs); |
673 } | 676 } |
674 | 677 |
675 void MimeUtil::ParseCodecString(const std::string& codecs, | 678 void MimeUtil::ParseCodecString(const std::string& codecs, |
676 std::vector<std::string>* codecs_out, | 679 std::vector<std::string>* codecs_out, |
677 bool strip) { | 680 bool strip) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 | 765 |
763 bool IsSupportedMimeType(const std::string& mime_type) { | 766 bool IsSupportedMimeType(const std::string& mime_type) { |
764 return g_mime_util.Get().IsSupportedMimeType(mime_type); | 767 return g_mime_util.Get().IsSupportedMimeType(mime_type); |
765 } | 768 } |
766 | 769 |
767 bool MatchesMimeType(const std::string& mime_type_pattern, | 770 bool MatchesMimeType(const std::string& mime_type_pattern, |
768 const std::string& mime_type) { | 771 const std::string& mime_type) { |
769 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); | 772 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); |
770 } | 773 } |
771 | 774 |
772 bool IsMimeType(const std::string& type_string) { | 775 bool ParseMimeTypeWithoutParameter(const std::string& type_string, |
773 return g_mime_util.Get().IsMimeType(type_string); | 776 std::string* top_level_type, |
| 777 std::string* subtype) { |
| 778 return g_mime_util.Get().ParseMimeTypeWithoutParameter( |
| 779 type_string, top_level_type, subtype); |
| 780 } |
| 781 |
| 782 bool IsValidTopLevelMimeType(const std::string& type_string) { |
| 783 return g_mime_util.Get().IsValidTopLevelMimeType(type_string); |
774 } | 784 } |
775 | 785 |
776 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 786 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
777 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 787 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
778 } | 788 } |
779 | 789 |
780 bool IsStrictMediaMimeType(const std::string& mime_type) { | 790 bool IsStrictMediaMimeType(const std::string& mime_type) { |
781 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 791 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
782 } | 792 } |
783 | 793 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 post_data->append("\r\n" + value + "\r\n"); | 1034 post_data->append("\r\n" + value + "\r\n"); |
1025 } | 1035 } |
1026 | 1036 |
1027 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1037 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1028 std::string* post_data) { | 1038 std::string* post_data) { |
1029 DCHECK(post_data); | 1039 DCHECK(post_data); |
1030 post_data->append("--" + mime_boundary + "--\r\n"); | 1040 post_data->append("--" + mime_boundary + "--\r\n"); |
1031 } | 1041 } |
1032 | 1042 |
1033 } // namespace net | 1043 } // namespace net |
OLD | NEW |