From a3526fee27da0e324cd022ea77d282d1146b3317 Mon Sep 17 00:00:00 2001 From: Gunnar Mills Date: Wed, 2 Feb 2022 21:56:44 +0000 Subject: Remove q-factor weighting on Accept Header bmcweb does not do anything with the q-factor weighting (;q=) so just remove it from the encoding. This is needed because routes like "/redfish/v1/Systems/system/LogServices/EventLog/Entries//attachment" have a check for isOctetAccepted. Even though */* is in the Accept Header isOctetAccepted still fails due to the q-factor weighting. On the system I tested, on firefox, Accept looks like: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8 The GUI reported being unable to download a AdditionalDataURI (e.g. ...attachment/) Here is the GUI code attempting to download the additional data: https://github.com/openbmc/webui-vue/blob/9b79a6e7e3df3d3cbaf9a7750bbe343628022026/src/views/Logs/EventLogs/EventLogs.vue#L155 https://github.com/openbmc/webui-vue/blob/9b79a6e7e3df3d3cbaf9a7750bbe343628022026/src/locales/en-US.json#L251 Today this results in a 400 Bad Request due to isOctetAccepted. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept Tested: /redfish/v1/Systems/system/LogServices/PostCodes/Entries//attachment/ and .../EventLog/Entries//attachment now return correctly. Change-Id: I969f5f2c32c4acccd4d80615f17c44d0c8fabd0d Signed-off-by: Gunnar Mills --- include/http_utility.hpp | 9 ++++++++- include/ut/http_utility_test.cpp | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/http_utility.hpp b/include/http_utility.hpp index 46742a0ce6..b7844bacb8 100644 --- a/include/http_utility.hpp +++ b/include/http_utility.hpp @@ -47,8 +47,15 @@ inline bool requestPrefersHtml(std::string_view header) inline bool isOctetAccepted(std::string_view header) { - for (const std::string& encoding : parseAccept(header)) + for (std::string_view encoding : parseAccept(header)) { + // ignore any q-factor weighting (;q=) + std::size_t separator = encoding.find(";q="); + + if (separator != std::string_view::npos) + { + encoding = encoding.substr(0, separator); + } if (encoding == "*/*" || encoding == "application/octet-stream") { return true; diff --git a/include/ut/http_utility_test.cpp b/include/ut/http_utility_test.cpp index e412e17536..1a33fd8648 100644 --- a/include/ut/http_utility_test.cpp +++ b/include/ut/http_utility_test.cpp @@ -27,6 +27,16 @@ TEST(IsOctetAccepted, ContainsOctetReturnsTrue) EXPECT_TRUE(isOctetAccepted("*/*, application/octet-stream")); } +TEST(IsOctetAccepted, ContainsAnyMimeTypeReturnsTrue) +{ + EXPECT_TRUE(http_helpers::isOctetAccepted("text/html, */*")); +} + +TEST(IsOctetAccepted, ContainsQFactorWeightingReturnsTrue) +{ + EXPECT_TRUE(http_helpers::isOctetAccepted("text/html, */*;q=0.8")); +} + TEST(IsOctetAccepted, NoOctetReturnsFalse) { EXPECT_FALSE(isOctetAccepted("text/html, application/json")); @@ -34,4 +44,4 @@ TEST(IsOctetAccepted, NoOctetReturnsFalse) } } // namespace -} // namespace http_helpers \ No newline at end of file +} // namespace http_helpers -- cgit v1.2.3