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