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