summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorgbillock@chromium.org <gbillock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-06 00:05:59 +0000
committergbillock@chromium.org <gbillock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-06 00:05:59 +0000
commit0b99ae575f39da34f7494723934d2a8cfeca63a8 (patch)
treec1fd8b3d1a06aef9cc498e530006ee562007b47e /net/base
parent694e199351b4bf1b3d74a1b6ec55f751a7df3aea (diff)
downloadchromium_src-0b99ae575f39da34f7494723934d2a8cfeca63a8.zip
chromium_src-0b99ae575f39da34f7494723934d2a8cfeca63a8.tar.gz
chromium_src-0b99ae575f39da34f7494723934d2a8cfeca63a8.tar.bz2
Move function for classifying a string as a mime type into MimeUtil
R=rvargas@chromium.org BUG=None TEST=MimeUtilTest.* Review URL: https://chromiumcodereview.appspot.com/10448109 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140657 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/mime_util.cc49
-rw-r--r--net/base/mime_util.h5
-rw-r--r--net/base/mime_util_unittest.cc35
3 files changed, 89 insertions, 0 deletions
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index 8ff58e6d..364cbdf 100644
--- a/net/base/mime_util.cc
+++ b/net/base/mime_util.cc
@@ -43,6 +43,8 @@ class MimeUtil : public PlatformMimeUtil {
bool MatchesMimeType(const std::string &mime_type_pattern,
const std::string &mime_type) const;
+ bool IsMimeType(const std::string& type_string) const;
+
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const;
void ParseCodecString(const std::string& codecs,
@@ -486,6 +488,49 @@ bool MimeUtil::MatchesMimeType(const std::string& mime_type_pattern,
return true;
}
+// See http://www.iana.org/assignments/media-types/index.html
+static const char* legal_top_level_types[] = {
+ "application/",
+ "audio/",
+ "example/",
+ "image/",
+ "message/",
+ "model/",
+ "multipart/",
+ "text/",
+ "video/",
+};
+
+bool MimeUtil::IsMimeType(const std::string& type_string) const {
+ // MIME types are always ASCII and case-insensitive (at least, the top-level
+ // and secondary types we care about).
+ if (!IsStringASCII(type_string))
+ return false;
+
+ if (type_string == "*/*" || type_string == "*")
+ return true;
+
+ for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) {
+ if (StartsWithASCII(type_string, legal_top_level_types[i], false) &&
+ type_string.length() > strlen(legal_top_level_types[i])) {
+ return true;
+ }
+ }
+
+ // If there's a "/" separator character, and the token before it is
+ // "x-" + (ascii characters), it is also a MIME type.
+ size_t slash = type_string.find('/');
+ if (slash < 3 ||
+ slash == std::string::npos || slash == type_string.length() - 1) {
+ return false;
+ }
+
+ if (StartsWithASCII(type_string, "x-", false))
+ return true;
+
+ return false;
+}
+
bool MimeUtil::AreSupportedMediaCodecs(
const std::vector<std::string>& codecs) const {
return AreSupportedCodecs(codecs_map_, codecs);
@@ -578,6 +623,10 @@ bool MatchesMimeType(const std::string& mime_type_pattern,
return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type);
}
+bool IsMimeType(const std::string& type_string) {
+ return g_mime_util.Get().IsMimeType(type_string);
+}
+
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
}
diff --git a/net/base/mime_util.h b/net/base/mime_util.h
index ba3e25d..ff70f4a 100644
--- a/net/base/mime_util.h
+++ b/net/base/mime_util.h
@@ -58,6 +58,11 @@ NET_EXPORT bool IsSupportedMimeType(const std::string& mime_type);
NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern,
const std::string& mime_type);
+// Returns true if the |type_string| is a correctly-formed mime type specifier.
+// Allows strings of the form x/y[;params], where "x" is a legal mime type name.
+// Also allows wildcard types -- "x/*", "*/*", and "*".
+NET_EXPORT bool IsMimeType(const std::string& type_string);
+
// Returns true if and only if all codecs are supported, false otherwise.
NET_EXPORT bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs);
diff --git a/net/base/mime_util_unittest.cc b/net/base/mime_util_unittest.cc
index c647294..1ee2862 100644
--- a/net/base/mime_util_unittest.cc
+++ b/net/base/mime_util_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "base/basictypes.h"
+#include "base/utf_string_conversions.h"
#include "net/base/mime_util.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -126,4 +127,38 @@ TEST(MimeUtilTest, ParseCodecString) {
EXPECT_EQ("mp4a.40.2", codecs_out[1]);
}
+TEST(MimeUtilTest, TestIsMimeType) {
+ std::string nonAscii("application/nonutf8");
+ EXPECT_TRUE(IsMimeType(nonAscii));
+#if defined(OS_WIN)
+ nonAscii.append(WideToUTF8(std::wstring(L"\u2603")));
+#else
+ nonAscii.append("\u2603"); // unicode snowman
+#endif
+ EXPECT_FALSE(IsMimeType(nonAscii));
+
+ EXPECT_TRUE(IsMimeType("application/mime"));
+ EXPECT_TRUE(IsMimeType("audio/mime"));
+ EXPECT_TRUE(IsMimeType("example/mime"));
+ EXPECT_TRUE(IsMimeType("image/mime"));
+ EXPECT_TRUE(IsMimeType("message/mime"));
+ EXPECT_TRUE(IsMimeType("model/mime"));
+ EXPECT_TRUE(IsMimeType("multipart/mime"));
+ EXPECT_TRUE(IsMimeType("text/mime"));
+ EXPECT_TRUE(IsMimeType("TEXT/mime"));
+ EXPECT_TRUE(IsMimeType("Text/mime"));
+ EXPECT_TRUE(IsMimeType("TeXt/mime"));
+ EXPECT_TRUE(IsMimeType("video/mime"));
+ EXPECT_TRUE(IsMimeType("video/mime;parameter"));
+ EXPECT_TRUE(IsMimeType("*/*"));
+ EXPECT_TRUE(IsMimeType("*"));
+
+ EXPECT_TRUE(IsMimeType("x-video/mime"));
+ EXPECT_TRUE(IsMimeType("X-Video/mime"));
+ EXPECT_FALSE(IsMimeType("x-video/"));
+ EXPECT_FALSE(IsMimeType("x-/mime"));
+ EXPECT_FALSE(IsMimeType("mime/looking"));
+ EXPECT_FALSE(IsMimeType("text/"));
+}
+
} // namespace net