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 | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 7 | #include "http_request.h" |
| 8 | #include "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 | 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{}) |
| 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 | 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(); |
| 107 | }; |
| 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 |