blob: 30a9b9f4b5ad519b5d10a8957e8bded76bd6d261 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
Ed Tanous04e438c2020-10-03 08:06:26 -07002#include "http_request.hpp"
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003
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>)>
Ed Tanous81ce6092020-12-17 16:54:55 +000069 openHandler,
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 std::function<void(Connection&, const std::string&, bool)>
Ed Tanous81ce6092020-12-17 16:54:55 +000071 messageHandler,
72 std::function<void(Connection&, const std::string&)> closeHandler,
73 std::function<void(Connection&)> errorHandler) :
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 Tanous81ce6092020-12-17 16:54:55 +000076 openHandler(std::move(openHandler)),
77 messageHandler(std::move(messageHandler)),
78 closeHandler(std::move(closeHandler)),
79 errorHandler(std::move(errorHandler)), session(reqIn.session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 {
dhineskumare02bdd962021-07-08 16:06:49 +053081 /* Turn on the timeouts on websocket stream to server role */
82 ws.set_option(boost::beast::websocket::stream_base::timeout::suggested(
83 boost::beast::role_type::server));
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 BMCWEB_LOG_DEBUG << "Creating new connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070085 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070086
Ed Tanous2c70f802020-09-28 14:29:23 -070087 boost::asio::io_context& getIoContext() override
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 {
Ed Tanous271584a2019-07-09 16:24:22 -070089 return static_cast<boost::asio::io_context&>(
90 ws.get_executor().context());
Ed Tanous911ac312017-08-15 09:37:42 -070091 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070092
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 void start()
94 {
95 BMCWEB_LOG_DEBUG << "starting connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070096
Ed Tanousfe5b2162019-05-22 14:28:16 -070097 using bf = boost::beast::http::field;
98
Jan Sowinskiee52ae12020-01-09 16:28:32 +000099 std::string_view protocol = req[bf::sec_websocket_protocol];
Ed Tanous7045c8d2017-04-03 10:04:37 -0700100
Ed Tanousd4d77e32020-08-18 00:07:28 -0700101 ws.set_option(boost::beast::websocket::stream_base::decorator(
James Feistf8aa3d22020-04-08 18:32:33 -0700102 [session{session}, protocol{std::string(protocol)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 boost::beast::websocket::response_type& m) {
James Feistf8aa3d22020-04-08 18:32:33 -0700104
105#ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION
Ed Tanousa90daf12021-01-15 14:18:01 -0800106 if (session != nullptr)
James Feistf8aa3d22020-04-08 18:32:33 -0700107 {
Ed Tanousa90daf12021-01-15 14:18:01 -0800108 // use protocol for csrf checking
109 if (session->cookieAuth &&
110 !crow::utility::constantTimeStringCompare(
111 protocol, session->csrfToken))
112 {
113 BMCWEB_LOG_ERROR << "Websocket CSRF error";
114 m.result(boost::beast::http::status::unauthorized);
115 return;
116 }
James Feistf8aa3d22020-04-08 18:32:33 -0700117 }
118#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 if (!protocol.empty())
120 {
Ed Tanousfe5b2162019-05-22 14:28:16 -0700121 m.insert(bf::sec_websocket_protocol, protocol);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 }
Ed Tanousfe5b2162019-05-22 14:28:16 -0700123
124 m.insert(bf::strict_transport_security, "max-age=31536000; "
125 "includeSubdomains; "
126 "preload");
127 m.insert(bf::pragma, "no-cache");
128 m.insert(bf::cache_control, "no-Store,no-Cache");
129 m.insert("Content-Security-Policy", "default-src 'self'");
130 m.insert("X-XSS-Protection", "1; "
131 "mode=block");
132 m.insert("X-Content-Type-Options", "nosniff");
Ed Tanousd4d77e32020-08-18 00:07:28 -0700133 }));
134
135 // Perform the websocket upgrade
136 ws.async_accept(req, [this, self(shared_from_this())](
137 boost::system::error_code ec) {
138 if (ec)
139 {
140 BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec;
141 return;
142 }
143 acceptDone();
144 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700146
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500147 void sendBinary(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 {
149 ws.binary(true);
150 outBuffer.emplace_back(msg);
151 doWrite();
152 }
153
154 void sendBinary(std::string&& msg) override
155 {
156 ws.binary(true);
157 outBuffer.emplace_back(std::move(msg));
158 doWrite();
159 }
160
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500161 void sendText(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 {
163 ws.text(true);
164 outBuffer.emplace_back(msg);
165 doWrite();
166 }
167
168 void sendText(std::string&& msg) override
169 {
170 ws.text(true);
171 outBuffer.emplace_back(std::move(msg));
172 doWrite();
173 }
174
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500175 void close(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176 {
177 ws.async_close(
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200178 {boost::beast::websocket::close_code::normal, msg},
Ed Tanous43b761d2019-02-13 20:10:56 -0800179 [self(shared_from_this())](boost::system::error_code ec) {
Ed Tanousceac6f72018-12-02 11:58:47 -0800180 if (ec == boost::asio::error::operation_aborted)
181 {
182 return;
183 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 if (ec)
185 {
186 BMCWEB_LOG_ERROR << "Error closing websocket " << ec;
187 return;
188 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 });
190 }
191
192 void acceptDone()
193 {
194 BMCWEB_LOG_DEBUG << "Websocket accepted connection";
195
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200196 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
197 res, [this, self(shared_from_this())]() { doRead(); });
198
199 asyncResp->res.result(boost::beast::http::status::ok);
200
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 if (openHandler)
202 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200203 openHandler(*this, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700205 }
206
207 void doRead()
208 {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500209 ws.async_read(inBuffer,
210 [this, self(shared_from_this())](
Ed Tanous81ce6092020-12-17 16:54:55 +0000211 boost::beast::error_code ec, std::size_t bytesRead) {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500212 if (ec)
213 {
214 if (ec != boost::beast::websocket::error::closed)
215 {
216 BMCWEB_LOG_ERROR << "doRead error " << ec;
217 }
218 if (closeHandler)
219 {
220 std::string_view reason = ws.reason().reason;
221 closeHandler(*this, std::string(reason));
222 }
223 return;
224 }
225 if (messageHandler)
226 {
227 messageHandler(*this, inString, ws.got_text());
228 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000229 inBuffer.consume(bytesRead);
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500230 inString.clear();
231 doRead();
232 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 }
234
235 void doWrite()
236 {
237 // If we're already doing a write, ignore the request, it will be picked
238 // up when the current write is complete
239 if (doingWrite)
240 {
241 return;
242 }
243
244 if (outBuffer.empty())
245 {
246 // Done for now
247 return;
248 }
249 doingWrite = true;
Ed Tanouscb13a392020-07-25 19:02:03 +0000250 ws.async_write(boost::asio::buffer(outBuffer.front()),
251 [this, self(shared_from_this())](
252 boost::beast::error_code ec, std::size_t) {
253 doingWrite = false;
254 outBuffer.erase(outBuffer.begin());
255 if (ec == boost::beast::websocket::error::closed)
256 {
257 // Do nothing here. doRead handler will call the
258 // closeHandler.
259 close("Write error");
260 return;
261 }
262 if (ec)
263 {
264 BMCWEB_LOG_ERROR << "Error in ws.async_write "
265 << ec;
266 return;
267 }
268 doWrite();
269 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 }
271
272 private:
Ed Tanous2aee6ca2021-02-01 09:52:17 -0800273 boost::beast::websocket::stream<Adaptor, false> ws;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700274
Ed Tanous609145a2018-09-05 16:27:36 -0700275 std::string inString;
276 boost::asio::dynamic_string_buffer<std::string::value_type,
277 std::string::traits_type,
278 std::string::allocator_type>
279 inBuffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700280 std::vector<std::string> outBuffer;
281 bool doingWrite = false;
282
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200283 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
284 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700285 std::function<void(Connection&, const std::string&, bool)> messageHandler;
286 std::function<void(Connection&, const std::string&)> closeHandler;
287 std::function<void(Connection&)> errorHandler;
Ed Tanous52cc1122020-07-18 13:51:21 -0700288 std::shared_ptr<persistent_data::UserSession> session;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700289};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290} // namespace websocket
291} // namespace crow