blob: 123a7e11299753fc01aef43bf8842d709ae14d8a [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
Abhilash Rajub5f288d2023-11-08 22:32:44 -06002#include "http_file_body.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08004#include "utils/hex_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -07005
Ed Tanousd43cd0c2020-09-30 20:46:53 -07006#include <boost/beast/http/message.hpp>
Ed Tanousfaf100f2023-05-25 10:03:14 -07007#include <nlohmann/json.hpp>
Ed Tanous7045c8d2017-04-03 10:04:37 -07008
Ed Tanous8a9a25c2021-05-11 14:50:58 -07009#include <optional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050010#include <string>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070011#include <string_view>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050012#include <utility>
Ed Tanous1abe55e2018-09-05 08:30:59 -070013namespace crow
14{
Ed Tanouse0d918b2018-03-27 17:41:04 -070015
Ed Tanous52cc1122020-07-18 13:51:21 -070016template <typename Adaptor, typename Handler>
Ed Tanous7045c8d2017-04-03 10:04:37 -070017class Connection;
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +010018
Ed Tanous27b0cf92023-08-07 12:02:40 -070019namespace http = boost::beast::http;
20
Ed Tanous1abe55e2018-09-05 08:30:59 -070021struct Response
22{
Ed Tanous52cc1122020-07-18 13:51:21 -070023 template <typename Adaptor, typename Handler>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024 friend class crow::Connection;
Ed Tanous7045c8d2017-04-03 10:04:37 -070025
Ed Tanous52e31622024-01-23 16:31:11 -080026 http::response<bmcweb::FileBody> response;
Ed Tanouse0d918b2018-03-27 17:41:04 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 nlohmann::json jsonValue;
Ed Tanous27b0cf92023-08-07 12:02:40 -070029 using fields_type = http::header<false, http::fields>;
30 fields_type& fields()
31 {
Ed Tanous52e31622024-01-23 16:31:11 -080032 return response.base();
Ed Tanous27b0cf92023-08-07 12:02:40 -070033 }
34
35 const fields_type& fields() const
36 {
Ed Tanous52e31622024-01-23 16:31:11 -080037 return response.base();
Ed Tanous27b0cf92023-08-07 12:02:40 -070038 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070039
Ed Tanous26ccae32023-02-16 10:28:44 -080040 void addHeader(std::string_view key, std::string_view value)
Ed Tanous1abe55e2018-09-05 08:30:59 -070041 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070042 fields().insert(key, value);
Ed Tanous7045c8d2017-04-03 10:04:37 -070043 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070044
Ed Tanous27b0cf92023-08-07 12:02:40 -070045 void addHeader(http::field key, std::string_view value)
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070047 fields().insert(key, value);
Ed Tanous994fd862023-06-06 13:37:03 -070048 }
49
Ed Tanous27b0cf92023-08-07 12:02:40 -070050 void clearHeader(http::field key)
Ed Tanous994fd862023-06-06 13:37:03 -070051 {
Ed Tanous27b0cf92023-08-07 12:02:40 -070052 fields().erase(key);
Ed Tanous2cd4cc12018-07-25 10:51:19 -070053 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070054
Ed Tanous52e31622024-01-23 16:31:11 -080055 Response() = default;
Ed Tanous13548d82022-07-22 09:50:44 -070056 Response(Response&& res) noexcept :
Ed Tanous27b0cf92023-08-07 12:02:40 -070057 response(std::move(res.response)), jsonValue(std::move(res.jsonValue)),
58 completed(res.completed)
Ed Tanous13548d82022-07-22 09:50:44 -070059 {
Ed Tanous13548d82022-07-22 09:50:44 -070060 // 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 Tanous13548d82022-07-22 09:50:44 -070066 }
67
Ed Tanousecd6a3a2022-01-07 09:18:40 -080068 ~Response() = default;
69
70 Response(const Response&) = delete;
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 Response& operator=(const Response& r) = delete;
72
73 Response& operator=(Response&& r) noexcept
74 {
Ed Tanous62598e32023-07-17 17:06:25 -070075 BMCWEB_LOG_DEBUG("Moving response containers; this: {}; other: {}",
76 logPtr(this), logPtr(&r));
Nan Zhou72374eb2022-01-27 17:06:51 -080077 if (this == &r)
78 {
79 return *this;
80 }
Ed Tanous27b0cf92023-08-07 12:02:40 -070081 response = std::move(r.response);
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 jsonValue = std::move(r.jsonValue);
Ed Tanous13548d82022-07-22 09:50:44 -070083
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 Tanous1abe55e2018-09-05 08:30:59 -070098 completed = r.completed;
99 return *this;
100 }
101
Nan Zhou3590bd12022-08-12 18:05:09 +0000102 void result(unsigned v)
103 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700104 fields().result(v);
Nan Zhou3590bd12022-08-12 18:05:09 +0000105 }
106
Ed Tanous27b0cf92023-08-07 12:02:40 -0700107 void result(http::status v)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700109 fields().result(v);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 }
111
Ed Tanous27b0cf92023-08-07 12:02:40 -0700112 void copyBody(const Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 {
Ed Tanous52e31622024-01-23 16:31:11 -0800114 response.body() = res.response.body();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700115 }
116
117 http::status result() const
118 {
119 return fields().result();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 }
121
Carson Labrado039a47e2022-04-05 16:03:20 +0000122 unsigned resultInt() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700124 return fields().result_int();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125 }
126
Ed Tanousbb60f4d2022-06-27 10:39:09 -0700127 std::string_view reason() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700129 return fields().reason();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 }
131
132 bool isCompleted() const noexcept
133 {
134 return completed;
135 }
136
Ed Tanous27b0cf92023-08-07 12:02:40 -0700137 const std::string* body()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 {
Ed Tanous52e31622024-01-23 16:31:11 -0800139 return &response.body().str();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 }
141
Carson Labrado46a81462022-04-27 21:11:37 +0000142 std::string_view getHeaderValue(std::string_view key) const
143 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700144 return fields()[key];
Carson Labrado46a81462022-04-27 21:11:37 +0000145 }
146
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 void keepAlive(bool k)
148 {
Ed Tanous52e31622024-01-23 16:31:11 -0800149 response.keep_alive(k);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 }
151
Ed Tanousbb60f4d2022-06-27 10:39:09 -0700152 bool keepAlive() const
Ed Tanousceac6f72018-12-02 11:58:47 -0800153 {
Ed Tanous52e31622024-01-23 16:31:11 -0800154 return response.keep_alive();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700155 }
156
Ed Tanous52e31622024-01-23 16:31:11 -0800157 std::optional<uint64_t> size()
158 {
159 return response.body().payloadSize();
160 }
161
162 void preparePayload()
Ed Tanous27b0cf92023-08-07 12:02:40 -0700163 {
164 // This code is a throw-free equivalent to
165 // beast::http::message::prepare_payload
Ed Tanous52e31622024-01-23 16:31:11 -0800166 std::optional<uint64_t> pSize = response.body().payloadSize();
167 if (!pSize)
168 {
169 return;
170 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700171 using http::status;
172 using http::status_class;
173 using http::to_status_class;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700174 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 Tanous52e31622024-01-23 16:31:11 -0800183 response.content_length(0);
184 return;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700185 }
Ed Tanous52e31622024-01-23 16:31:11 -0800186 response.content_length(*pSize);
Ed Tanous23a21a12020-07-25 04:45:05 +0000187 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188
189 void clear()
190 {
Ed Tanous62598e32023-07-17 17:06:25 -0700191 BMCWEB_LOG_DEBUG("{} Clearing response containers", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800192 response.clear();
193
Ed Tanousa6695a82023-05-16 11:39:18 -0700194 jsonValue = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 completed = false;
Ed Tanous291d7092022-04-13 12:34:57 -0700196 expectedHash = std::nullopt;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 }
198
Ed Tanous2d6cb562022-07-07 20:44:54 -0700199 std::string computeEtag() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 {
Ed Tanous89f18002022-03-24 18:38:24 -0700201 // Only set etag if this request succeeded
Ed Tanous27b0cf92023-08-07 12:02:40 -0700202 if (result() != http::status::ok)
Ed Tanous89f18002022-03-24 18:38:24 -0700203 {
Ed Tanous2d6cb562022-07-07 20:44:54 -0700204 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 Tanous27b0cf92023-08-07 12:02:40 -0700215 void write(std::string&& bodyPart)
216 {
Ed Tanous52e31622024-01-23 16:31:11 -0800217 response.body().str() = std::move(bodyPart);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700218 }
219
Ed Tanous2d6cb562022-07-07 20:44:54 -0700220 void end()
221 {
222 std::string etag = computeEtag();
223 if (!etag.empty())
224 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700225 addHeader(http::field::etag, etag);
Ed Tanous89f18002022-03-24 18:38:24 -0700226 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 if (completed)
228 {
Ed Tanous62598e32023-07-17 17:06:25 -0700229 BMCWEB_LOG_ERROR("{} Response was ended twice", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 return;
231 }
232 completed = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700233 BMCWEB_LOG_DEBUG("{} calling completion handler", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 if (completeRequestHandler)
235 {
Ed Tanous62598e32023-07-17 17:06:25 -0700236 BMCWEB_LOG_DEBUG("{} completion handler was valid", logPtr(this));
Nan Zhou72374eb2022-01-27 17:06:51 -0800237 completeRequestHandler(*this);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 }
239 }
240
Nan Zhou72374eb2022-01-27 17:06:51 -0800241 void setCompleteRequestHandler(std::function<void(Response&)>&& handler)
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700242 {
Ed Tanous62598e32023-07-17 17:06:25 -0700243 BMCWEB_LOG_DEBUG("{} setting completion handler", logPtr(this));
Nan Zhou72374eb2022-01-27 17:06:51 -0800244 completeRequestHandler = std::move(handler);
Ed Tanous13548d82022-07-22 09:50:44 -0700245
246 // Now that we have a new completion handler attached, we're no longer
247 // complete
248 completed = false;
Nan Zhou72374eb2022-01-27 17:06:51 -0800249 }
250
251 std::function<void(Response&)> releaseCompleteRequestHandler()
252 {
Ed Tanous62598e32023-07-17 17:06:25 -0700253 BMCWEB_LOG_DEBUG("{} releasing completion handler{}", logPtr(this),
254 static_cast<bool>(completeRequestHandler));
Nan Zhou72374eb2022-01-27 17:06:51 -0800255 std::function<void(Response&)> ret = completeRequestHandler;
256 completeRequestHandler = nullptr;
Ed Tanous13548d82022-07-22 09:50:44 -0700257 completed = true;
Nan Zhou72374eb2022-01-27 17:06:51 -0800258 return ret;
259 }
260
Ed Tanous291d7092022-04-13 12:34:57 -0700261 void setHashAndHandleNotModified()
262 {
263 // Can only hash if we have content that's valid
Ed Tanous27b0cf92023-08-07 12:02:40 -0700264 if (jsonValue.empty() || result() != http::status::ok)
Ed Tanous291d7092022-04-13 12:34:57 -0700265 {
266 return;
267 }
268 size_t hashval = std::hash<nlohmann::json>{}(jsonValue);
269 std::string hexVal = "\"" + intToHexString(hashval, 8) + "\"";
Ed Tanous27b0cf92023-08-07 12:02:40 -0700270 addHeader(http::field::etag, hexVal);
Ed Tanous291d7092022-04-13 12:34:57 -0700271 if (expectedHash && hexVal == *expectedHash)
272 {
Ed Tanousa6695a82023-05-16 11:39:18 -0700273 jsonValue = nullptr;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700274 result(http::status::not_modified);
Ed Tanous291d7092022-04-13 12:34:57 -0700275 }
276 }
277
278 void setExpectedHash(std::string_view hash)
279 {
280 expectedHash = hash;
281 }
282
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600283 bool openFile(const std::filesystem::path& path,
284 bmcweb::EncodingType enc = bmcweb::EncodingType::Raw)
Ed Tanous27b0cf92023-08-07 12:02:40 -0700285 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700286 boost::beast::error_code ec;
Ed Tanous52e31622024-01-23 16:31:11 -0800287 response.body().open(path.c_str(), boost::beast::file_mode::read, ec);
288 response.body().encodingType = enc;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700289 if (ec)
290 {
Ed Tanous52e31622024-01-23 16:31:11 -0800291 BMCWEB_LOG_ERROR("Failed to open file {}", path.c_str());
Ed Tanous27b0cf92023-08-07 12:02:40 -0700292 return false;
293 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600294 return true;
295 }
296
297 bool openFd(int fd, bmcweb::EncodingType enc = bmcweb::EncodingType::Raw)
298 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600299 boost::beast::error_code ec;
Ed Tanous52e31622024-01-23 16:31:11 -0800300 response.body().encodingType = enc;
301 response.body().setFd(fd, ec);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600302 if (ec)
303 {
304 BMCWEB_LOG_ERROR("Failed to set fd");
305 return false;
306 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600307 return true;
308 }
309
310 private:
Ed Tanous291d7092022-04-13 12:34:57 -0700311 std::optional<std::string> expectedHash;
Nan Zhou72374eb2022-01-27 17:06:51 -0800312 bool completed = false;
313 std::function<void(Response&)> completeRequestHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700314};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315} // namespace crow