Overview
Features
Download
Documentation
Community
Add-Ons & Services

Net::MediaType, “*/subtype” and “*/*” matching

General discussion regarding the development of POCO for contributors.

Net::MediaType, “*/subtype” and “*/*” matching

Postby 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();
callcc
 
Posts: 9
Joined: 12 Apr 2010, 12:14
Location: Russia

Re: Net::MediaType, “*/subtype” and “*/*” matching

Postby guenter » 31 Jan 2011, 15:11

Thank you. Added to 1.4.1.
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to Contributors

Who is online

Users browsing this forum: No registered users and 1 guest

cron