summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorsmckay@chromium.org <smckay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-28 19:31:14 +0000
committersmckay@chromium.org <smckay@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-28 19:31:14 +0000
commitecf652e0ab0b625276e2b9cc1a435ff865fb5e51 (patch)
treeeba05ad40d368afdb2a234143e71502b84180dc7 /net/base
parent9ca3560e25dbea8ea7fffff512038e674e875179 (diff)
downloadchromium_src-ecf652e0ab0b625276e2b9cc1a435ff865fb5e51.zip
chromium_src-ecf652e0ab0b625276e2b9cc1a435ff865fb5e51.tar.gz
chromium_src-ecf652e0ab0b625276e2b9cc1a435ff865fb5e51.tar.bz2
Support for extracting IANA media type.
BUG=143998 Review URL: https://chromiumcodereview.appspot.com/10829468 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153713 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/mime_util.cc30
-rw-r--r--net/base/mime_util.h5
-rw-r--r--net/base/mime_util_unittest.cc17
3 files changed, 52 insertions, 0 deletions
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index 62788dc..2880fa19 100644
--- a/net/base/mime_util.cc
+++ b/net/base/mime_util.cc
@@ -17,6 +17,27 @@
using std::string;
+namespace {
+
+struct MediaType {
+ const char name[12];
+ const char matcher[13];
+};
+
+static const MediaType kIanaMediaTypes[] = {
+ { "application", "application/" },
+ { "audio", "audio/" },
+ { "example", "example/" },
+ { "image", "image/" },
+ { "message", "message/" },
+ { "model", "model/" },
+ { "multipart", "multipart/" },
+ { "text", "text/" },
+ { "video", "video/" },
+};
+
+} // namespace
+
namespace net {
// Singleton utility class for mime types.
@@ -871,4 +892,13 @@ void GetMediaCodecsBlacklistedForTests(std::vector<std::string>* codecs) {
#endif
}
+const std::string GetIANAMediaType(const std::string& mime_type) {
+ for (size_t i = 0; i < arraysize(kIanaMediaTypes); ++i) {
+ if (StartsWithASCII(mime_type, kIanaMediaTypes[i].matcher, true)) {
+ return kIanaMediaTypes[i].name;
+ }
+ }
+ return "";
+}
+
} // namespace net
diff --git a/net/base/mime_util.h b/net/base/mime_util.h
index 7b75f33..07f7b76 100644
--- a/net/base/mime_util.h
+++ b/net/base/mime_util.h
@@ -124,6 +124,11 @@ NET_EXPORT void GetMediaTypesBlacklistedForTests(
NET_EXPORT void GetMediaCodecsBlacklistedForTests(
std::vector<std::string>* codecs);
+// Returns the IANA media type contained in |mime_type|, or an empty
+// string if |mime_type| does not specifify a known media type.
+// Supported media types are defined at:
+// http://www.iana.org/assignments/media-types/index.html
+NET_EXPORT const std::string GetIANAMediaType(const std::string& mime_type);
} // namespace net
#endif // NET_BASE_MIME_UTIL_H__
diff --git a/net/base/mime_util_unittest.cc b/net/base/mime_util_unittest.cc
index 8210b0c..ae112cb 100644
--- a/net/base/mime_util_unittest.cc
+++ b/net/base/mime_util_unittest.cc
@@ -168,4 +168,21 @@ TEST(MimeUtilTest, TestIsMimeType) {
EXPECT_FALSE(IsMimeType("text/"));
}
+TEST(MimeUtilTest, TestToIANAMediaType) {
+ EXPECT_EQ("", GetIANAMediaType("texting/driving"));
+ EXPECT_EQ("", GetIANAMediaType("ham/sandwich"));
+ EXPECT_EQ("", GetIANAMediaType(""));
+ EXPECT_EQ("", GetIANAMediaType("/application/hamsandwich"));
+
+ EXPECT_EQ("application", GetIANAMediaType("application/poodle-wrestler"));
+ EXPECT_EQ("audio", GetIANAMediaType("audio/mpeg"));
+ EXPECT_EQ("example", GetIANAMediaType("example/yomomma"));
+ EXPECT_EQ("image", GetIANAMediaType("image/png"));
+ EXPECT_EQ("message", GetIANAMediaType("message/sipfrag"));
+ EXPECT_EQ("model", GetIANAMediaType("model/vrml"));
+ EXPECT_EQ("multipart", GetIANAMediaType("multipart/mixed"));
+ EXPECT_EQ("text", GetIANAMediaType("text/plain"));
+ EXPECT_EQ("video", GetIANAMediaType("video/H261"));
+}
+
} // namespace net