blob: 40fbcc01b49dd2d6f5b38e95b35f50d8ddfaa713 [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 {
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
Ed Tanousa90daf12021-01-15 14:18:01 -0800103 if (session != nullptr)
James Feistf8aa3d22020-04-08 18:32:33 -0700104 {
Ed Tanousa90daf12021-01-15 14:18:01 -0800105 // use protocol for csrf checking
106 if (session->cookieAuth &&
107 !crow::utility::constantTimeStringCompare(
108 protocol, session->csrfToken))
109 {
110 BMCWEB_LOG_ERROR << "Websocket CSRF error";
111 m.result(boost::beast::http::status::unauthorized);
112 return;
113 }
James Feistf8aa3d22020-04-08 18:32:33 -0700114 }
115#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 if (!protocol.empty())
117 {
Ed Tanousfe5b2162019-05-22 14:28:16 -0700118 m.insert(bf::sec_websocket_protocol, protocol);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 }
Ed Tanousfe5b2162019-05-22 14:28:16 -0700120
121 m.insert(bf::strict_transport_security, "max-age=31536000; "
122 "includeSubdomains; "
123 "preload");
124 m.insert(bf::pragma, "no-cache");
125 m.insert(bf::cache_control, "no-Store,no-Cache");
126 m.insert("Content-Security-Policy", "default-src 'self'");
127 m.insert("X-XSS-Protection", "1; "
128 "mode=block");
129 m.insert("X-Content-Type-Options", "nosniff");
Ed Tanousd4d77e32020-08-18 00:07:28 -0700130 }));
131
132 // Perform the websocket upgrade
133 ws.async_accept(req, [this, self(shared_from_this())](
134 boost::system::error_code ec) {
135 if (ec)
136 {
137 BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec;
138 return;
139 }
140 acceptDone();
141 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700143
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500144 void sendBinary(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145 {
146 ws.binary(true);
147 outBuffer.emplace_back(msg);
148 doWrite();
149 }
150
151 void sendBinary(std::string&& msg) override
152 {
153 ws.binary(true);
154 outBuffer.emplace_back(std::move(msg));
155 doWrite();
156 }
157
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500158 void sendText(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 {
160 ws.text(true);
161 outBuffer.emplace_back(msg);
162 doWrite();
163 }
164
165 void sendText(std::string&& msg) override
166 {
167 ws.text(true);
168 outBuffer.emplace_back(std::move(msg));
169 doWrite();
170 }
171
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500172 void close(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 {
174 ws.async_close(
Wludzik, Jozeff6a0d632020-07-16 15:16:02 +0200175 {boost::beast::websocket::close_code::normal, msg},
Ed Tanous43b761d2019-02-13 20:10:56 -0800176 [self(shared_from_this())](boost::system::error_code ec) {
Ed Tanousceac6f72018-12-02 11:58:47 -0800177 if (ec == boost::asio::error::operation_aborted)
178 {
179 return;
180 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 if (ec)
182 {
183 BMCWEB_LOG_ERROR << "Error closing websocket " << ec;
184 return;
185 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 });
187 }
188
189 void acceptDone()
190 {
191 BMCWEB_LOG_DEBUG << "Websocket accepted connection";
192
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200193 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
194 res, [this, self(shared_from_this())]() { doRead(); });
195
196 asyncResp->res.result(boost::beast::http::status::ok);
197
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 if (openHandler)
199 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200200 openHandler(*this, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 }
203
204 void doRead()
205 {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500206 ws.async_read(inBuffer,
207 [this, self(shared_from_this())](
Ed Tanous81ce6092020-12-17 16:54:55 +0000208 boost::beast::error_code ec, std::size_t bytesRead) {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500209 if (ec)
210 {
211 if (ec != boost::beast::websocket::error::closed)
212 {
213 BMCWEB_LOG_ERROR << "doRead error " << ec;
214 }
215 if (closeHandler)
216 {
217 std::string_view reason = ws.reason().reason;
218 closeHandler(*this, std::string(reason));
219 }
220 return;
221 }
222 if (messageHandler)
223 {
224 messageHandler(*this, inString, ws.got_text());
225 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000226 inBuffer.consume(bytesRead);
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500227 inString.clear();
228 doRead();
229 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 }
231
232 void doWrite()
233 {
234 // If we're already doing a write, ignore the request, it will be picked
235 // up when the current write is complete
236 if (doingWrite)
237 {
238 return;
239 }
240
241 if (outBuffer.empty())
242 {
243 // Done for now
244 return;
245 }
246 doingWrite = true;
Ed Tanouscb13a392020-07-25 19:02:03 +0000247 ws.async_write(boost::asio::buffer(outBuffer.front()),
248 [this, self(shared_from_this())](
249 boost::beast::error_code ec, std::size_t) {
250 doingWrite = false;
251 outBuffer.erase(outBuffer.begin());
252 if (ec == boost::beast::websocket::error::closed)
253 {
254 // Do nothing here. doRead handler will call the
255 // closeHandler.
256 close("Write error");
257 return;
258 }
259 if (ec)
260 {
261 BMCWEB_LOG_ERROR << "Error in ws.async_write "
262 << ec;
263 return;
264 }
265 doWrite();
266 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700267 }
268
269 private:
Ed Tanous2aee6ca2021-02-01 09:52:17 -0800270 boost::beast::websocket::stream<Adaptor, false> ws;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271
Ed Tanous609145a2018-09-05 16:27:36 -0700272 std::string inString;
273 boost::asio::dynamic_string_buffer<std::string::value_type,
274 std::string::traits_type,
275 std::string::allocator_type>
276 inBuffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277 std::vector<std::string> outBuffer;
278 bool doingWrite = false;
279
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200280 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
281 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700282 std::function<void(Connection&, const std::string&, bool)> messageHandler;
283 std::function<void(Connection&, const std::string&)> closeHandler;
284 std::function<void(Connection&)> errorHandler;
Ed Tanous52cc1122020-07-18 13:51:21 -0700285 std::shared_ptr<persistent_data::UserSession> session;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700286};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287} // namespace websocket
288} // namespace crow