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