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