xref: /openbmc/bmcweb/http/http_response.hpp (revision 499b5b4dcbf3a936e65694de24219e65e3268e4d)
104e438cbSEd Tanous #pragma once
2b2896149SEd Tanous #include "http_body.hpp"
304e438cbSEd Tanous #include "logging.hpp"
43ccb3adbSEd Tanous #include "utils/hex_utils.hpp"
504e438cbSEd Tanous 
604e438cbSEd Tanous #include <boost/beast/http/message.hpp>
7faf100f9SEd Tanous #include <nlohmann/json.hpp>
804e438cbSEd Tanous 
98a9a25c8SEd Tanous #include <optional>
1004e438cbSEd Tanous #include <string>
118a9a25c8SEd Tanous #include <string_view>
128e3f7032SAbhilash Raju #include <utility>
1304e438cbSEd Tanous namespace crow
1404e438cbSEd Tanous {
1504e438cbSEd Tanous 
1604e438cbSEd Tanous template <typename Adaptor, typename Handler>
1704e438cbSEd Tanous class Connection;
1804e438cbSEd Tanous 
1927b0cf90SEd Tanous namespace http = boost::beast::http;
2027b0cf90SEd Tanous 
2104e438cbSEd Tanous struct Response
2204e438cbSEd Tanous {
2304e438cbSEd Tanous     template <typename Adaptor, typename Handler>
2404e438cbSEd Tanous     friend class crow::Connection;
2504e438cbSEd Tanous 
26b2896149SEd Tanous     http::response<bmcweb::HttpBody> response;
2704e438cbSEd Tanous 
2804e438cbSEd Tanous     nlohmann::json jsonValue;
2927b0cf90SEd Tanous     using fields_type = http::header<false, http::fields>;
3027b0cf90SEd Tanous     fields_type& fields()
3127b0cf90SEd Tanous     {
3252e31629SEd Tanous         return response.base();
3327b0cf90SEd Tanous     }
3427b0cf90SEd Tanous 
3527b0cf90SEd Tanous     const fields_type& fields() const
3627b0cf90SEd Tanous     {
3752e31629SEd Tanous         return response.base();
3827b0cf90SEd Tanous     }
3904e438cbSEd Tanous 
4026ccae32SEd Tanous     void addHeader(std::string_view key, std::string_view value)
4104e438cbSEd Tanous     {
4227b0cf90SEd Tanous         fields().insert(key, value);
4304e438cbSEd Tanous     }
4404e438cbSEd Tanous 
4527b0cf90SEd Tanous     void addHeader(http::field key, std::string_view value)
4604e438cbSEd Tanous     {
4727b0cf90SEd Tanous         fields().insert(key, value);
48994fd86aSEd Tanous     }
49994fd86aSEd Tanous 
5027b0cf90SEd Tanous     void clearHeader(http::field key)
51994fd86aSEd Tanous     {
5227b0cf90SEd Tanous         fields().erase(key);
5304e438cbSEd Tanous     }
5404e438cbSEd Tanous 
5552e31629SEd Tanous     Response() = default;
5613548d85SEd Tanous     Response(Response&& res) noexcept :
5727b0cf90SEd Tanous         response(std::move(res.response)), jsonValue(std::move(res.jsonValue)),
5827b0cf90SEd Tanous         completed(res.completed)
5913548d85SEd Tanous     {
6013548d85SEd Tanous         // See note in operator= move handler for why this is needed.
6113548d85SEd Tanous         if (!res.completed)
6213548d85SEd Tanous         {
6313548d85SEd Tanous             completeRequestHandler = std::move(res.completeRequestHandler);
6413548d85SEd Tanous             res.completeRequestHandler = nullptr;
6513548d85SEd Tanous         }
6613548d85SEd Tanous     }
6713548d85SEd Tanous 
68ecd6a3a2SEd Tanous     ~Response() = default;
69ecd6a3a2SEd Tanous 
70ecd6a3a2SEd Tanous     Response(const Response&) = delete;
7104e438cbSEd Tanous     Response& operator=(const Response& r) = delete;
7204e438cbSEd Tanous 
7304e438cbSEd Tanous     Response& operator=(Response&& r) noexcept
7404e438cbSEd Tanous     {
7562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Moving response containers; this: {}; other: {}",
7662598e31SEd Tanous                          logPtr(this), logPtr(&r));
7772374eb7SNan Zhou         if (this == &r)
7872374eb7SNan Zhou         {
7972374eb7SNan Zhou             return *this;
8072374eb7SNan Zhou         }
8127b0cf90SEd Tanous         response = std::move(r.response);
8204e438cbSEd Tanous         jsonValue = std::move(r.jsonValue);
83*499b5b4dSEd Tanous         expectedHash = std::move(r.expectedHash);
8413548d85SEd Tanous 
8513548d85SEd Tanous         // Only need to move completion handler if not already completed
8613548d85SEd Tanous         // Note, there are cases where we might move out of a Response object
8713548d85SEd Tanous         // while in a completion handler for that response object.  This check
8813548d85SEd Tanous         // is intended to prevent destructing the functor we are currently
8913548d85SEd Tanous         // executing from in that case.
9013548d85SEd Tanous         if (!r.completed)
9113548d85SEd Tanous         {
9272374eb7SNan Zhou             completeRequestHandler = std::move(r.completeRequestHandler);
9372374eb7SNan Zhou             r.completeRequestHandler = nullptr;
9413548d85SEd Tanous         }
9513548d85SEd Tanous         else
9613548d85SEd Tanous         {
9713548d85SEd Tanous             completeRequestHandler = nullptr;
9813548d85SEd Tanous         }
9913548d85SEd Tanous         completed = r.completed;
10004e438cbSEd Tanous         return *this;
10104e438cbSEd Tanous     }
10204e438cbSEd Tanous 
1033590bd1dSNan Zhou     void result(unsigned v)
1043590bd1dSNan Zhou     {
10527b0cf90SEd Tanous         fields().result(v);
1063590bd1dSNan Zhou     }
1073590bd1dSNan Zhou 
10827b0cf90SEd Tanous     void result(http::status v)
10904e438cbSEd Tanous     {
11027b0cf90SEd Tanous         fields().result(v);
11104e438cbSEd Tanous     }
11204e438cbSEd Tanous 
11327b0cf90SEd Tanous     void copyBody(const Response& res)
11404e438cbSEd Tanous     {
11552e31629SEd Tanous         response.body() = res.response.body();
11627b0cf90SEd Tanous     }
11727b0cf90SEd Tanous 
11827b0cf90SEd Tanous     http::status result() const
11927b0cf90SEd Tanous     {
12027b0cf90SEd Tanous         return fields().result();
12104e438cbSEd Tanous     }
12204e438cbSEd Tanous 
123039a47e3SCarson Labrado     unsigned resultInt() const
12404e438cbSEd Tanous     {
12527b0cf90SEd Tanous         return fields().result_int();
12604e438cbSEd Tanous     }
12704e438cbSEd Tanous 
128bb60f4deSEd Tanous     std::string_view reason() const
12904e438cbSEd Tanous     {
13027b0cf90SEd Tanous         return fields().reason();
13104e438cbSEd Tanous     }
13204e438cbSEd Tanous 
13304e438cbSEd Tanous     bool isCompleted() const noexcept
13404e438cbSEd Tanous     {
13504e438cbSEd Tanous         return completed;
13604e438cbSEd Tanous     }
13704e438cbSEd Tanous 
13827b0cf90SEd Tanous     const std::string* body()
13904e438cbSEd Tanous     {
14052e31629SEd Tanous         return &response.body().str();
14104e438cbSEd Tanous     }
14204e438cbSEd Tanous 
14346a81465SCarson Labrado     std::string_view getHeaderValue(std::string_view key) const
14446a81465SCarson Labrado     {
14527b0cf90SEd Tanous         return fields()[key];
14646a81465SCarson Labrado     }
14746a81465SCarson Labrado 
148*499b5b4dSEd Tanous     std::string_view getHeaderValue(boost::beast::http::field key) const
149*499b5b4dSEd Tanous     {
150*499b5b4dSEd Tanous         return fields()[key];
151*499b5b4dSEd Tanous     }
152*499b5b4dSEd Tanous 
15304e438cbSEd Tanous     void keepAlive(bool k)
15404e438cbSEd Tanous     {
15552e31629SEd Tanous         response.keep_alive(k);
15604e438cbSEd Tanous     }
15704e438cbSEd Tanous 
158bb60f4deSEd Tanous     bool keepAlive() const
15904e438cbSEd Tanous     {
16052e31629SEd Tanous         return response.keep_alive();
16127b0cf90SEd Tanous     }
16227b0cf90SEd Tanous 
16352e31629SEd Tanous     std::optional<uint64_t> size()
16452e31629SEd Tanous     {
16552e31629SEd Tanous         return response.body().payloadSize();
16652e31629SEd Tanous     }
16752e31629SEd Tanous 
16852e31629SEd Tanous     void preparePayload()
16927b0cf90SEd Tanous     {
17027b0cf90SEd Tanous         // This code is a throw-free equivalent to
17127b0cf90SEd Tanous         // beast::http::message::prepare_payload
17252e31629SEd Tanous         std::optional<uint64_t> pSize = response.body().payloadSize();
17352e31629SEd Tanous         if (!pSize)
17452e31629SEd Tanous         {
17552e31629SEd Tanous             return;
17652e31629SEd Tanous         }
17727b0cf90SEd Tanous         using http::status;
17827b0cf90SEd Tanous         using http::status_class;
17927b0cf90SEd Tanous         using http::to_status_class;
18027b0cf90SEd Tanous         bool is1XXReturn = to_status_class(result()) ==
18127b0cf90SEd Tanous                            status_class::informational;
18227b0cf90SEd Tanous         if (*pSize > 0 && (is1XXReturn || result() == status::no_content ||
18327b0cf90SEd Tanous                            result() == status::not_modified))
18427b0cf90SEd Tanous         {
18527b0cf90SEd Tanous             BMCWEB_LOG_CRITICAL("{} Response content provided but code was "
18627b0cf90SEd Tanous                                 "no-content or not_modified, which aren't "
18727b0cf90SEd Tanous                                 "allowed to have a body",
18827b0cf90SEd Tanous                                 logPtr(this));
18952e31629SEd Tanous             response.content_length(0);
19052e31629SEd Tanous             return;
19127b0cf90SEd Tanous         }
19252e31629SEd Tanous         response.content_length(*pSize);
19304e438cbSEd Tanous     }
19404e438cbSEd Tanous 
19504e438cbSEd Tanous     void clear()
19604e438cbSEd Tanous     {
19762598e31SEd Tanous         BMCWEB_LOG_DEBUG("{} Clearing response containers", logPtr(this));
19852e31629SEd Tanous         response.clear();
19906fc9bebSEd Tanous         response.body().clear();
20052e31629SEd Tanous 
201a6695a84SEd Tanous         jsonValue = nullptr;
20204e438cbSEd Tanous         completed = false;
203291d709dSEd Tanous         expectedHash = std::nullopt;
20404e438cbSEd Tanous     }
20504e438cbSEd Tanous 
2062d6cb56bSEd Tanous     std::string computeEtag() const
20704e438cbSEd Tanous     {
20889f18008SEd Tanous         // Only set etag if this request succeeded
20927b0cf90SEd Tanous         if (result() != http::status::ok)
21089f18008SEd Tanous         {
2112d6cb56bSEd Tanous             return "";
21289f18008SEd Tanous         }
2132d6cb56bSEd Tanous         // and the json response isn't empty
2142d6cb56bSEd Tanous         if (jsonValue.empty())
2152d6cb56bSEd Tanous         {
2162d6cb56bSEd Tanous             return "";
2172d6cb56bSEd Tanous         }
2182d6cb56bSEd Tanous         size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
2192d6cb56bSEd Tanous         return "\"" + intToHexString(hashval, 8) + "\"";
2202d6cb56bSEd Tanous     }
2212d6cb56bSEd Tanous 
22227b0cf90SEd Tanous     void write(std::string&& bodyPart)
22327b0cf90SEd Tanous     {
22452e31629SEd Tanous         response.body().str() = std::move(bodyPart);
22527b0cf90SEd Tanous     }
22627b0cf90SEd Tanous 
2272d6cb56bSEd Tanous     void end()
2282d6cb56bSEd Tanous     {
22904e438cbSEd Tanous         if (completed)
23004e438cbSEd Tanous         {
23162598e31SEd Tanous             BMCWEB_LOG_ERROR("{} Response was ended twice", logPtr(this));
23204e438cbSEd Tanous             return;
23304e438cbSEd Tanous         }
23404e438cbSEd Tanous         completed = true;
23562598e31SEd Tanous         BMCWEB_LOG_DEBUG("{} calling completion handler", logPtr(this));
23604e438cbSEd Tanous         if (completeRequestHandler)
23704e438cbSEd Tanous         {
23862598e31SEd Tanous             BMCWEB_LOG_DEBUG("{} completion handler was valid", logPtr(this));
23972374eb7SNan Zhou             completeRequestHandler(*this);
24004e438cbSEd Tanous         }
24104e438cbSEd Tanous     }
24204e438cbSEd Tanous 
24372374eb7SNan Zhou     void setCompleteRequestHandler(std::function<void(Response&)>&& handler)
2444147b8acSJohn Edward Broadbent     {
24562598e31SEd Tanous         BMCWEB_LOG_DEBUG("{} setting completion handler", logPtr(this));
24672374eb7SNan Zhou         completeRequestHandler = std::move(handler);
24713548d85SEd Tanous 
24813548d85SEd Tanous         // Now that we have a new completion handler attached, we're no longer
24913548d85SEd Tanous         // complete
25013548d85SEd Tanous         completed = false;
25172374eb7SNan Zhou     }
25272374eb7SNan Zhou 
25372374eb7SNan Zhou     std::function<void(Response&)> releaseCompleteRequestHandler()
25472374eb7SNan Zhou     {
25562598e31SEd Tanous         BMCWEB_LOG_DEBUG("{} releasing completion handler{}", logPtr(this),
25662598e31SEd Tanous                          static_cast<bool>(completeRequestHandler));
25772374eb7SNan Zhou         std::function<void(Response&)> ret = completeRequestHandler;
25872374eb7SNan Zhou         completeRequestHandler = nullptr;
25913548d85SEd Tanous         completed = true;
26072374eb7SNan Zhou         return ret;
26172374eb7SNan Zhou     }
26272374eb7SNan Zhou 
263291d709dSEd Tanous     void setHashAndHandleNotModified()
264291d709dSEd Tanous     {
265291d709dSEd Tanous         // Can only hash if we have content that's valid
26627b0cf90SEd Tanous         if (jsonValue.empty() || result() != http::status::ok)
267291d709dSEd Tanous         {
268291d709dSEd Tanous             return;
269291d709dSEd Tanous         }
270291d709dSEd Tanous         size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
271291d709dSEd Tanous         std::string hexVal = "\"" + intToHexString(hashval, 8) + "\"";
27227b0cf90SEd Tanous         addHeader(http::field::etag, hexVal);
273291d709dSEd Tanous         if (expectedHash && hexVal == *expectedHash)
274291d709dSEd Tanous         {
275a6695a84SEd Tanous             jsonValue = nullptr;
27627b0cf90SEd Tanous             result(http::status::not_modified);
277291d709dSEd Tanous         }
278291d709dSEd Tanous     }
279291d709dSEd Tanous 
280291d709dSEd Tanous     void setExpectedHash(std::string_view hash)
281291d709dSEd Tanous     {
282291d709dSEd Tanous         expectedHash = hash;
283291d709dSEd Tanous     }
284291d709dSEd Tanous 
285b5f288d2SAbhilash Raju     bool openFile(const std::filesystem::path& path,
286b5f288d2SAbhilash Raju                   bmcweb::EncodingType enc = bmcweb::EncodingType::Raw)
28727b0cf90SEd Tanous     {
28827b0cf90SEd Tanous         boost::beast::error_code ec;
28952e31629SEd Tanous         response.body().open(path.c_str(), boost::beast::file_mode::read, ec);
29052e31629SEd Tanous         response.body().encodingType = enc;
29127b0cf90SEd Tanous         if (ec)
29227b0cf90SEd Tanous         {
29352e31629SEd Tanous             BMCWEB_LOG_ERROR("Failed to open file {}", path.c_str());
29427b0cf90SEd Tanous             return false;
29527b0cf90SEd Tanous         }
296b5f288d2SAbhilash Raju         return true;
297b5f288d2SAbhilash Raju     }
298b5f288d2SAbhilash Raju 
299b5f288d2SAbhilash Raju     bool openFd(int fd, bmcweb::EncodingType enc = bmcweb::EncodingType::Raw)
300b5f288d2SAbhilash Raju     {
301b5f288d2SAbhilash Raju         boost::beast::error_code ec;
30252e31629SEd Tanous         response.body().encodingType = enc;
30352e31629SEd Tanous         response.body().setFd(fd, ec);
304b5f288d2SAbhilash Raju         if (ec)
305b5f288d2SAbhilash Raju         {
306b5f288d2SAbhilash Raju             BMCWEB_LOG_ERROR("Failed to set fd");
307b5f288d2SAbhilash Raju             return false;
308b5f288d2SAbhilash Raju         }
309b5f288d2SAbhilash Raju         return true;
310b5f288d2SAbhilash Raju     }
311b5f288d2SAbhilash Raju 
312b5f288d2SAbhilash Raju   private:
313291d709dSEd Tanous     std::optional<std::string> expectedHash;
31472374eb7SNan Zhou     bool completed = false;
31572374eb7SNan Zhou     std::function<void(Response&)> completeRequestHandler;
31604e438cbSEd Tanous };
31704e438cbSEd Tanous } // namespace crow
318