blob: 021fffa07bff32638b520453aacaabc621b7b7e8 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002#include "http_request.h"
3
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02004#include <async_resp.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07005#include <boost/algorithm/string/predicate.hpp>
Ed Tanous609145a2018-09-05 16:27:36 -07006#include <boost/asio/buffer.hpp>
Ed Tanous1b0044b2018-08-03 14:30:05 -07007#include <boost/beast/websocket.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008
Gunnar Mills1214b7e2020-06-04 10:11:30 -05009#include <array>
10#include <functional>
Ed Tanous1b0044b2018-08-03 14:30:05 -070011
12#ifdef BMCWEB_ENABLE_SSL
13#include <boost/beast/websocket/ssl.hpp>
14#endif
Ed Tanous7045c8d2017-04-03 10:04:37 -070015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016namespace crow
17{
18namespace websocket
19{
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021struct Connection : std::enable_shared_from_this<Connection>
22{
23 public:
Jan Sowinskiee52ae12020-01-09 16:28:32 +000024 explicit Connection(const crow::Request& reqIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +000025 req(reqIn.req), userdataPtr(nullptr)
26 {}
Ed Tanous911ac312017-08-15 09:37:42 -070027
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010028 explicit Connection(const crow::Request& reqIn, std::string user) :
Ed Tanous23a21a12020-07-25 04:45:05 +000029 req(reqIn.req), userName{std::move(user)}, userdataPtr(nullptr)
30 {}
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010031
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050032 virtual void sendBinary(const std::string_view msg) = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 virtual void sendBinary(std::string&& msg) = 0;
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050034 virtual void sendText(const std::string_view msg) = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 virtual void sendText(std::string&& msg) = 0;
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050036 virtual void close(const std::string_view msg = "quit") = 0;
Ed Tanous2c70f802020-09-28 14:29:23 -070037 virtual boost::asio::io_context& getIoContext() = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 virtual ~Connection() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -070039
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 void userdata(void* u)
41 {
42 userdataPtr = u;
43 }
44 void* userdata()
45 {
46 return userdataPtr;
47 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070048
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010049 const std::string& getUserName() const
50 {
51 return userName;
52 }
53
Jan Sowinskiee52ae12020-01-09 16:28:32 +000054 boost::beast::http::request<boost::beast::http::string_body> req;
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020055 crow::Response res;
Ed Tanous911ac312017-08-15 09:37:42 -070056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 private:
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010058 std::string userName{};
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 void* userdataPtr;
Ed Tanous7045c8d2017-04-03 10:04:37 -070060};
61
Gunnar Mills1214b7e2020-06-04 10:11:30 -050062template <typename Adaptor>
63class ConnectionImpl : public Connection
Ed Tanous1abe55e2018-09-05 08:30:59 -070064{
65 public:
66 ConnectionImpl(
Jan Sowinskiee52ae12020-01-09 16:28:32 +000067 const crow::Request& reqIn, Adaptor adaptorIn,
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020068 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
69 open_handler,
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 std::function<void(Connection&, const std::string&, bool)>
71 message_handler,
72 std::function<void(Connection&, const std::string&)> close_handler,
73 std::function<void(Connection&)> error_handler) :
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010074 Connection(reqIn, reqIn.session->username),
Adriana Kobylak69664cf2019-03-12 10:49:48 -050075 ws(std::move(adaptorIn)), inString(), inBuffer(inString, 131088),
Ed Tanousceac6f72018-12-02 11:58:47 -080076 openHandler(std::move(open_handler)),
Ed Tanous55c7b7a2018-05-22 15:27:24 -070077 messageHandler(std::move(message_handler)),
78 closeHandler(std::move(close_handler)),
James Feistf8aa3d22020-04-08 18:32:33 -070079 errorHandler(std::move(error_handler)), session(reqIn.session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 {
81 BMCWEB_LOG_DEBUG << "Creating new connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070082 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070083
Ed Tanous2c70f802020-09-28 14:29:23 -070084 boost::asio::io_context& getIoContext() override
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
Ed Tanous271584a2019-07-09 16:24:22 -070086 return static_cast<boost::asio::io_context&>(
87 ws.get_executor().context());
Ed Tanous911ac312017-08-15 09:37:42 -070088 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070089
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 void start()
91 {
92 BMCWEB_LOG_DEBUG << "starting connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070093
Ed Tanousfe5b2162019-05-22 14:28:16 -070094 using bf = boost::beast::http::field;
95
Jan Sowinskiee52ae12020-01-09 16:28:32 +000096 std::string_view protocol = req[bf::sec_websocket_protocol];
Ed Tanous7045c8d2017-04-03 10:04:37 -070097
Ed Tanousd4d77e32020-08-18 00:07:28 -070098 ws.set_option(boost::beast::websocket::stream_base::decorator(
James Feistf8aa3d22020-04-08 18:32:33 -070099 [session{session}, protocol{std::string(protocol)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 boost::beast::websocket::response_type& m) {
James Feistf8aa3d22020-04-08 18:32:33 -0700101
102#ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION
103 // use protocol for csrf checking
104 if (session->cookieAuth &&
105 !crow::utility::constantTimeStringCompare(
106 protocol, session->csrfToken))
107 {
108 BMCWEB_LOG_ERROR << "Websocket CSRF error";
109 m.result(boost::beast::http::status::unauthorized);
110 return;
111 }
112#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 if (!protocol.empty())
114 {
Ed Tanousfe5b2162019-05-22 14:28:16 -0700115 m.insert(bf::sec_websocket_protocol, protocol);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 }
Ed Tanousfe5b2162019-05-22 14:28:16 -0700117
118 m.insert(bf::strict_transport_security, "max-age=31536000; "
119 "includeSubdomains; "
120 "preload");
121 m.insert(bf::pragma, "no-cache");
122 m.insert(bf::cache_control, "no-Store,no-Cache");
123 m.insert("Content-Security-Policy", "default-src 'self'");
124 m.insert("X-XSS-Protection", "1; "
125 "mode=block");
126 m.insert("X-Content-Type-Options", "nosniff");
Ed Tanousd4d77e32020-08-18 00:07:28 -0700127 }));
128
129 // Perform the websocket upgrade
130 ws.async_accept(req, [this, self(shared_from_this())](
131 boost::system::error_code ec) {
132 if (ec)
133 {
134 BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec;
135 return;
136 }
137 acceptDone();
138 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700140
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500141 void sendBinary(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 {
143 ws.binary(true);
144 outBuffer.emplace_back(msg);
145 doWrite();
146 }
147
148 void sendBinary(std::string&& msg) override
149 {
150 ws.binary(true);
151 outBuffer.emplace_back(std::move(msg));
152 doWrite();
153 }
154
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500155 void sendText(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 {
157 ws.text(true);
158 outBuffer.emplace_back(msg);
159 doWrite();
160 }
161
162 void sendText(std::string&& msg) override
163 {
164 ws.text(true);
165 outBuffer.emplace_back(std::move(msg));
166 doWrite();
167 }
168
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500169 void close(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 {
171 ws.async_close(
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200172 {boost::beast::websocket::close_code::normal, msg},
Ed Tanous43b761d2019-02-13 20:10:56 -0800173 [self(shared_from_this())](boost::system::error_code ec) {
Ed Tanousceac6f72018-12-02 11:58:47 -0800174 if (ec == boost::asio::error::operation_aborted)
175 {
176 return;
177 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 if (ec)
179 {
180 BMCWEB_LOG_ERROR << "Error closing websocket " << ec;
181 return;
182 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 });
184 }
185
186 void acceptDone()
187 {
188 BMCWEB_LOG_DEBUG << "Websocket accepted connection";
189
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200190 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
191 res, [this, self(shared_from_this())]() { doRead(); });
192
193 asyncResp->res.result(boost::beast::http::status::ok);
194
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 if (openHandler)
196 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200197 openHandler(*this, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 }
200
201 void doRead()
202 {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500203 ws.async_read(inBuffer,
204 [this, self(shared_from_this())](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700205 boost::beast::error_code ec, std::size_t bytes_read) {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500206 if (ec)
207 {
208 if (ec != boost::beast::websocket::error::closed)
209 {
210 BMCWEB_LOG_ERROR << "doRead error " << ec;
211 }
212 if (closeHandler)
213 {
214 std::string_view reason = ws.reason().reason;
215 closeHandler(*this, std::string(reason));
216 }
217 return;
218 }
219 if (messageHandler)
220 {
221 messageHandler(*this, inString, ws.got_text());
222 }
223 inBuffer.consume(bytes_read);
224 inString.clear();
225 doRead();
226 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
228
229 void doWrite()
230 {
231 // If we're already doing a write, ignore the request, it will be picked
232 // up when the current write is complete
233 if (doingWrite)
234 {
235 return;
236 }
237
238 if (outBuffer.empty())
239 {
240 // Done for now
241 return;
242 }
243 doingWrite = true;
Ed Tanouscb13a392020-07-25 19:02:03 +0000244 ws.async_write(boost::asio::buffer(outBuffer.front()),
245 [this, self(shared_from_this())](
246 boost::beast::error_code ec, std::size_t) {
247 doingWrite = false;
248 outBuffer.erase(outBuffer.begin());
249 if (ec == boost::beast::websocket::error::closed)
250 {
251 // Do nothing here. doRead handler will call the
252 // closeHandler.
253 close("Write error");
254 return;
255 }
256 if (ec)
257 {
258 BMCWEB_LOG_ERROR << "Error in ws.async_write "
259 << ec;
260 return;
261 }
262 doWrite();
263 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700264 }
265
266 private:
Ed Tanousceac6f72018-12-02 11:58:47 -0800267 boost::beast::websocket::stream<Adaptor> ws;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268
Ed Tanous609145a2018-09-05 16:27:36 -0700269 std::string inString;
270 boost::asio::dynamic_string_buffer<std::string::value_type,
271 std::string::traits_type,
272 std::string::allocator_type>
273 inBuffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700274 std::vector<std::string> outBuffer;
275 bool doingWrite = false;
276
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200277 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
278 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 std::function<void(Connection&, const std::string&, bool)> messageHandler;
280 std::function<void(Connection&, const std::string&)> closeHandler;
281 std::function<void(Connection&)> errorHandler;
Ed Tanous52cc1122020-07-18 13:51:21 -0700282 std::shared_ptr<persistent_data::UserSession> session;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700283};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284} // namespace websocket
285} // namespace crow