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