by callcc » 29 Jan 2011, 14:34
Using it in HTTP server (handling client's “Accept” header).
- Code: Select all
Index: Net/include/Poco/Net/MediaType.h
===================================================================
--- Net/include/Poco/Net/MediaType.h (revision 1403)
+++ Net/include/Poco/Net/MediaType.h (working copy)
@@ -124,3 +124,11 @@
bool matches(const std::string& type) const;
/// Returns true iff the type matches the given type.
/// Matching is case insensitive.
+
+ bool matchesRange(const MediaType& mediaType) const;
+ /// Returns true if the type and subtype match
+ /// the type and subtype of the given media type.
+ /// If the MIME type is a range of types it matches
+ /// any media type withing the range (e.g. "image/*" matches
+ /// any image media type, "*/*" matches anything).
+ /// Matching is case insensitive.
+ bool matchesRange(const std::string& type, const std::string& subType) const;
+ /// Returns true if the type and subtype match
+ /// the given type and subtype.
+ /// If the MIME type is a range of types it matches
+ /// any media type withing the range (e.g. "image/*" matches
+ /// any image media type, "*/*" matches anything).
+ /// Matching is case insensitive.
+
+ bool matchesRange(const std::string& type) const;
+ /// Returns true if the type matches the given type or
+ /// the type is a range of types denoted by "*".
+ /// Matching is case insensitive.
+
protected:
void parse(const std::string& mediaType);
Index: Net/src/MediaType.cpp
===================================================================
--- Net/src/MediaType.cpp (revision 1403)
+++ Net/src/MediaType.cpp (working copy)
@@ -172,6 +172,28 @@
}
+bool MediaType::matchesRange(const MediaType& mediaType) const
+{
+ return matchesRange(mediaType._type, mediaType._subType);
+}
+
+
+bool MediaType::matchesRange(const std::string& type, const std::string& subType) const
+{
+ if (_type == "*" || icompare(_type, type) == 0) {
+ return _subType == "*" || icompare(_subType, subType) == 0;
+ }
+
+ return false;
+}
+
+
+bool MediaType::matchesRange(const std::string& type) const
+{
+ return _type == "*" || matches(type);
+}
+
+
void MediaType::parse(const std::string& mediaType)
{
_type.clear();