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