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