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