diff options
author | Carson Labrado <clabrado@google.com> | 2023-03-17 23:27:00 +0300 |
---|---|---|
committer | Carson Labrado <clabrado@google.com> | 2023-03-28 02:16:43 +0300 |
commit | b27e1cbefc2a12a838de209decb7e22b5ba1b2c0 (patch) | |
tree | bb4eadf383f4e988c297d90bccec0d487931f77a | |
parent | 0ed80c8ce93a38eca6951ffad5c3143a3a720053 (diff) | |
download | bmcweb-b27e1cbefc2a12a838de209decb7e22b5ba1b2c0.tar.xz |
Aggregation: Prefix fix HttpHeaders property
The "HttpHeaders" property in a response is an array of HTTP headers. We
perform prefix fixing on the "Location" header from responses so we
should also fix any "Location" headers which are contained by
"HttpHeaders" in an aggregated response. This requires special handling
since each header is represented as a single string in the response.
Added testcase for HttpHeaders property
Tested:
All unit tests pass
Signed-off-by: Carson Labrado <clabrado@google.com>
Change-Id: I3040c4ea52b2bebcb6e206bb50585c6a75538f0a
-rw-r--r-- | redfish-core/include/redfish_aggregator.hpp | 40 | ||||
-rw-r--r-- | test/redfish-core/include/redfish_aggregator_test.cpp | 33 |
2 files changed, 73 insertions, 0 deletions
diff --git a/redfish-core/include/redfish_aggregator.hpp b/redfish-core/include/redfish_aggregator.hpp index 1d340f770b..c640586c10 100644 --- a/redfish-core/include/redfish_aggregator.hpp +++ b/redfish-core/include/redfish_aggregator.hpp @@ -175,6 +175,38 @@ static inline void addAggregatedHeaders(crow::Response& asyncResp, // TODO: we need special handling for Link Header Value } +// Fix HTTP headers which appear in responses from Task resources among others +static inline void addPrefixToHeadersInResp(nlohmann::json& json, + std::string_view prefix) +{ + // The passed in "HttpHeaders" should be an array of headers + nlohmann::json::array_t* array = json.get_ptr<nlohmann::json::array_t*>(); + if (array == nullptr) + { + BMCWEB_LOG_ERROR << "Field wasn't an array_t????"; + return; + } + + for (nlohmann::json& item : *array) + { + // Each header is a single string with the form "<Field>: <Value>" + std::string* strHeader = item.get_ptr<std::string*>(); + if (strHeader == nullptr) + { + BMCWEB_LOG_CRITICAL << "Field wasn't a string????"; + continue; + } + + constexpr std::string_view location = "Location: "; + if (strHeader->starts_with(location)) + { + std::string header = strHeader->substr(location.size()); + addPrefixToStringItem(header, prefix); + *strHeader = std::string(location) + header; + } + } +} + // Search the json for all URIs and add the supplied prefix if the URI is for // an aggregated resource. static inline void addPrefixes(nlohmann::json& json, std::string_view prefix) @@ -191,6 +223,14 @@ static inline void addPrefixes(nlohmann::json& json, std::string_view prefix) continue; } + // "HttpHeaders" contains HTTP headers. Among those we need to + // attempt to fix the "Location" header + if (item.first == "HttpHeaders") + { + addPrefixToHeadersInResp(item.second, prefix); + continue; + } + // Recusively parse the rest of the json addPrefixes(item.second, prefix); } diff --git a/test/redfish-core/include/redfish_aggregator_test.cpp b/test/redfish-core/include/redfish_aggregator_test.cpp index 838e5edf6b..2fdd784d3b 100644 --- a/test/redfish-core/include/redfish_aggregator_test.cpp +++ b/test/redfish-core/include/redfish_aggregator_test.cpp @@ -206,6 +206,39 @@ TEST(addPrefixes, ParseJsonObjectNestedArray) "/redfish/v1/Chassis/5B42_TestChassis"); } +TEST(addPrefixes, FixHttpHeadersInResponseBody) +{ + nlohmann::json taskResp = nlohmann::json::parse(R"( + { + "@odata.id": "/redfish/v1/TaskService/Tasks/0", + "Name": "Task 0", + "Payload": { + "HttpHeaders": [ + "User-Agent: curl/7.87.0", + "Accept: */*", + "Host: 127.127.12.7", + "Content-Length: 33", + "Location: /redfish/v1/Managers/bmc/LogServices/Dump/Entries/0" + ] + }, + "PercentComplete": 100, + "TaskMonitor": "/redfish/v1/TaskService/Tasks/0/Monitor", + "TaskState": "Completed", + "TaskStatus": "OK" + } + )", + nullptr, false); + + addPrefixes(taskResp, "5B247A"); + EXPECT_EQ(taskResp["@odata.id"], "/redfish/v1/TaskService/Tasks/5B247A_0"); + EXPECT_EQ(taskResp["TaskMonitor"], + "/redfish/v1/TaskService/Tasks/5B247A_0/Monitor"); + nlohmann::json& httpHeaders = taskResp["Payload"]["HttpHeaders"]; + EXPECT_EQ( + httpHeaders[4], + "Location: /redfish/v1/Managers/5B247A_bmc/LogServices/Dump/Entries/0"); +} + // Attempts to perform prefix fixing on a response with response code "result". // Fixing should always occur void assertProcessResponse(unsigned result) |