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 | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 7 | |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 8 | #include <optional> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | #include <string> |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 10 | #include <string_view> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 12 | namespace crow |
| 13 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 15 | template <typename Adaptor, typename Handler> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 16 | class Connection; |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 17 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | struct Response |
| 19 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 20 | template <typename Adaptor, typename Handler> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | friend class crow::Connection; |
| 22 | using response_type = |
| 23 | boost::beast::http::response<boost::beast::http::string_body>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 24 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 25 | std::optional<response_type> stringResponse; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | nlohmann::json jsonValue; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 29 | void addHeader(const std::string_view key, const std::string_view value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | { |
| 31 | stringResponse->set(key, value); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 32 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 34 | void addHeader(boost::beast::http::field key, std::string_view value) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | { |
| 36 | stringResponse->set(key, value); |
Ed Tanous | 2cd4cc1 | 2018-07-25 10:51:19 -0700 | [diff] [blame] | 37 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 38 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | Response() : stringResponse(response_type{}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 40 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 41 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 42 | ~Response() = default; |
| 43 | |
| 44 | Response(const Response&) = delete; |
| 45 | Response(Response&&) = delete; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | Response& operator=(const Response& r) = delete; |
| 48 | |
| 49 | Response& operator=(Response&& r) noexcept |
| 50 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 51 | BMCWEB_LOG_DEBUG << "Moving response containers; this: " << this |
| 52 | << "; other: " << &r; |
| 53 | if (this == &r) |
| 54 | { |
| 55 | return *this; |
| 56 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | stringResponse = std::move(r.stringResponse); |
| 58 | r.stringResponse.emplace(response_type{}); |
| 59 | jsonValue = std::move(r.jsonValue); |
| 60 | completed = r.completed; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 61 | completeRequestHandler = std::move(r.completeRequestHandler); |
| 62 | isAliveHelper = std::move(r.isAliveHelper); |
| 63 | r.completeRequestHandler = nullptr; |
| 64 | r.isAliveHelper = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | void result(boost::beast::http::status v) |
| 69 | { |
| 70 | stringResponse->result(v); |
| 71 | } |
| 72 | |
| 73 | boost::beast::http::status result() |
| 74 | { |
| 75 | return stringResponse->result(); |
| 76 | } |
| 77 | |
| 78 | unsigned resultInt() |
| 79 | { |
| 80 | return stringResponse->result_int(); |
| 81 | } |
| 82 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 83 | std::string_view reason() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
| 85 | return stringResponse->reason(); |
| 86 | } |
| 87 | |
| 88 | bool isCompleted() const noexcept |
| 89 | { |
| 90 | return completed; |
| 91 | } |
| 92 | |
| 93 | std::string& body() |
| 94 | { |
| 95 | return stringResponse->body(); |
| 96 | } |
| 97 | |
| 98 | void keepAlive(bool k) |
| 99 | { |
| 100 | stringResponse->keep_alive(k); |
| 101 | } |
| 102 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 103 | bool keepAlive() |
| 104 | { |
| 105 | return stringResponse->keep_alive(); |
| 106 | } |
| 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | void preparePayload() |
| 109 | { |
| 110 | stringResponse->prepare_payload(); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 111 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | |
| 113 | void clear() |
| 114 | { |
| 115 | BMCWEB_LOG_DEBUG << this << " Clearing response containers"; |
| 116 | stringResponse.emplace(response_type{}); |
| 117 | jsonValue.clear(); |
| 118 | completed = false; |
| 119 | } |
| 120 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 121 | void write(std::string_view bodyPart) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 123 | stringResponse->body() += std::string(bodyPart); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void end() |
| 127 | { |
| 128 | if (completed) |
| 129 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 130 | BMCWEB_LOG_ERROR << this << " Response was ended twice"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | completed = true; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 134 | BMCWEB_LOG_DEBUG << this << " calling completion handler"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 135 | if (completeRequestHandler) |
| 136 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 137 | BMCWEB_LOG_DEBUG << this << " completion handler was valid"; |
| 138 | completeRequestHandler(*this); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | bool isAlive() |
| 143 | { |
| 144 | return isAliveHelper && isAliveHelper(); |
| 145 | } |
| 146 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 147 | void setCompleteRequestHandler(std::function<void(Response&)>&& handler) |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 148 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 149 | BMCWEB_LOG_DEBUG << this << " setting completion handler"; |
| 150 | completeRequestHandler = std::move(handler); |
| 151 | } |
| 152 | |
| 153 | std::function<void(Response&)> releaseCompleteRequestHandler() |
| 154 | { |
| 155 | BMCWEB_LOG_DEBUG << this << " releasing completion handler" |
| 156 | << static_cast<bool>(completeRequestHandler); |
| 157 | std::function<void(Response&)> ret = completeRequestHandler; |
| 158 | completeRequestHandler = nullptr; |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | void setIsAliveHelper(std::function<bool()>&& handler) |
| 163 | { |
| 164 | isAliveHelper = std::move(handler); |
| 165 | } |
| 166 | |
| 167 | std::function<bool()> releaseIsAliveHelper() |
| 168 | { |
| 169 | std::function<bool()> ret = std::move(isAliveHelper); |
| 170 | isAliveHelper = nullptr; |
| 171 | return ret; |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | private: |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 175 | bool completed = false; |
| 176 | std::function<void(Response&)> completeRequestHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | std::function<bool()> isAliveHelper; |
| 178 | |
| 179 | // In case of a JSON object, set the Content-Type header |
| 180 | void jsonMode() |
| 181 | { |
| 182 | addHeader("Content-Type", "application/json"); |
| 183 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 184 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 185 | } // namespace crow |