1 #pragma once
2 #include "bmcweb_config.h"
3 
4 #include "app.hpp"
5 #include "async_resp.hpp"
6 #include "error_messages.hpp"
7 #include "filter_expr_executor.hpp"
8 #include "filter_expr_printer.hpp"
9 #include "http_request.hpp"
10 #include "http_response.hpp"
11 #include "json_formatters.hpp"
12 #include "logging.hpp"
13 #include "str_utility.hpp"
14 
15 #include <sys/types.h>
16 
17 #include <boost/beast/http/message.hpp> // IWYU pragma: keep
18 #include <boost/beast/http/status.hpp>
19 #include <boost/beast/http/verb.hpp>
20 #include <boost/url/params_view.hpp>
21 #include <nlohmann/json.hpp>
22 
23 #include <algorithm>
24 #include <array>
25 #include <cctype>
26 #include <charconv>
27 #include <compare>
28 #include <cstdint>
29 #include <functional>
30 #include <iterator>
31 #include <limits>
32 #include <map>
33 #include <memory>
34 #include <optional>
35 #include <ranges>
36 #include <string>
37 #include <string_view>
38 #include <system_error>
39 #include <utility>
40 #include <vector>
41 
42 // IWYU pragma: no_include <boost/url/impl/params_view.hpp>
43 // IWYU pragma: no_include <boost/beast/http/impl/message.hpp>
44 // IWYU pragma: no_include <boost/intrusive/detail/list_iterator.hpp>
45 // IWYU pragma: no_include <boost/algorithm/string/detail/classification.hpp>
46 // IWYU pragma: no_include <boost/iterator/iterator_facade.hpp>
47 // IWYU pragma: no_include <boost/type_index/type_index_facade.hpp>
48 // IWYU pragma: no_include <stdint.h>
49 
50 namespace redfish
51 {
52 namespace query_param
53 {
54 
55 enum class ExpandType : uint8_t
56 {
57     None,
58     Links,
59     NotLinks,
60     Both,
61 };
62 
63 // A simple implementation of Trie to help |recursiveSelect|.
64 class SelectTrieNode
65 {
66   public:
67     SelectTrieNode() = default;
68 
69     const SelectTrieNode* find(const std::string& jsonKey) const
70     {
71         auto it = children.find(jsonKey);
72         if (it == children.end())
73         {
74             return nullptr;
75         }
76         return &it->second;
77     }
78 
79     // Creates a new node if the key doesn't exist, returns the reference to the
80     // newly created node; otherwise, return the reference to the existing node
81     SelectTrieNode* emplace(std::string_view jsonKey)
82     {
83         auto [it, _] = children.emplace(jsonKey, SelectTrieNode{});
84         return &it->second;
85     }
86 
87     bool empty() const
88     {
89         return children.empty();
90     }
91 
92     void clear()
93     {
94         children.clear();
95     }
96 
97     void setToSelected()
98     {
99         selected = true;
100     }
101 
102     bool isSelected() const
103     {
104         return selected;
105     }
106 
107   private:
108     std::map<std::string, SelectTrieNode, std::less<>> children;
109     bool selected = false;
110 };
111 
112 // Validates the property in the $select parameter. Every character is among
113 // [a-zA-Z0-9#@_.] (taken from Redfish spec, section 9.6 Properties)
114 inline bool isSelectedPropertyAllowed(std::string_view property)
115 {
116     // These a magic number, but with it it's less likely that this code
117     // introduces CVE; e.g., too large properties crash the service.
118     constexpr int maxPropertyLength = 60;
119     if (property.empty() || property.size() > maxPropertyLength)
120     {
121         return false;
122     }
123     for (char ch : property)
124     {
125         if (std::isalnum(static_cast<unsigned char>(ch)) == 0 && ch != '#' &&
126             ch != '@' && ch != '.')
127         {
128             return false;
129         }
130     }
131     return true;
132 }
133 
134 struct SelectTrie
135 {
136     SelectTrie() = default;
137 
138     // Inserts a $select value; returns false if the nestedProperty is illegal.
139     bool insertNode(std::string_view nestedProperty)
140     {
141         if (nestedProperty.empty())
142         {
143             return false;
144         }
145         SelectTrieNode* currNode = &root;
146         size_t index = nestedProperty.find_first_of('/');
147         while (!nestedProperty.empty())
148         {
149             std::string_view property = nestedProperty.substr(0, index);
150             if (!isSelectedPropertyAllowed(property))
151             {
152                 return false;
153             }
154             currNode = currNode->emplace(property);
155             if (index == std::string::npos)
156             {
157                 break;
158             }
159             nestedProperty.remove_prefix(index + 1);
160             index = nestedProperty.find_first_of('/');
161         }
162         currNode->setToSelected();
163         return true;
164     }
165 
166     SelectTrieNode root;
167 };
168 
169 // The struct stores the parsed query parameters of the default Redfish route.
170 struct Query
171 {
172     // Only
173     bool isOnly = false;
174 
175     // Expand
176     uint8_t expandLevel = 0;
177     ExpandType expandType = ExpandType::None;
178 
179     // Skip
180     std::optional<size_t> skip = std::nullopt;
181 
182     // Top
183     static constexpr size_t maxTop = 1000; // Max entries a response contain
184     std::optional<size_t> top = std::nullopt;
185 
186     // Filter
187     std::optional<filter_ast::LogicalAnd> filter = std::nullopt;
188 
189     // Select
190     // Unclear how to make this use structured initialization without this.
191     // Might be a tidy bug?  Ignore for now
192     // NOLINTNEXTLINE(readability-redundant-member-init)
193     SelectTrie selectTrie{};
194 };
195 
196 // The struct defines how resource handlers in redfish-core/lib/ can handle
197 // query parameters themselves, so that the default Redfish route will delegate
198 // the processing.
199 struct QueryCapabilities
200 {
201     bool canDelegateOnly = false;
202     bool canDelegateTop = false;
203     bool canDelegateSkip = false;
204     uint8_t canDelegateExpandLevel = 0;
205     bool canDelegateSelect = false;
206 };
207 
208 // Delegates query parameters according to the given |queryCapabilities|
209 // This function doesn't check query parameter conflicts since the parse
210 // function will take care of it.
211 // Returns a delegated query object which can be used by individual resource
212 // handlers so that handlers don't need to query again.
213 inline Query delegate(const QueryCapabilities& queryCapabilities, Query& query)
214 {
215     Query delegated{};
216     // delegate only
217     if (query.isOnly && queryCapabilities.canDelegateOnly)
218     {
219         delegated.isOnly = true;
220         query.isOnly = false;
221     }
222     // delegate expand as much as we can
223     if (query.expandType != ExpandType::None)
224     {
225         delegated.expandType = query.expandType;
226         if (query.expandLevel <= queryCapabilities.canDelegateExpandLevel)
227         {
228             query.expandType = ExpandType::None;
229             delegated.expandLevel = query.expandLevel;
230             query.expandLevel = 0;
231         }
232         else
233         {
234             delegated.expandLevel = queryCapabilities.canDelegateExpandLevel;
235         }
236     }
237 
238     // delegate top
239     if (query.top && queryCapabilities.canDelegateTop)
240     {
241         delegated.top = query.top;
242         query.top = std::nullopt;
243     }
244 
245     // delegate skip
246     if (query.skip && queryCapabilities.canDelegateSkip)
247     {
248         delegated.skip = query.skip;
249         query.skip = 0;
250     }
251 
252     // delegate select
253     if (!query.selectTrie.root.empty() && queryCapabilities.canDelegateSelect)
254     {
255         delegated.selectTrie = std::move(query.selectTrie);
256         query.selectTrie.root.clear();
257     }
258     return delegated;
259 }
260 
261 inline bool getExpandType(std::string_view value, Query& query)
262 {
263     if (value.empty())
264     {
265         return false;
266     }
267     switch (value[0])
268     {
269         case '*':
270             query.expandType = ExpandType::Both;
271             break;
272         case '.':
273             query.expandType = ExpandType::NotLinks;
274             break;
275         case '~':
276             query.expandType = ExpandType::Links;
277             break;
278         default:
279             return false;
280     }
281     value.remove_prefix(1);
282     if (value.empty())
283     {
284         query.expandLevel = 1;
285         return true;
286     }
287     constexpr std::string_view levels = "($levels=";
288     if (!value.starts_with(levels))
289     {
290         return false;
291     }
292     value.remove_prefix(levels.size());
293 
294     auto it = std::from_chars(value.begin(), value.end(), query.expandLevel);
295     if (it.ec != std::errc())
296     {
297         return false;
298     }
299     value.remove_prefix(
300         static_cast<size_t>(std::distance(value.begin(), it.ptr)));
301     return value == ")";
302 }
303 
304 enum class QueryError
305 {
306     Ok,
307     OutOfRange,
308     ValueFormat,
309 };
310 
311 inline QueryError getNumericParam(std::string_view value, size_t& param)
312 {
313     std::from_chars_result r =
314         std::from_chars(value.begin(), value.end(), param);
315 
316     // If the number wasn't representable in the type, it's out of range
317     if (r.ec == std::errc::result_out_of_range)
318     {
319         return QueryError::OutOfRange;
320     }
321     // All other errors are value format
322     if (r.ec != std::errc())
323     {
324         return QueryError::ValueFormat;
325     }
326     return QueryError::Ok;
327 }
328 
329 inline QueryError getSkipParam(std::string_view value, Query& query)
330 {
331     return getNumericParam(value, query.skip.emplace());
332 }
333 
334 inline QueryError getTopParam(std::string_view value, Query& query)
335 {
336     QueryError ret = getNumericParam(value, query.top.emplace());
337     if (ret != QueryError::Ok)
338     {
339         return ret;
340     }
341 
342     // Range check for sanity.
343     if (query.top > Query::maxTop)
344     {
345         return QueryError::OutOfRange;
346     }
347 
348     return QueryError::Ok;
349 }
350 
351 // Parses and validates the $select parameter.
352 // As per OData URL Conventions and Redfish Spec, the $select values shall be
353 // comma separated Resource Path
354 // Ref:
355 // 1. https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
356 // 2.
357 // https://docs.oasis-open.org/odata/odata/v4.01/os/abnf/odata-abnf-construction-rules.txt
358 inline bool getSelectParam(std::string_view value, Query& query)
359 {
360     std::vector<std::string> properties;
361     bmcweb::split(properties, value, ',');
362     if (properties.empty())
363     {
364         return false;
365     }
366     // These a magic number, but with it it's less likely that this code
367     // introduces CVE; e.g., too large properties crash the service.
368     constexpr int maxNumProperties = 10;
369     if (properties.size() > maxNumProperties)
370     {
371         return false;
372     }
373     for (const auto& property : properties)
374     {
375         if (!query.selectTrie.insertNode(property))
376         {
377             return false;
378         }
379     }
380     return true;
381 }
382 
383 // Parses and validates the $filter parameter.
384 inline bool getFilterParam(std::string_view value, Query& query)
385 {
386     query.filter = parseFilter(value);
387     return query.filter.has_value();
388 }
389 
390 inline std::optional<Query>
391     parseParameters(boost::urls::params_view urlParams, crow::Response& res)
392 {
393     Query ret{};
394     for (const boost::urls::params_view::value_type& it : urlParams)
395     {
396         if (it.key == "only")
397         {
398             if (!it.value.empty())
399             {
400                 messages::queryParameterValueFormatError(res, it.value, it.key);
401                 return std::nullopt;
402             }
403             ret.isOnly = true;
404         }
405         else if (it.key == "$expand" && BMCWEB_INSECURE_ENABLE_REDFISH_QUERY)
406         {
407             if (!getExpandType(it.value, ret))
408             {
409                 messages::queryParameterValueFormatError(res, it.value, it.key);
410                 return std::nullopt;
411             }
412         }
413         else if (it.key == "$top")
414         {
415             QueryError topRet = getTopParam(it.value, ret);
416             if (topRet == QueryError::ValueFormat)
417             {
418                 messages::queryParameterValueFormatError(res, it.value, it.key);
419                 return std::nullopt;
420             }
421             if (topRet == QueryError::OutOfRange)
422             {
423                 messages::queryParameterOutOfRange(
424                     res, it.value, "$top",
425                     "0-" + std::to_string(Query::maxTop));
426                 return std::nullopt;
427             }
428         }
429         else if (it.key == "$skip")
430         {
431             QueryError topRet = getSkipParam(it.value, ret);
432             if (topRet == QueryError::ValueFormat)
433             {
434                 messages::queryParameterValueFormatError(res, it.value, it.key);
435                 return std::nullopt;
436             }
437             if (topRet == QueryError::OutOfRange)
438             {
439                 messages::queryParameterOutOfRange(
440                     res, it.value, it.key,
441                     "0-" + std::to_string(std::numeric_limits<size_t>::max()));
442                 return std::nullopt;
443             }
444         }
445         else if (it.key == "$select")
446         {
447             if (!getSelectParam(it.value, ret))
448             {
449                 messages::queryParameterValueFormatError(res, it.value, it.key);
450                 return std::nullopt;
451             }
452         }
453         else if (it.key == "$filter" && BMCWEB_INSECURE_ENABLE_REDFISH_QUERY)
454         {
455             if (!getFilterParam(it.value, ret))
456             {
457                 messages::queryParameterValueFormatError(res, it.value, it.key);
458                 return std::nullopt;
459             }
460         }
461         else
462         {
463             // Intentionally ignore other errors Redfish spec, 7.3.1
464             if (it.key.starts_with("$"))
465             {
466                 // Services shall return... The HTTP 501 Not Implemented
467                 // status code for any unsupported query parameters that
468                 // start with $ .
469                 messages::queryParameterValueFormatError(res, it.value, it.key);
470                 res.result(boost::beast::http::status::not_implemented);
471                 return std::nullopt;
472             }
473             // "Shall ignore unknown or unsupported query parameters that do
474             // not begin with $ ."
475         }
476     }
477 
478     if (ret.expandType != ExpandType::None && !ret.selectTrie.root.empty())
479     {
480         messages::queryCombinationInvalid(res);
481         return std::nullopt;
482     }
483 
484     return ret;
485 }
486 
487 inline bool processOnly(crow::App& app, crow::Response& res,
488                         std::function<void(crow::Response&)>& completionHandler)
489 {
490     BMCWEB_LOG_DEBUG("Processing only query param");
491     auto itMembers = res.jsonValue.find("Members");
492     if (itMembers == res.jsonValue.end())
493     {
494         messages::queryNotSupportedOnResource(res);
495         completionHandler(res);
496         return false;
497     }
498     auto itMemBegin = itMembers->begin();
499     if (itMemBegin == itMembers->end() || itMembers->size() != 1)
500     {
501         BMCWEB_LOG_DEBUG(
502             "Members contains {} element, returning full collection.",
503             itMembers->size());
504         completionHandler(res);
505         return false;
506     }
507 
508     auto itUrl = itMemBegin->find("@odata.id");
509     if (itUrl == itMemBegin->end())
510     {
511         BMCWEB_LOG_DEBUG("No found odata.id");
512         messages::internalError(res);
513         completionHandler(res);
514         return false;
515     }
516     const std::string* url = itUrl->get_ptr<const std::string*>();
517     if (url == nullptr)
518     {
519         BMCWEB_LOG_DEBUG("@odata.id wasn't a string????");
520         messages::internalError(res);
521         completionHandler(res);
522         return false;
523     }
524     // TODO(Ed) copy request headers?
525     // newReq.session = req.session;
526     std::error_code ec;
527     auto newReq = std::make_shared<crow::Request>(
528         crow::Request::Body{boost::beast::http::verb::get, *url, 11}, ec);
529     if (ec)
530     {
531         messages::internalError(res);
532         completionHandler(res);
533         return false;
534     }
535 
536     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
537     BMCWEB_LOG_DEBUG("setting completion handler on {}",
538                      logPtr(&asyncResp->res));
539     asyncResp->res.setCompleteRequestHandler(std::move(completionHandler));
540     app.handle(newReq, asyncResp);
541     return true;
542 }
543 
544 struct ExpandNode
545 {
546     nlohmann::json::json_pointer location;
547     std::string uri;
548 
549     bool operator==(const ExpandNode& other) const
550     {
551         return location == other.location && uri == other.uri;
552     }
553 };
554 
555 inline void findNavigationReferencesInArrayRecursive(
556     ExpandType eType, nlohmann::json::array_t& array,
557     const nlohmann::json::json_pointer& jsonPtr, int depth, int skipDepth,
558     bool inLinks, std::vector<ExpandNode>& out);
559 
560 inline void findNavigationReferencesInObjectRecursive(
561     ExpandType eType, nlohmann::json::object_t& obj,
562     const nlohmann::json::json_pointer& jsonPtr, int depth, int skipDepth,
563     bool inLinks, std::vector<ExpandNode>& out);
564 
565 // Walks a json object looking for Redfish NavigationReference entries that
566 // might need resolved.  It recursively walks the jsonResponse object, looking
567 // for links at every level, and returns a list (out) of locations within the
568 // tree that need to be expanded.  The current json pointer location p is passed
569 // in to reference the current node that's being expanded, so it can be combined
570 // with the keys from the jsonResponse object
571 inline void findNavigationReferencesRecursive(
572     ExpandType eType, nlohmann::json& jsonResponse,
573     const nlohmann::json::json_pointer& jsonPtr, int depth, int skipDepth,
574     bool inLinks, std::vector<ExpandNode>& out)
575 {
576     // If no expand is needed, return early
577     if (eType == ExpandType::None)
578     {
579         return;
580     }
581 
582     nlohmann::json::array_t* array =
583         jsonResponse.get_ptr<nlohmann::json::array_t*>();
584     if (array != nullptr)
585     {
586         findNavigationReferencesInArrayRecursive(eType, *array, jsonPtr, depth,
587                                                  skipDepth, inLinks, out);
588     }
589     nlohmann::json::object_t* obj =
590         jsonResponse.get_ptr<nlohmann::json::object_t*>();
591     if (obj == nullptr)
592     {
593         return;
594     }
595     findNavigationReferencesInObjectRecursive(eType, *obj, jsonPtr, depth,
596                                               skipDepth, inLinks, out);
597 }
598 
599 inline void findNavigationReferencesInArrayRecursive(
600     ExpandType eType, nlohmann::json::array_t& array,
601     const nlohmann::json::json_pointer& jsonPtr, int depth, int skipDepth,
602     bool inLinks, std::vector<ExpandNode>& out)
603 {
604     size_t index = 0;
605     // For arrays, walk every element in the array
606     for (auto& element : array)
607     {
608         nlohmann::json::json_pointer newPtr = jsonPtr / index;
609         BMCWEB_LOG_DEBUG("Traversing response at {}", newPtr.to_string());
610         findNavigationReferencesRecursive(eType, element, newPtr, depth,
611                                           skipDepth, inLinks, out);
612         index++;
613     }
614 }
615 
616 inline void findNavigationReferencesInObjectRecursive(
617     ExpandType eType, nlohmann::json::object_t& obj,
618     const nlohmann::json::json_pointer& jsonPtr, int depth, int skipDepth,
619     bool inLinks, std::vector<ExpandNode>& out)
620 {
621     // Navigation References only ever have a single element
622     if (obj.size() == 1)
623     {
624         if (obj.begin()->first == "@odata.id")
625         {
626             const std::string* uri =
627                 obj.begin()->second.get_ptr<const std::string*>();
628             if (uri != nullptr)
629             {
630                 BMCWEB_LOG_DEBUG("Found {} at {}", *uri, jsonPtr.to_string());
631                 if (skipDepth == 0)
632                 {
633                     out.push_back({jsonPtr, *uri});
634                 }
635                 return;
636             }
637         }
638     }
639 
640     int newDepth = depth;
641     auto odataId = obj.find("@odata.id");
642     if (odataId != obj.end())
643     {
644         // The Redfish spec requires all resources to include the resource
645         // identifier.  If the object has multiple elements and one of them is
646         // "@odata.id" then that means we have entered a new level / expanded
647         // resource.  We need to stop traversing if we're already at the desired
648         // depth
649         if (obj.size() > 1)
650         {
651             if (depth == 0)
652             {
653                 return;
654             }
655             if (skipDepth > 0)
656             {
657                 skipDepth--;
658             }
659         }
660 
661         if (skipDepth == 0)
662         {
663             newDepth--;
664         }
665     }
666 
667     // Loop the object and look for links
668     for (auto& element : obj)
669     {
670         bool localInLinks = inLinks;
671         if (!localInLinks)
672         {
673             // Check if this is a links node
674             localInLinks = element.first == "Links";
675         }
676         // Only traverse the parts of the tree the user asked for
677         // Per section 7.3 of the redfish specification
678         if (localInLinks && eType == ExpandType::NotLinks)
679         {
680             continue;
681         }
682         if (!localInLinks && eType == ExpandType::Links)
683         {
684             continue;
685         }
686         nlohmann::json::json_pointer newPtr = jsonPtr / element.first;
687         BMCWEB_LOG_DEBUG("Traversing response at {}", newPtr);
688 
689         findNavigationReferencesRecursive(eType, element.second, newPtr,
690                                           newDepth, skipDepth, localInLinks,
691                                           out);
692     }
693 }
694 
695 // TODO: When aggregation is enabled and we receive a partially expanded
696 // response we may need need additional handling when the original URI was
697 // up tree from a top level collection.
698 // Isn't a concern until https://gerrit.openbmc.org/c/openbmc/bmcweb/+/60556
699 // lands.  May want to avoid forwarding query params when request is uptree from
700 // a top level collection.
701 inline std::vector<ExpandNode> findNavigationReferences(
702     ExpandType eType, int depth, int skipDepth, nlohmann::json& jsonResponse)
703 {
704     std::vector<ExpandNode> ret;
705     const nlohmann::json::json_pointer root = nlohmann::json::json_pointer("");
706     // SkipDepth +1 since we are skipping the root by default.
707     findNavigationReferencesRecursive(eType, jsonResponse, root, depth,
708                                       skipDepth + 1, false, ret);
709     return ret;
710 }
711 
712 // Formats a query parameter string for the sub-query.
713 // Returns std::nullopt on failures.
714 // This function shall handle $select when it is added.
715 // There is no need to handle parameters that's not compatible with $expand,
716 // e.g., $only, since this function will only be called in side $expand handlers
717 inline std::optional<std::string> formatQueryForExpand(const Query& query)
718 {
719     // query.expandLevel<=1: no need to do subqueries
720     if (query.expandLevel <= 1)
721     {
722         return "";
723     }
724     std::string str = "?$expand=";
725     switch (query.expandType)
726     {
727         case ExpandType::Links:
728             str += '~';
729             break;
730         case ExpandType::NotLinks:
731             str += '.';
732             break;
733         case ExpandType::Both:
734             str += '*';
735             break;
736         case ExpandType::None:
737             return "";
738         default:
739             return std::nullopt;
740     }
741     str += "($levels=";
742     str += std::to_string(query.expandLevel - 1);
743     str += ')';
744     return str;
745 }
746 
747 // Propagates the worst error code to the final response.
748 // The order of error code is (from high to low)
749 // 500 Internal Server Error
750 // 511 Network Authentication Required
751 // 510 Not Extended
752 // 508 Loop Detected
753 // 507 Insufficient Storage
754 // 506 Variant Also Negotiates
755 // 505 HTTP Version Not Supported
756 // 504 Gateway Timeout
757 // 503 Service Unavailable
758 // 502 Bad Gateway
759 // 501 Not Implemented
760 // 401 Unauthorized
761 // 451 - 409 Error codes (not listed explicitly)
762 // 408 Request Timeout
763 // 407 Proxy Authentication Required
764 // 406 Not Acceptable
765 // 405 Method Not Allowed
766 // 404 Not Found
767 // 403 Forbidden
768 // 402 Payment Required
769 // 400 Bad Request
770 inline unsigned propogateErrorCode(unsigned finalCode, unsigned subResponseCode)
771 {
772     // We keep a explicit list for error codes that this project often uses
773     // Higher priority codes are in lower indexes
774     constexpr std::array<unsigned, 13> orderedCodes = {
775         500, 507, 503, 502, 501, 401, 412, 409, 406, 405, 404, 403, 400};
776     size_t finalCodeIndex = std::numeric_limits<size_t>::max();
777     size_t subResponseCodeIndex = std::numeric_limits<size_t>::max();
778     for (size_t i = 0; i < orderedCodes.size(); ++i)
779     {
780         if (orderedCodes[i] == finalCode)
781         {
782             finalCodeIndex = i;
783         }
784         if (orderedCodes[i] == subResponseCode)
785         {
786             subResponseCodeIndex = i;
787         }
788     }
789     if (finalCodeIndex != std::numeric_limits<size_t>::max() &&
790         subResponseCodeIndex != std::numeric_limits<size_t>::max())
791     {
792         return finalCodeIndex <= subResponseCodeIndex
793                    ? finalCode
794                    : subResponseCode;
795     }
796     if (subResponseCode == 500 || finalCode == 500)
797     {
798         return 500;
799     }
800     if (subResponseCode > 500 || finalCode > 500)
801     {
802         return std::max(finalCode, subResponseCode);
803     }
804     if (subResponseCode == 401)
805     {
806         return subResponseCode;
807     }
808     return std::max(finalCode, subResponseCode);
809 }
810 
811 // Propagates all error messages into |finalResponse|
812 inline void propogateError(crow::Response& finalResponse,
813                            crow::Response& subResponse)
814 {
815     // no errors
816     if (subResponse.resultInt() >= 200 && subResponse.resultInt() < 400)
817     {
818         return;
819     }
820     messages::moveErrorsToErrorJson(finalResponse.jsonValue,
821                                     subResponse.jsonValue);
822     finalResponse.result(
823         propogateErrorCode(finalResponse.resultInt(), subResponse.resultInt()));
824 }
825 
826 class MultiAsyncResp : public std::enable_shared_from_this<MultiAsyncResp>
827 {
828   public:
829     // This object takes a single asyncResp object as the "final" one, then
830     // allows callers to attach sub-responses within the json tree that need
831     // to be executed and filled into their appropriate locations.  This
832     // class manages the final "merge" of the json resources.
833     MultiAsyncResp(crow::App& appIn,
834                    std::shared_ptr<bmcweb::AsyncResp> finalResIn) :
835         app(appIn), finalRes(std::move(finalResIn))
836     {}
837 
838     void addAwaitingResponse(
839         const std::shared_ptr<bmcweb::AsyncResp>& res,
840         const nlohmann::json::json_pointer& finalExpandLocation)
841     {
842         res->res.setCompleteRequestHandler(std::bind_front(
843             placeResultStatic, shared_from_this(), finalExpandLocation));
844     }
845 
846     void placeResult(const nlohmann::json::json_pointer& locationToPlace,
847                      crow::Response& res)
848     {
849         BMCWEB_LOG_DEBUG("placeResult for {}", locationToPlace);
850         propogateError(finalRes->res, res);
851         if (!res.jsonValue.is_object() || res.jsonValue.empty())
852         {
853             return;
854         }
855         nlohmann::json& finalObj = finalRes->res.jsonValue[locationToPlace];
856         finalObj = std::move(res.jsonValue);
857     }
858 
859     // Handles the very first level of Expand, and starts a chain of sub-queries
860     // for deeper levels.
861     void startQuery(const Query& query, const Query& delegated)
862     {
863         std::vector<ExpandNode> nodes = findNavigationReferences(
864             query.expandType, query.expandLevel, delegated.expandLevel,
865             finalRes->res.jsonValue);
866         BMCWEB_LOG_DEBUG("{} nodes to traverse", nodes.size());
867         const std::optional<std::string> queryStr = formatQueryForExpand(query);
868         if (!queryStr)
869         {
870             messages::internalError(finalRes->res);
871             return;
872         }
873         for (const ExpandNode& node : nodes)
874         {
875             const std::string subQuery = node.uri + *queryStr;
876             BMCWEB_LOG_DEBUG("URL of subquery:  {}", subQuery);
877             std::error_code ec;
878             auto newReq = std::make_shared<crow::Request>(
879                 crow::Request::Body{boost::beast::http::verb::get, subQuery,
880                                     11},
881                 ec);
882             if (ec)
883             {
884                 messages::internalError(finalRes->res);
885                 return;
886             }
887 
888             auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
889             BMCWEB_LOG_DEBUG("setting completion handler on {}",
890                              logPtr(&asyncResp->res));
891 
892             addAwaitingResponse(asyncResp, node.location);
893             app.handle(newReq, asyncResp);
894         }
895     }
896 
897   private:
898     static void
899         placeResultStatic(const std::shared_ptr<MultiAsyncResp>& multi,
900                           const nlohmann::json::json_pointer& locationToPlace,
901                           crow::Response& res)
902     {
903         multi->placeResult(locationToPlace, res);
904     }
905 
906     crow::App& app;
907     std::shared_ptr<bmcweb::AsyncResp> finalRes;
908 };
909 
910 inline void processTopAndSkip(const Query& query, crow::Response& res)
911 {
912     if (!query.skip && !query.top)
913     {
914         // No work to do.
915         return;
916     }
917     nlohmann::json::object_t* obj =
918         res.jsonValue.get_ptr<nlohmann::json::object_t*>();
919     if (obj == nullptr)
920     {
921         // Shouldn't be possible.  All responses should be objects.
922         messages::internalError(res);
923         return;
924     }
925 
926     BMCWEB_LOG_DEBUG("Handling top/skip");
927     nlohmann::json::object_t::iterator members = obj->find("Members");
928     if (members == obj->end())
929     {
930         // From the Redfish specification 7.3.1
931         // ... the HTTP 400 Bad Request status code with the
932         // QueryNotSupportedOnResource message from the Base Message Registry
933         // for any supported query parameters that apply only to resource
934         // collections but are used on singular resources.
935         messages::queryNotSupportedOnResource(res);
936         return;
937     }
938 
939     nlohmann::json::array_t* arr =
940         members->second.get_ptr<nlohmann::json::array_t*>();
941     if (arr == nullptr)
942     {
943         messages::internalError(res);
944         return;
945     }
946 
947     if (query.skip)
948     {
949         // Per section 7.3.1 of the Redfish specification, $skip is run before
950         // $top Can only skip as many values as we have
951         size_t skip = std::min(arr->size(), *query.skip);
952         arr->erase(arr->begin(), arr->begin() + static_cast<ssize_t>(skip));
953     }
954     if (query.top)
955     {
956         size_t top = std::min(arr->size(), *query.top);
957         arr->erase(arr->begin() + static_cast<ssize_t>(top), arr->end());
958     }
959 }
960 
961 // Given a JSON subtree |currRoot|, this function erases leaves whose keys are
962 // not in the |currNode| Trie node.
963 inline void recursiveSelect(nlohmann::json& currRoot,
964                             const SelectTrieNode& currNode)
965 {
966     nlohmann::json::object_t* object =
967         currRoot.get_ptr<nlohmann::json::object_t*>();
968     if (object != nullptr)
969     {
970         BMCWEB_LOG_DEBUG("Current JSON is an object");
971         auto it = currRoot.begin();
972         while (it != currRoot.end())
973         {
974             auto nextIt = std::next(it);
975             BMCWEB_LOG_DEBUG("key={}", it.key());
976             const SelectTrieNode* nextNode = currNode.find(it.key());
977             // Per the Redfish spec section 7.3.3, the service shall select
978             // certain properties as if $select was omitted. This applies to
979             // every TrieNode that contains leaves and the root.
980             constexpr std::array<std::string_view, 5> reservedProperties = {
981                 "@odata.id", "@odata.type", "@odata.context", "@odata.etag",
982                 "error"};
983             bool reserved = std::ranges::find(reservedProperties, it.key()) !=
984                             reservedProperties.end();
985             if (reserved || (nextNode != nullptr && nextNode->isSelected()))
986             {
987                 it = nextIt;
988                 continue;
989             }
990             if (nextNode != nullptr)
991             {
992                 BMCWEB_LOG_DEBUG("Recursively select: {}", it.key());
993                 recursiveSelect(*it, *nextNode);
994                 it = nextIt;
995                 continue;
996             }
997             BMCWEB_LOG_DEBUG("{} is getting removed!", it.key());
998             it = currRoot.erase(it);
999         }
1000     }
1001     nlohmann::json::array_t* array =
1002         currRoot.get_ptr<nlohmann::json::array_t*>();
1003     if (array != nullptr)
1004     {
1005         BMCWEB_LOG_DEBUG("Current JSON is an array");
1006         // Array index is omitted, so reuse the same Trie node
1007         for (nlohmann::json& nextRoot : *array)
1008         {
1009             recursiveSelect(nextRoot, currNode);
1010         }
1011     }
1012 }
1013 
1014 // The current implementation of $select still has the following TODOs due to
1015 //  ambiguity and/or complexity.
1016 // 1. combined with $expand; https://github.com/DMTF/Redfish/issues/5058 was
1017 // created for clarification.
1018 // 2. respect the full odata spec; e.g., deduplication, namespace, star (*),
1019 // etc.
1020 inline void processSelect(crow::Response& intermediateResponse,
1021                           const SelectTrieNode& trieRoot)
1022 {
1023     BMCWEB_LOG_DEBUG("Process $select quary parameter");
1024     recursiveSelect(intermediateResponse.jsonValue, trieRoot);
1025 }
1026 
1027 inline void
1028     processAllParams(crow::App& app, const Query& query, const Query& delegated,
1029                      std::function<void(crow::Response&)>& completionHandler,
1030                      crow::Response& intermediateResponse)
1031 {
1032     if (!completionHandler)
1033     {
1034         BMCWEB_LOG_DEBUG("Function was invalid?");
1035         return;
1036     }
1037 
1038     BMCWEB_LOG_DEBUG("Processing query params");
1039     // If the request failed, there's no reason to even try to run query
1040     // params.
1041     if (intermediateResponse.resultInt() < 200 ||
1042         intermediateResponse.resultInt() >= 400)
1043     {
1044         completionHandler(intermediateResponse);
1045         return;
1046     }
1047     if (query.isOnly)
1048     {
1049         processOnly(app, intermediateResponse, completionHandler);
1050         return;
1051     }
1052 
1053     if (query.top || query.skip)
1054     {
1055         processTopAndSkip(query, intermediateResponse);
1056     }
1057 
1058     if (query.expandType != ExpandType::None)
1059     {
1060         BMCWEB_LOG_DEBUG("Executing expand query");
1061         auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
1062             std::move(intermediateResponse));
1063 
1064         asyncResp->res.setCompleteRequestHandler(std::move(completionHandler));
1065         auto multi = std::make_shared<MultiAsyncResp>(app, asyncResp);
1066         multi->startQuery(query, delegated);
1067         return;
1068     }
1069 
1070     if (query.filter)
1071     {
1072         applyFilterToCollection(intermediateResponse.jsonValue, *query.filter);
1073     }
1074 
1075     // According to Redfish Spec Section 7.3.1, $select is the last parameter to
1076     // to process
1077     if (!query.selectTrie.root.empty())
1078     {
1079         processSelect(intermediateResponse, query.selectTrie.root);
1080     }
1081 
1082     completionHandler(intermediateResponse);
1083 }
1084 
1085 } // namespace query_param
1086 } // namespace redfish
1087