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 | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 43 | ~Response() = default; |
| 44 | |
| 45 | Response(const Response&) = delete; |
| 46 | Response(Response&&) = delete; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 47 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | Response& operator=(const Response& r) = delete; |
| 49 | |
| 50 | Response& operator=(Response&& r) noexcept |
| 51 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 52 | BMCWEB_LOG_DEBUG << "Moving response containers; this: " << this |
| 53 | << "; other: " << &r; |
| 54 | if (this == &r) |
| 55 | { |
| 56 | return *this; |
| 57 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | stringResponse = std::move(r.stringResponse); |
| 59 | r.stringResponse.emplace(response_type{}); |
| 60 | jsonValue = std::move(r.jsonValue); |
| 61 | completed = r.completed; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 62 | completeRequestHandler = std::move(r.completeRequestHandler); |
| 63 | isAliveHelper = std::move(r.isAliveHelper); |
| 64 | r.completeRequestHandler = nullptr; |
| 65 | r.isAliveHelper = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | void result(boost::beast::http::status v) |
| 70 | { |
| 71 | stringResponse->result(v); |
| 72 | } |
| 73 | |
| 74 | boost::beast::http::status result() |
| 75 | { |
| 76 | return stringResponse->result(); |
| 77 | } |
| 78 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 79 | unsigned resultInt() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
| 81 | return stringResponse->result_int(); |
| 82 | } |
| 83 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 84 | std::string_view reason() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | { |
| 86 | return stringResponse->reason(); |
| 87 | } |
| 88 | |
| 89 | bool isCompleted() const noexcept |
| 90 | { |
| 91 | return completed; |
| 92 | } |
| 93 | |
| 94 | std::string& body() |
| 95 | { |
| 96 | return stringResponse->body(); |
| 97 | } |
| 98 | |
| 99 | void keepAlive(bool k) |
| 100 | { |
| 101 | stringResponse->keep_alive(k); |
| 102 | } |
| 103 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 104 | bool keepAlive() |
| 105 | { |
| 106 | return stringResponse->keep_alive(); |
| 107 | } |
| 108 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | void preparePayload() |
| 110 | { |
| 111 | stringResponse->prepare_payload(); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 112 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | |
| 114 | void clear() |
| 115 | { |
| 116 | BMCWEB_LOG_DEBUG << this << " Clearing response containers"; |
| 117 | stringResponse.emplace(response_type{}); |
| 118 | jsonValue.clear(); |
| 119 | completed = false; |
| 120 | } |
| 121 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 122 | void write(std::string_view bodyPart) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 124 | stringResponse->body() += std::string(bodyPart); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void end() |
| 128 | { |
Ed Tanous | 89f1800 | 2022-03-24 18:38:24 -0700 | [diff] [blame] | 129 | // Only set etag if this request succeeded |
| 130 | if (result() == boost::beast::http::status::ok) |
| 131 | { |
| 132 | // and the json response isn't empty |
| 133 | if (!jsonValue.empty()) |
| 134 | { |
| 135 | size_t hashval = std::hash<nlohmann::json>{}(jsonValue); |
| 136 | std::string hexVal = "\"" + intToHexString(hashval, 8) + "\""; |
| 137 | addHeader(boost::beast::http::field::etag, hexVal); |
| 138 | } |
| 139 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 140 | if (completed) |
| 141 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 142 | BMCWEB_LOG_ERROR << this << " Response was ended twice"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | completed = true; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 146 | BMCWEB_LOG_DEBUG << this << " calling completion handler"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 147 | if (completeRequestHandler) |
| 148 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 149 | BMCWEB_LOG_DEBUG << this << " completion handler was valid"; |
| 150 | completeRequestHandler(*this); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 154 | bool isAlive() |
| 155 | { |
| 156 | return isAliveHelper && isAliveHelper(); |
| 157 | } |
| 158 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 159 | void setCompleteRequestHandler(std::function<void(Response&)>&& handler) |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 160 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 161 | BMCWEB_LOG_DEBUG << this << " setting completion handler"; |
| 162 | completeRequestHandler = std::move(handler); |
| 163 | } |
| 164 | |
| 165 | std::function<void(Response&)> releaseCompleteRequestHandler() |
| 166 | { |
| 167 | BMCWEB_LOG_DEBUG << this << " releasing completion handler" |
| 168 | << static_cast<bool>(completeRequestHandler); |
| 169 | std::function<void(Response&)> ret = completeRequestHandler; |
| 170 | completeRequestHandler = nullptr; |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | void setIsAliveHelper(std::function<bool()>&& handler) |
| 175 | { |
| 176 | isAliveHelper = std::move(handler); |
| 177 | } |
| 178 | |
| 179 | std::function<bool()> releaseIsAliveHelper() |
| 180 | { |
| 181 | std::function<bool()> ret = std::move(isAliveHelper); |
| 182 | isAliveHelper = nullptr; |
| 183 | return ret; |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | private: |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 187 | bool completed = false; |
| 188 | std::function<void(Response&)> completeRequestHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 189 | std::function<bool()> isAliveHelper; |
| 190 | |
| 191 | // In case of a JSON object, set the Content-Type header |
| 192 | void jsonMode() |
| 193 | { |
| 194 | addHeader("Content-Type", "application/json"); |
| 195 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 196 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | } // namespace crow |