Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2 | #include "nlohmann/json.hpp" |
| 3 | |
| 4 | #include <boost/beast/http.hpp> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 5 | #include <string> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 6 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 7 | #include "crow/http_request.h" |
Ed Tanous | 0d485ef | 2017-05-23 09:23:53 -0700 | [diff] [blame] | 8 | #include "crow/logging.h" |
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 | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 13 | template <typename Adaptor, typename Handler, typename... Middlewares> |
| 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 | { |
| 18 | template <typename Adaptor, typename Handler, typename... Middlewares> |
| 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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | void addHeader(const boost::string_view key, const boost::string_view value) |
| 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 | |
| 32 | void addHeader(boost::beast::http::field key, boost::string_view value) |
| 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{}) |
| 38 | { |
| 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 | explicit Response(boost::beast::http::status code) : |
| 42 | stringResponse(response_type{}) |
| 43 | { |
| 44 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 45 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | explicit Response(boost::string_view body_) : |
| 47 | stringResponse(response_type{}) |
| 48 | { |
| 49 | stringResponse->body() = std::string(body_); |
| 50 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 51 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | Response(boost::beast::http::status code, boost::string_view s) : |
| 53 | stringResponse(response_type{}) |
| 54 | { |
| 55 | stringResponse->result(code); |
| 56 | stringResponse->body() = std::string(s); |
| 57 | } |
| 58 | |
| 59 | Response(Response&& r) |
| 60 | { |
| 61 | BMCWEB_LOG_DEBUG << "Moving response containers"; |
| 62 | *this = std::move(r); |
| 63 | } |
| 64 | |
| 65 | ~Response() |
| 66 | { |
| 67 | BMCWEB_LOG_DEBUG << this << " Destroying response"; |
| 68 | } |
| 69 | |
| 70 | Response& operator=(const Response& r) = delete; |
| 71 | |
| 72 | Response& operator=(Response&& r) noexcept |
| 73 | { |
| 74 | BMCWEB_LOG_DEBUG << "Moving response containers"; |
| 75 | stringResponse = std::move(r.stringResponse); |
| 76 | r.stringResponse.emplace(response_type{}); |
| 77 | jsonValue = std::move(r.jsonValue); |
| 78 | completed = r.completed; |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | void result(boost::beast::http::status v) |
| 83 | { |
| 84 | stringResponse->result(v); |
| 85 | } |
| 86 | |
| 87 | boost::beast::http::status result() |
| 88 | { |
| 89 | return stringResponse->result(); |
| 90 | } |
| 91 | |
| 92 | unsigned resultInt() |
| 93 | { |
| 94 | return stringResponse->result_int(); |
| 95 | } |
| 96 | |
| 97 | boost::string_view reason() |
| 98 | { |
| 99 | return stringResponse->reason(); |
| 100 | } |
| 101 | |
| 102 | bool isCompleted() const noexcept |
| 103 | { |
| 104 | return completed; |
| 105 | } |
| 106 | |
| 107 | std::string& body() |
| 108 | { |
| 109 | return stringResponse->body(); |
| 110 | } |
| 111 | |
| 112 | void keepAlive(bool k) |
| 113 | { |
| 114 | stringResponse->keep_alive(k); |
| 115 | } |
| 116 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame^] | 117 | bool keepAlive() |
| 118 | { |
| 119 | return stringResponse->keep_alive(); |
| 120 | } |
| 121 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | void preparePayload() |
| 123 | { |
| 124 | stringResponse->prepare_payload(); |
| 125 | }; |
| 126 | |
| 127 | void clear() |
| 128 | { |
| 129 | BMCWEB_LOG_DEBUG << this << " Clearing response containers"; |
| 130 | stringResponse.emplace(response_type{}); |
| 131 | jsonValue.clear(); |
| 132 | completed = false; |
| 133 | } |
| 134 | |
| 135 | void write(boost::string_view body_part) |
| 136 | { |
| 137 | stringResponse->body() += std::string(body_part); |
| 138 | } |
| 139 | |
| 140 | void end() |
| 141 | { |
| 142 | if (completed) |
| 143 | { |
| 144 | BMCWEB_LOG_ERROR << "Response was ended twice"; |
| 145 | return; |
| 146 | } |
| 147 | completed = true; |
| 148 | BMCWEB_LOG_DEBUG << "calling completion handler"; |
| 149 | if (completeRequestHandler) |
| 150 | { |
| 151 | BMCWEB_LOG_DEBUG << "completion handler was valid"; |
| 152 | completeRequestHandler(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void end(boost::string_view body_part) |
| 157 | { |
| 158 | write(body_part); |
| 159 | end(); |
| 160 | } |
| 161 | |
| 162 | bool isAlive() |
| 163 | { |
| 164 | return isAliveHelper && isAliveHelper(); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | bool completed{}; |
| 169 | std::function<void()> completeRequestHandler; |
| 170 | std::function<bool()> isAliveHelper; |
| 171 | |
| 172 | // In case of a JSON object, set the Content-Type header |
| 173 | void jsonMode() |
| 174 | { |
| 175 | addHeader("Content-Type", "application/json"); |
| 176 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 177 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | } // namespace crow |