Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 2 | #include "http_file_body.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 4 | #include "utils/hex_utils.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 5 | |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 6 | #include <boost/beast/http/message.hpp> |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 7 | #include <boost/beast/http/message_generator.hpp> |
Ed Tanous | d4b6c66 | 2021-03-10 13:29:30 -0800 | [diff] [blame] | 8 | #include <boost/beast/http/string_body.hpp> |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 9 | #include <boost/variant2/variant.hpp> |
Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 10 | #include <nlohmann/json.hpp> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 12 | #include <optional> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 13 | #include <string> |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 14 | #include <string_view> |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 15 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 18 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 19 | template <typename Adaptor, typename Handler> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 20 | class Connection; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 21 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 22 | namespace http = boost::beast::http; |
| 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | struct Response |
| 25 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 26 | template <typename Adaptor, typename Handler> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | friend class crow::Connection; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 29 | using string_response = http::response<http::string_body>; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 30 | using file_response = http::response<bmcweb::FileBody>; |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 31 | |
| 32 | // Use boost variant2 because it doesn't have valueless by exception |
| 33 | boost::variant2::variant<string_response, file_response> response; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 34 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | nlohmann::json jsonValue; |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 36 | using fields_type = http::header<false, http::fields>; |
| 37 | fields_type& fields() |
| 38 | { |
| 39 | return boost::variant2::visit( |
| 40 | [](auto&& r) -> fields_type& { return r.base(); }, response); |
| 41 | } |
| 42 | |
| 43 | const fields_type& fields() const |
| 44 | { |
| 45 | return boost::variant2::visit( |
| 46 | [](auto&& r) -> const fields_type& { return r.base(); }, response); |
| 47 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 49 | void addHeader(std::string_view key, std::string_view value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 51 | fields().insert(key, value); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 52 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 54 | void addHeader(http::field key, std::string_view value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 56 | fields().insert(key, value); |
Ed Tanous | 994fd86 | 2023-06-06 13:37:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 59 | void clearHeader(http::field key) |
Ed Tanous | 994fd86 | 2023-06-06 13:37:03 -0700 | [diff] [blame] | 60 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 61 | fields().erase(key); |
Ed Tanous | 2cd4cc1 | 2018-07-25 10:51:19 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 63 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 64 | Response() : response(string_response()) {} |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 65 | Response(Response&& res) noexcept : |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 66 | response(std::move(res.response)), jsonValue(std::move(res.jsonValue)), |
| 67 | completed(res.completed) |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 68 | { |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 69 | // See note in operator= move handler for why this is needed. |
| 70 | if (!res.completed) |
| 71 | { |
| 72 | completeRequestHandler = std::move(res.completeRequestHandler); |
| 73 | res.completeRequestHandler = nullptr; |
| 74 | } |
| 75 | isAliveHelper = res.isAliveHelper; |
| 76 | res.isAliveHelper = nullptr; |
| 77 | } |
| 78 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 79 | ~Response() = default; |
| 80 | |
| 81 | Response(const Response&) = delete; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | Response& operator=(const Response& r) = delete; |
| 83 | |
| 84 | Response& operator=(Response&& r) noexcept |
| 85 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_DEBUG("Moving response containers; this: {}; other: {}", |
| 87 | logPtr(this), logPtr(&r)); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 88 | if (this == &r) |
| 89 | { |
| 90 | return *this; |
| 91 | } |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 92 | response = std::move(r.response); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | jsonValue = std::move(r.jsonValue); |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 94 | |
| 95 | // Only need to move completion handler if not already completed |
| 96 | // Note, there are cases where we might move out of a Response object |
| 97 | // while in a completion handler for that response object. This check |
| 98 | // is intended to prevent destructing the functor we are currently |
| 99 | // executing from in that case. |
| 100 | if (!r.completed) |
| 101 | { |
| 102 | completeRequestHandler = std::move(r.completeRequestHandler); |
| 103 | r.completeRequestHandler = nullptr; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | completeRequestHandler = nullptr; |
| 108 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | completed = r.completed; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 110 | isAliveHelper = std::move(r.isAliveHelper); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 111 | r.isAliveHelper = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | return *this; |
| 113 | } |
| 114 | |
Nan Zhou | 3590bd1 | 2022-08-12 18:05:09 +0000 | [diff] [blame] | 115 | void result(unsigned v) |
| 116 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 117 | fields().result(v); |
Nan Zhou | 3590bd1 | 2022-08-12 18:05:09 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 120 | void result(http::status v) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 122 | fields().result(v); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 125 | void copyBody(const Response& res) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 127 | const string_response* s = |
| 128 | boost::variant2::get_if<string_response>(&(res.response)); |
| 129 | if (s == nullptr) |
| 130 | { |
| 131 | BMCWEB_LOG_ERROR("Unable to copy a file"); |
| 132 | return; |
| 133 | } |
| 134 | string_response* myString = |
| 135 | boost::variant2::get_if<string_response>(&response); |
| 136 | if (myString == nullptr) |
| 137 | { |
| 138 | myString = &response.emplace<string_response>(); |
| 139 | } |
| 140 | myString->body() = s->body(); |
| 141 | } |
| 142 | |
| 143 | http::status result() const |
| 144 | { |
| 145 | return fields().result(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 148 | unsigned resultInt() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 150 | return fields().result_int(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ed Tanous | bb60f4d | 2022-06-27 10:39:09 -0700 | [diff] [blame] | 153 | std::string_view reason() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 154 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 155 | return fields().reason(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool isCompleted() const noexcept |
| 159 | { |
| 160 | return completed; |
| 161 | } |
| 162 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 163 | const std::string* body() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 165 | string_response* body = |
| 166 | boost::variant2::get_if<string_response>(&response); |
| 167 | if (body == nullptr) |
| 168 | { |
| 169 | return nullptr; |
| 170 | } |
| 171 | return &body->body(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Carson Labrado | 46a8146 | 2022-04-27 21:11:37 +0000 | [diff] [blame] | 174 | std::string_view getHeaderValue(std::string_view key) const |
| 175 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 176 | return fields()[key]; |
Carson Labrado | 46a8146 | 2022-04-27 21:11:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | void keepAlive(bool k) |
| 180 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 181 | return boost::variant2::visit([k](auto&& r) { r.keep_alive(k); }, |
| 182 | response); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Ed Tanous | bb60f4d | 2022-06-27 10:39:09 -0700 | [diff] [blame] | 185 | bool keepAlive() const |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 186 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 187 | return boost::variant2::visit([](auto&& r) { return r.keep_alive(); }, |
| 188 | response); |
| 189 | } |
| 190 | |
| 191 | uint64_t getContentLength(boost::optional<uint64_t> pSize) |
| 192 | { |
| 193 | // This code is a throw-free equivalent to |
| 194 | // beast::http::message::prepare_payload |
| 195 | using http::status; |
| 196 | using http::status_class; |
| 197 | using http::to_status_class; |
| 198 | if (!pSize) |
| 199 | { |
| 200 | return 0; |
| 201 | } |
| 202 | bool is1XXReturn = to_status_class(result()) == |
| 203 | status_class::informational; |
| 204 | if (*pSize > 0 && (is1XXReturn || result() == status::no_content || |
| 205 | result() == status::not_modified)) |
| 206 | { |
| 207 | BMCWEB_LOG_CRITICAL("{} Response content provided but code was " |
| 208 | "no-content or not_modified, which aren't " |
| 209 | "allowed to have a body", |
| 210 | logPtr(this)); |
| 211 | return 0; |
| 212 | } |
| 213 | return *pSize; |
| 214 | } |
| 215 | |
| 216 | uint64_t size() |
| 217 | { |
| 218 | return boost::variant2::visit( |
| 219 | [](auto&& res) -> uint64_t { return res.body().size(); }, response); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | void preparePayload() |
| 223 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 224 | boost::variant2::visit( |
| 225 | [this](auto&& r) { |
| 226 | r.content_length(getContentLength(r.payload_size())); |
| 227 | }, |
| 228 | response); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 229 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 230 | |
| 231 | void clear() |
| 232 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 233 | BMCWEB_LOG_DEBUG("{} Clearing response containers", logPtr(this)); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 234 | response.emplace<string_response>(); |
Ed Tanous | a6695a8 | 2023-05-16 11:39:18 -0700 | [diff] [blame] | 235 | jsonValue = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 236 | completed = false; |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 237 | expectedHash = std::nullopt; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 240 | std::string computeEtag() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 241 | { |
Ed Tanous | 89f1800 | 2022-03-24 18:38:24 -0700 | [diff] [blame] | 242 | // Only set etag if this request succeeded |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 243 | if (result() != http::status::ok) |
Ed Tanous | 89f1800 | 2022-03-24 18:38:24 -0700 | [diff] [blame] | 244 | { |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 245 | return ""; |
| 246 | } |
| 247 | // and the json response isn't empty |
| 248 | if (jsonValue.empty()) |
| 249 | { |
| 250 | return ""; |
| 251 | } |
| 252 | size_t hashval = std::hash<nlohmann::json>{}(jsonValue); |
| 253 | return "\"" + intToHexString(hashval, 8) + "\""; |
| 254 | } |
| 255 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 256 | void write(std::string&& bodyPart) |
| 257 | { |
| 258 | string_response* str = |
| 259 | boost::variant2::get_if<string_response>(&response); |
| 260 | if (str != nullptr) |
| 261 | { |
| 262 | str->body() += bodyPart; |
| 263 | return; |
| 264 | } |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 265 | http::header<false> headTemp = std::move(fields()); |
| 266 | string_response& stringResponse = |
| 267 | response.emplace<string_response>(std::move(headTemp)); |
| 268 | stringResponse.body() = std::move(bodyPart); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 271 | void end() |
| 272 | { |
| 273 | std::string etag = computeEtag(); |
| 274 | if (!etag.empty()) |
| 275 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 276 | addHeader(http::field::etag, etag); |
Ed Tanous | 89f1800 | 2022-03-24 18:38:24 -0700 | [diff] [blame] | 277 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 278 | if (completed) |
| 279 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 280 | BMCWEB_LOG_ERROR("{} Response was ended twice", logPtr(this)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | completed = true; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 284 | BMCWEB_LOG_DEBUG("{} calling completion handler", logPtr(this)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | if (completeRequestHandler) |
| 286 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 287 | BMCWEB_LOG_DEBUG("{} completion handler was valid", logPtr(this)); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 288 | completeRequestHandler(*this); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Ed Tanous | bb60f4d | 2022-06-27 10:39:09 -0700 | [diff] [blame] | 292 | bool isAlive() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 293 | { |
| 294 | return isAliveHelper && isAliveHelper(); |
| 295 | } |
| 296 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 297 | void setCompleteRequestHandler(std::function<void(Response&)>&& handler) |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 298 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 299 | BMCWEB_LOG_DEBUG("{} setting completion handler", logPtr(this)); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 300 | completeRequestHandler = std::move(handler); |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 301 | |
| 302 | // Now that we have a new completion handler attached, we're no longer |
| 303 | // complete |
| 304 | completed = false; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | std::function<void(Response&)> releaseCompleteRequestHandler() |
| 308 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 309 | BMCWEB_LOG_DEBUG("{} releasing completion handler{}", logPtr(this), |
| 310 | static_cast<bool>(completeRequestHandler)); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 311 | std::function<void(Response&)> ret = completeRequestHandler; |
| 312 | completeRequestHandler = nullptr; |
Ed Tanous | 13548d8 | 2022-07-22 09:50:44 -0700 | [diff] [blame] | 313 | completed = true; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | void setIsAliveHelper(std::function<bool()>&& handler) |
| 318 | { |
| 319 | isAliveHelper = std::move(handler); |
| 320 | } |
| 321 | |
| 322 | std::function<bool()> releaseIsAliveHelper() |
| 323 | { |
| 324 | std::function<bool()> ret = std::move(isAliveHelper); |
| 325 | isAliveHelper = nullptr; |
| 326 | return ret; |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 329 | void setHashAndHandleNotModified() |
| 330 | { |
| 331 | // Can only hash if we have content that's valid |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 332 | if (jsonValue.empty() || result() != http::status::ok) |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 333 | { |
| 334 | return; |
| 335 | } |
| 336 | size_t hashval = std::hash<nlohmann::json>{}(jsonValue); |
| 337 | std::string hexVal = "\"" + intToHexString(hashval, 8) + "\""; |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 338 | addHeader(http::field::etag, hexVal); |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 339 | if (expectedHash && hexVal == *expectedHash) |
| 340 | { |
Ed Tanous | a6695a8 | 2023-05-16 11:39:18 -0700 | [diff] [blame] | 341 | jsonValue = nullptr; |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 342 | result(http::status::not_modified); |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | void setExpectedHash(std::string_view hash) |
| 347 | { |
| 348 | expectedHash = hash; |
| 349 | } |
| 350 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 351 | using message_generator = http::message_generator; |
| 352 | message_generator generator() |
| 353 | { |
| 354 | return boost::variant2::visit( |
| 355 | [](auto& r) -> message_generator { return std::move(r); }, |
| 356 | response); |
| 357 | } |
| 358 | |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 359 | bool openFile(const std::filesystem::path& path, |
| 360 | bmcweb::EncodingType enc = bmcweb::EncodingType::Raw) |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 361 | { |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 362 | file_response::body_type::value_type body(enc); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 363 | boost::beast::error_code ec; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 364 | body.open(path.c_str(), boost::beast::file_mode::read, ec); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 365 | if (ec) |
| 366 | { |
| 367 | return false; |
| 368 | } |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame^] | 369 | updateFileBody(std::move(body)); |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | bool openFd(int fd, bmcweb::EncodingType enc = bmcweb::EncodingType::Raw) |
| 374 | { |
| 375 | file_response::body_type::value_type body(enc); |
| 376 | boost::beast::error_code ec; |
| 377 | body.setFd(fd, ec); |
| 378 | if (ec) |
| 379 | { |
| 380 | BMCWEB_LOG_ERROR("Failed to set fd"); |
| 381 | return false; |
| 382 | } |
| 383 | updateFileBody(std::move(body)); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | private: |
| 388 | void updateFileBody(file_response::body_type::value_type file) |
| 389 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 390 | // store the headers on stack temporarily so we can reconstruct the new |
| 391 | // base with the old headers copied in. |
| 392 | http::header<false> headTemp = std::move(fields()); |
| 393 | file_response& fileResponse = |
| 394 | response.emplace<file_response>(std::move(headTemp)); |
| 395 | fileResponse.body() = std::move(file); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 398 | std::optional<std::string> expectedHash; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 399 | bool completed = false; |
| 400 | std::function<void(Response&)> completeRequestHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 401 | std::function<bool()> isAliveHelper; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 402 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 403 | } // namespace crow |