blob: 33f24b31f96cfaa10a07b9bd2de80f7067b22521 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
Ed Tanousb2896142024-01-31 15:25:47 -08004#include "http_body.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07005#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08006#include "utils/hex_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -07007
Ed Tanous0242baf2024-05-16 19:52:47 -07008#include <fcntl.h>
9
Ed Tanousd7857202025-01-28 15:32:26 -080010#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 Tanousd43cd0c2020-09-30 20:46:53 -070014#include <boost/beast/http/message.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <boost/beast/http/status.hpp>
Abiola Asojod23d6342025-06-18 20:15:24 +000016#include <boost/url/url_view.hpp>
Ed Tanousfaf100f2023-05-25 10:03:14 -070017#include <nlohmann/json.hpp>
Ed Tanous7045c8d2017-04-03 10:04:37 -070018
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <cstddef>
20#include <cstdint>
21#include <filesystem>
22#include <functional>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070023#include <optional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024#include <string>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070025#include <string_view>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050026#include <utility>
Ed Tanous0242baf2024-05-16 19:52:47 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace crow
29{
Ed Tanouse0d918b2018-03-27 17:41:04 -070030
Ed Tanous52cc1122020-07-18 13:51:21 -070031template <typename Adaptor, typename Handler>
Ed Tanous7045c8d2017-04-03 10:04:37 -070032class Connection;
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +010033
Ed Tanous27b0cf92023-08-07 12:02:40 -070034namespace http = boost::beast::http;
35
Myung Baed51c61b2024-09-13 10:35:34 -050036enum class OpenCode
37{
38 Success,
39 FileDoesNotExist,
40 InternalError,
41};
42
Ed Tanous1abe55e2018-09-05 08:30:59 -070043struct Response
44{
Ed Tanous52cc1122020-07-18 13:51:21 -070045 template <typename Adaptor, typename Handler>
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 friend class crow::Connection;
Ed Tanous7045c8d2017-04-03 10:04:37 -070047
Ed Tanousb2896142024-01-31 15:25:47 -080048 http::response<bmcweb::HttpBody> response;
Ed Tanouse0d918b2018-03-27 17:41:04 -070049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 nlohmann::json jsonValue;
Ed Tanous27b0cf92023-08-07 12:02:40 -070051 using fields_type = http::header<false, http::fields>;
52 fields_type& fields()
53 {
Ed Tanous52e31622024-01-23 16:31:11 -080054 return response.base();
Ed Tanous27b0cf92023-08-07 12:02:40 -070055 }
56
57 const fields_type& fields() const
58 {
Ed Tanous52e31622024-01-23 16:31:11 -080059 return response.base();
Ed Tanous27b0cf92023-08-07 12:02:40 -070060 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070061
Ed Tanous26ccae32023-02-16 10:28:44 -080062 void addHeader(std::string_view key, std::string_view value)
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070064 fields().insert(key, value);
Ed Tanous7045c8d2017-04-03 10:04:37 -070065 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070066
Ed Tanous27b0cf92023-08-07 12:02:40 -070067 void addHeader(http::field key, std::string_view value)
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070069 fields().insert(key, value);
Ed Tanous994fd862023-06-06 13:37:03 -070070 }
71
Ed Tanous27b0cf92023-08-07 12:02:40 -070072 void clearHeader(http::field key)
Ed Tanous994fd862023-06-06 13:37:03 -070073 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070074 fields().erase(key);
Ed Tanous2cd4cc12018-07-25 10:51:19 -070075 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070076
Ed Tanous52e31622024-01-23 16:31:11 -080077 Response() = default;
Ed Tanous13548d82022-07-22 09:50:44 -070078 Response(Response&& res) noexcept :
Ed Tanous27b0cf92023-08-07 12:02:40 -070079 response(std::move(res.response)), jsonValue(std::move(res.jsonValue)),
Ed Tanous37b912f2025-03-20 18:24:30 -070080 expectedHash(std::move(res.expectedHash)), completed(res.completed)
Ed Tanous13548d82022-07-22 09:50:44 -070081 {
Ed Tanous13548d82022-07-22 09:50:44 -070082 // See note in operator= move handler for why this is needed.
83 if (!res.completed)
84 {
85 completeRequestHandler = std::move(res.completeRequestHandler);
86 res.completeRequestHandler = nullptr;
87 }
Ed Tanous13548d82022-07-22 09:50:44 -070088 }
89
Ed Tanousecd6a3a2022-01-07 09:18:40 -080090 ~Response() = default;
91
92 Response(const Response&) = delete;
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 Response& operator=(const Response& r) = delete;
94
95 Response& operator=(Response&& r) noexcept
96 {
Ed Tanous62598e32023-07-17 17:06:25 -070097 BMCWEB_LOG_DEBUG("Moving response containers; this: {}; other: {}",
98 logPtr(this), logPtr(&r));
Nan Zhou72374eb2022-01-27 17:06:51 -080099 if (this == &r)
100 {
101 return *this;
102 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700103 response = std::move(r.response);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 jsonValue = std::move(r.jsonValue);
Ed Tanous499b5b42024-04-06 08:39:18 -0700105 expectedHash = std::move(r.expectedHash);
Ed Tanous13548d82022-07-22 09:50:44 -0700106
107 // Only need to move completion handler if not already completed
108 // Note, there are cases where we might move out of a Response object
109 // while in a completion handler for that response object. This check
110 // is intended to prevent destructing the functor we are currently
111 // executing from in that case.
112 if (!r.completed)
113 {
114 completeRequestHandler = std::move(r.completeRequestHandler);
115 r.completeRequestHandler = nullptr;
116 }
117 else
118 {
119 completeRequestHandler = nullptr;
120 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 completed = r.completed;
122 return *this;
123 }
124
Nan Zhou3590bd12022-08-12 18:05:09 +0000125 void result(unsigned v)
126 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700127 fields().result(v);
Nan Zhou3590bd12022-08-12 18:05:09 +0000128 }
129
Ed Tanous27b0cf92023-08-07 12:02:40 -0700130 void result(http::status v)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700132 fields().result(v);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 }
134
Ed Tanous27b0cf92023-08-07 12:02:40 -0700135 void copyBody(const Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 {
Ed Tanous52e31622024-01-23 16:31:11 -0800137 response.body() = res.response.body();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700138 }
139
140 http::status result() const
141 {
142 return fields().result();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 }
144
Carson Labrado039a47e2022-04-05 16:03:20 +0000145 unsigned resultInt() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700146 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700147 return fields().result_int();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 }
149
Ed Tanousbb60f4d2022-06-27 10:39:09 -0700150 std::string_view reason() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700151 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700152 return fields().reason();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 }
154
155 bool isCompleted() const noexcept
156 {
157 return completed;
158 }
159
Ed Tanous27b0cf92023-08-07 12:02:40 -0700160 const std::string* body()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 {
Ed Tanous52e31622024-01-23 16:31:11 -0800162 return &response.body().str();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 }
164
Carson Labrado46a81462022-04-27 21:11:37 +0000165 std::string_view getHeaderValue(std::string_view key) const
166 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700167 return fields()[key];
Carson Labrado46a81462022-04-27 21:11:37 +0000168 }
169
Ed Tanous499b5b42024-04-06 08:39:18 -0700170 std::string_view getHeaderValue(boost::beast::http::field key) const
171 {
172 return fields()[key];
173 }
174
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 void keepAlive(bool k)
176 {
Ed Tanous52e31622024-01-23 16:31:11 -0800177 response.keep_alive(k);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 }
179
Ed Tanousbb60f4d2022-06-27 10:39:09 -0700180 bool keepAlive() const
Ed Tanousceac6f72018-12-02 11:58:47 -0800181 {
Ed Tanous52e31622024-01-23 16:31:11 -0800182 return response.keep_alive();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700183 }
184
Ed Tanous52e31622024-01-23 16:31:11 -0800185 std::optional<uint64_t> size()
186 {
187 return response.body().payloadSize();
188 }
189
Abiola Asojod23d6342025-06-18 20:15:24 +0000190 void preparePayload(const boost::urls::url_view& urlView)
Ed Tanous27b0cf92023-08-07 12:02:40 -0700191 {
Ed Tanous52e31622024-01-23 16:31:11 -0800192 std::optional<uint64_t> pSize = response.body().payloadSize();
Ed Tanous0242baf2024-05-16 19:52:47 -0700193
Ed Tanous27b0cf92023-08-07 12:02:40 -0700194 using http::status;
195 using http::status_class;
196 using http::to_status_class;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700197 bool is1XXReturn = to_status_class(result()) ==
198 status_class::informational;
Ed Tanous0242baf2024-05-16 19:52:47 -0700199 if (!pSize)
200 {
201 response.chunked(true);
202 return;
203 }
204 response.content_length(*pSize);
205
Abiola Asojo5070c7e2025-07-29 19:00:04 +0000206 if ((*pSize > 0) && (is1XXReturn || result() == status::no_content ||
207 result() == status::not_modified))
Ed Tanous27b0cf92023-08-07 12:02:40 -0700208 {
209 BMCWEB_LOG_CRITICAL("{} Response content provided but code was "
210 "no-content or not_modified, which aren't "
Abiola Asojod23d6342025-06-18 20:15:24 +0000211 "allowed to have a body for url : \"{}\"",
212 logPtr(this), urlView.path());
Ed Tanous52e31622024-01-23 16:31:11 -0800213 response.content_length(0);
214 return;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700215 }
Ed Tanous23a21a12020-07-25 04:45:05 +0000216 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217
218 void clear()
219 {
Ed Tanous62598e32023-07-17 17:06:25 -0700220 BMCWEB_LOG_DEBUG("{} Clearing response containers", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800221 response.clear();
Ed Tanous06fc9be2024-03-27 19:58:11 -0700222 response.body().clear();
Ed Tanous52e31622024-01-23 16:31:11 -0800223
Ed Tanousa6695a82023-05-16 11:39:18 -0700224 jsonValue = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 completed = false;
Ed Tanous291d7092022-04-13 12:34:57 -0700226 expectedHash = std::nullopt;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
228
Ed Tanous2d6cb562022-07-07 20:44:54 -0700229 std::string computeEtag() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 {
Ed Tanous89f18002022-03-24 18:38:24 -0700231 // Only set etag if this request succeeded
Ed Tanous27b0cf92023-08-07 12:02:40 -0700232 if (result() != http::status::ok)
Ed Tanous89f18002022-03-24 18:38:24 -0700233 {
Ed Tanous2d6cb562022-07-07 20:44:54 -0700234 return "";
235 }
236 // and the json response isn't empty
237 if (jsonValue.empty())
238 {
239 return "";
240 }
241 size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
242 return "\"" + intToHexString(hashval, 8) + "\"";
243 }
244
Ed Tanous27b0cf92023-08-07 12:02:40 -0700245 void write(std::string&& bodyPart)
246 {
Ed Tanous52e31622024-01-23 16:31:11 -0800247 response.body().str() = std::move(bodyPart);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700248 }
249
Ed Tanous2d6cb562022-07-07 20:44:54 -0700250 void end()
251 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 if (completed)
253 {
Ed Tanous62598e32023-07-17 17:06:25 -0700254 BMCWEB_LOG_ERROR("{} Response was ended twice", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 return;
256 }
257 completed = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700258 BMCWEB_LOG_DEBUG("{} calling completion handler", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 if (completeRequestHandler)
260 {
Ed Tanous62598e32023-07-17 17:06:25 -0700261 BMCWEB_LOG_DEBUG("{} completion handler was valid", logPtr(this));
Nan Zhou72374eb2022-01-27 17:06:51 -0800262 completeRequestHandler(*this);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700263 }
264 }
265
Nan Zhou72374eb2022-01-27 17:06:51 -0800266 void setCompleteRequestHandler(std::function<void(Response&)>&& handler)
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700267 {
Ed Tanous62598e32023-07-17 17:06:25 -0700268 BMCWEB_LOG_DEBUG("{} setting completion handler", logPtr(this));
Nan Zhou72374eb2022-01-27 17:06:51 -0800269 completeRequestHandler = std::move(handler);
Ed Tanous13548d82022-07-22 09:50:44 -0700270
271 // Now that we have a new completion handler attached, we're no longer
272 // complete
273 completed = false;
Nan Zhou72374eb2022-01-27 17:06:51 -0800274 }
275
276 std::function<void(Response&)> releaseCompleteRequestHandler()
277 {
Ed Tanous62598e32023-07-17 17:06:25 -0700278 BMCWEB_LOG_DEBUG("{} releasing completion handler{}", logPtr(this),
279 static_cast<bool>(completeRequestHandler));
Nan Zhou72374eb2022-01-27 17:06:51 -0800280 std::function<void(Response&)> ret = completeRequestHandler;
281 completeRequestHandler = nullptr;
Ed Tanous13548d82022-07-22 09:50:44 -0700282 completed = true;
Nan Zhou72374eb2022-01-27 17:06:51 -0800283 return ret;
284 }
285
Ed Tanous291d7092022-04-13 12:34:57 -0700286 void setHashAndHandleNotModified()
287 {
288 // Can only hash if we have content that's valid
Ed Tanous27b0cf92023-08-07 12:02:40 -0700289 if (jsonValue.empty() || result() != http::status::ok)
Ed Tanous291d7092022-04-13 12:34:57 -0700290 {
291 return;
292 }
293 size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
294 std::string hexVal = "\"" + intToHexString(hashval, 8) + "\"";
Ed Tanous27b0cf92023-08-07 12:02:40 -0700295 addHeader(http::field::etag, hexVal);
Ed Tanous291d7092022-04-13 12:34:57 -0700296 if (expectedHash && hexVal == *expectedHash)
297 {
Ed Tanousa6695a82023-05-16 11:39:18 -0700298 jsonValue = nullptr;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700299 result(http::status::not_modified);
Ed Tanous291d7092022-04-13 12:34:57 -0700300 }
301 }
302
303 void setExpectedHash(std::string_view hash)
304 {
305 expectedHash = hash;
306 }
307
Ed Tanousb2539062024-03-12 16:58:35 -0700308 OpenCode openFile(
309 const std::filesystem::path& path,
310 bmcweb::EncodingType enc = bmcweb::EncodingType::Raw,
311 bmcweb::CompressionType comp = bmcweb::CompressionType::Raw)
Ed Tanous27b0cf92023-08-07 12:02:40 -0700312 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700313 boost::beast::error_code ec;
Ed Tanous52e31622024-01-23 16:31:11 -0800314 response.body().open(path.c_str(), boost::beast::file_mode::read, ec);
315 response.body().encodingType = enc;
Ed Tanousb2539062024-03-12 16:58:35 -0700316 response.body().compressionType = comp;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700317 if (ec)
318 {
Myung Baed51c61b2024-09-13 10:35:34 -0500319 BMCWEB_LOG_ERROR("Failed to open file {}, ec={}", path.c_str(),
320 ec.value());
321 if (ec.value() == boost::system::errc::no_such_file_or_directory)
322 {
323 return OpenCode::FileDoesNotExist;
324 }
325 return OpenCode::InternalError;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700326 }
Myung Baed51c61b2024-09-13 10:35:34 -0500327 return OpenCode::Success;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600328 }
329
330 bool openFd(int fd, bmcweb::EncodingType enc = bmcweb::EncodingType::Raw)
331 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600332 boost::beast::error_code ec;
Ed Tanous0242baf2024-05-16 19:52:47 -0700333 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
334 int retval = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
335 if (retval == -1)
336 {
337 BMCWEB_LOG_ERROR("Setting O_NONBLOCK failed");
338 }
Ed Tanous52e31622024-01-23 16:31:11 -0800339 response.body().encodingType = enc;
340 response.body().setFd(fd, ec);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600341 if (ec)
342 {
343 BMCWEB_LOG_ERROR("Failed to set fd");
344 return false;
345 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600346 return true;
347 }
348
349 private:
Ed Tanous291d7092022-04-13 12:34:57 -0700350 std::optional<std::string> expectedHash;
Nan Zhou72374eb2022-01-27 17:06:51 -0800351 bool completed = false;
352 std::function<void(Response&)> completeRequestHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700353};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354} // namespace crow