| 1 | // |
| 2 | // MediaType.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: Messages |
| 6 | // Module: MediaType |
| 7 | // |
| 8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/MediaType.h" |
| 16 | #include "Poco/Net/MessageHeader.h" |
| 17 | #include "Poco/String.h" |
| 18 | #include "Poco/Ascii.h" |
| 19 | #include <algorithm> |
| 20 | |
| 21 | |
| 22 | using Poco::icompare; |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | namespace Net { |
| 27 | |
| 28 | |
| 29 | MediaType::MediaType(const std::string& mediaType) |
| 30 | { |
| 31 | parse(mediaType); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | MediaType::MediaType(const std::string& type, const std::string& subType): |
| 36 | _type(type), |
| 37 | _subType(subType) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | |
| 42 | MediaType::MediaType(const MediaType& mediaType): |
| 43 | _type(mediaType._type), |
| 44 | _subType(mediaType._subType), |
| 45 | _parameters(mediaType._parameters) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | |
| 50 | MediaType::~MediaType() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | |
| 55 | MediaType& MediaType::operator = (const MediaType& mediaType) |
| 56 | { |
| 57 | if (&mediaType != this) |
| 58 | { |
| 59 | _type = mediaType._type; |
| 60 | _subType = mediaType._subType; |
| 61 | _parameters = mediaType._parameters; |
| 62 | } |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | MediaType& MediaType::operator = (const std::string& mediaType) |
| 68 | { |
| 69 | parse(mediaType); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void MediaType::swap(MediaType& mediaType) |
| 75 | { |
| 76 | std::swap(_type, mediaType._type); |
| 77 | std::swap(_subType, mediaType._subType); |
| 78 | _parameters.swap(mediaType._parameters); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void MediaType::setType(const std::string& type) |
| 83 | { |
| 84 | _type = type; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void MediaType::setSubType(const std::string& subType) |
| 89 | { |
| 90 | _subType = subType; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void MediaType::setParameter(const std::string& name, const std::string& value) |
| 95 | { |
| 96 | _parameters.set(name, value); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | const std::string& MediaType::getParameter(const std::string& name) const |
| 101 | { |
| 102 | return _parameters.get(name); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool MediaType::hasParameter(const std::string& name) const |
| 107 | { |
| 108 | return _parameters.has(name); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void MediaType::removeParameter(const std::string& name) |
| 113 | { |
| 114 | _parameters.erase(name); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | std::string MediaType::toString() const |
| 119 | { |
| 120 | std::string result; |
| 121 | result.append(_type); |
| 122 | result.append("/" ); |
| 123 | result.append(_subType); |
| 124 | for (NameValueCollection::ConstIterator it = _parameters.begin(); it != _parameters.end(); ++it) |
| 125 | { |
| 126 | result.append("; " ); |
| 127 | result.append(it->first); |
| 128 | result.append("=" ); |
| 129 | MessageHeader::quote(it->second, result); |
| 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | bool MediaType::matches(const MediaType& mediaType) const |
| 136 | { |
| 137 | return matches(mediaType._type, mediaType._subType); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | bool MediaType::matches(const std::string& type, const std::string& subType) const |
| 142 | { |
| 143 | return icompare(_type, type) == 0 && icompare(_subType, subType) == 0; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | bool MediaType::matches(const std::string& type) const |
| 148 | { |
| 149 | return icompare(_type, type) == 0; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | bool MediaType::matchesRange(const MediaType& mediaType) const |
| 154 | { |
| 155 | return matchesRange(mediaType._type, mediaType._subType); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | bool MediaType::matchesRange(const std::string& type, const std::string& subType) const |
| 160 | { |
| 161 | if (_type == "*" || type == "*" || icompare(_type, type) == 0) |
| 162 | { |
| 163 | return _subType == "*" || subType == "*" || icompare(_subType, subType) == 0; |
| 164 | } |
| 165 | else return false; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | bool MediaType::matchesRange(const std::string& type) const |
| 170 | { |
| 171 | return _type == "*" || type == "*" || matches(type); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void MediaType::parse(const std::string& mediaType) |
| 176 | { |
| 177 | _type.clear(); |
| 178 | _subType.clear(); |
| 179 | _parameters.clear(); |
| 180 | std::string::const_iterator it = mediaType.begin(); |
| 181 | std::string::const_iterator end = mediaType.end(); |
| 182 | while (it != end && Poco::Ascii::isSpace(*it)) ++it; |
| 183 | while (it != end && *it != '/') _type += *it++; |
| 184 | if (it != end) ++it; |
| 185 | while (it != end && *it != ';' && !Poco::Ascii::isSpace(*it)) _subType += *it++; |
| 186 | while (it != end && *it != ';') ++it; |
| 187 | MessageHeader::splitParameters(it, end, _parameters); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | } } // namespace Poco::Net |
| 192 | |