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