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