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