Handle all possible subtypes
Accept header allows any possible parameter value, and expects
that unknown property subtypes are simply ignored.
We were previously enforcing that things either match q=<number>
or have no params. bmcweb has no usage of the params, but allow
them to parse silently per the spec in case someone sends them.
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types#structure_of_a_mime_type
This more meets the intent. In theory we could parse only q and charset
values, but allowing any key/values here makes us more resilient against
new mime types being added.
Tested: Unit tests included and passing
Change-Id: I1500be0da4c0c72185ee5bda5dfc31885dc6102d
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/include/http_utility.hpp b/include/http_utility.hpp
index 158228f..cb3ebc2 100644
--- a/include/http_utility.hpp
+++ b/include/http_utility.hpp
@@ -47,8 +47,9 @@
std::vector<ContentType> ct;
- auto parameters = *(lit(';') >> lit("q=") >> uint_ >> -(lit('.') >> uint_));
- auto typeCharset = char_("a-zA-Z.+-");
+ auto typeCharset = +(char_("a-zA-Z0-9.+-"));
+
+ auto parameters = *(lit(';') >> typeCharset >> lit("=") >> typeCharset);
auto mimeType = knownMimeType |
omit[+typeCharset >> lit('/') >> +typeCharset];
auto parser = +(mimeType >> omit[parameters >> -char_(',') >> *space]);
diff --git a/test/include/http_utility_test.cpp b/test/include/http_utility_test.cpp
index 2a5e8a7..018a5f4 100644
--- a/test/include/http_utility_test.cpp
+++ b/test/include/http_utility_test.cpp
@@ -81,6 +81,15 @@
cborJson),
ContentType::CBOR);
+ EXPECT_EQ(
+ getPreferredContentType("application/json;charset=UTF-8", htmlJson),
+ ContentType::JSON);
+
+ std::array<ContentType, 1> eventStream{ContentType::EventStream};
+ EXPECT_EQ(
+ getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
+ ContentType::EventStream);
+
EXPECT_EQ(getPreferredContentType("application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);