fixed GH #2843: Poco::Net::MediaType::parse() does not split parameters

This commit is contained in:
Günter Obiltschnig 2020-01-22 11:56:11 +01:00
parent eb26a95682
commit f6dafb437e
2 changed files with 27 additions and 20 deletions

View File

@ -31,7 +31,7 @@ MediaType::MediaType(const std::string& mediaType)
parse(mediaType);
}
MediaType::MediaType(const std::string& type, const std::string& subType):
_type(type),
_subType(subType)
@ -81,7 +81,7 @@ MediaType& MediaType::operator = (MediaType&& mediaType) noexcept
return *this;
}
MediaType& MediaType::operator = (const std::string& mediaType)
{
parse(mediaType);
@ -96,43 +96,43 @@ void MediaType::swap(MediaType& mediaType)
_parameters.swap(mediaType._parameters);
}
void MediaType::setType(const std::string& type)
{
_type = type;
}
void MediaType::setSubType(const std::string& subType)
{
_subType = subType;
}
void MediaType::setParameter(const std::string& name, const std::string& value)
{
_parameters.set(name, value);
}
const std::string& MediaType::getParameter(const std::string& name) const
{
return _parameters.get(name);
}
bool MediaType::hasParameter(const std::string& name) const
{
return _parameters.has(name);
}
void MediaType::removeParameter(const std::string& name)
{
_parameters.erase(name);
}
std::string MediaType::toString() const
{
std::string result;
@ -155,7 +155,7 @@ bool MediaType::matches(const MediaType& mediaType) const
return matches(mediaType._type, mediaType._subType);
}
bool MediaType::matches(const std::string& type, const std::string& subType) const
{
return icompare(_type, type) == 0 && icompare(_subType, subType) == 0;
@ -176,7 +176,7 @@ bool MediaType::matchesRange(const MediaType& mediaType) const
bool MediaType::matchesRange(const std::string& type, const std::string& subType) const
{
if (_type == "*" || type == "*" || icompare(_type, type) == 0)
if (_type == "*" || type == "*" || icompare(_type, type) == 0)
{
return _subType == "*" || subType == "*" || icompare(_subType, subType) == 0;
}
@ -202,6 +202,7 @@ void MediaType::parse(const std::string& mediaType)
if (it != end) ++it;
while (it != end && *it != ';' && !Poco::Ascii::isSpace(*it)) _subType += *it++;
while (it != end && *it != ';') ++it;
if (it != end) ++it; // skip semicolon
MessageHeader::splitParameters(it, end, _parameters);
}

View File

@ -33,19 +33,25 @@ void MediaTypeTest::testParse()
assertTrue (mt1.getType() == "text");
assertTrue (mt1.getSubType() == "plain");
assertTrue (mt1.parameters().empty());
MediaType mt2("text/xml;charset=us-ascii");
assertTrue (mt2.getType() == "text");
assertTrue (mt2.getSubType() == "xml");
assertTrue (!mt2.parameters().empty());
assertTrue (mt2.parameters().size() == 1);
assertTrue (mt2.getParameter("charset") == "us-ascii");
MediaType mt3("application/test; param1=value1; param2=\"value 2\"");
assertTrue (mt3.getType() == "application");
assertTrue (mt3.getSubType() == "test");
assertTrue (!mt3.parameters().empty());
assertTrue (mt3.parameters().size() == 2);
assertTrue (mt3.getParameter("param1") == "value1");
assertTrue (mt3.getParameter("PARAM2") == "value 2");
MediaType mt4("multipart/mixed; boundary=\"MIME_boundary_01234567\"");
assertTrue (mt4.getType() == "multipart");
assertTrue (mt4.getSubType() == "mixed");
assertTrue (mt4.parameters().size() == 1);
assertTrue (mt4.getParameter("boundary") == "MIME_boundary_01234567");
}
@ -53,10 +59,10 @@ void MediaTypeTest::testToString()
{
MediaType mt1("text", "plain");
assertTrue (mt1.toString() == "text/plain");
mt1.setParameter("charset", "iso-8859-1");
assertTrue (mt1.toString() == "text/plain; charset=iso-8859-1");
MediaType mt2("application", "test");
mt2.setParameter("param1", "value1");
mt2.setParameter("param2", "value 2");
@ -93,17 +99,17 @@ void MediaTypeTest::testMatchRange()
assertTrue (mt2.matchesRange(mt6));
assertTrue (mt3.matchesRange(mt6));
assertTrue (mt4.matchesRange(mt6));
assertTrue (mt5.matchesRange(mt1));
assertTrue (mt5.matchesRange(mt2));
assertTrue (mt5.matchesRange(mt3));
assertTrue (!mt5.matchesRange(mt4));
assertTrue (mt1.matchesRange("text", "*"));
assertTrue (mt2.matchesRange("text", "*"));
assertTrue (mt3.matchesRange("text", "*"));
assertTrue (!mt4.matchesRange("text", "*"));
assertTrue (mt1.matchesRange("*"));
assertTrue (mt4.matchesRange("*"));
}