blob: c467d2559451e03905b955aca7738402567bd69a [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
Ed Tanous911ac312017-08-15 09:37:42 -07002#include <array>
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02003#include <async_resp.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07004#include <boost/algorithm/string/predicate.hpp>
Ed Tanous609145a2018-09-05 16:27:36 -07005#include <boost/asio/buffer.hpp>
Ed Tanous1b0044b2018-08-03 14:30:05 -07006#include <boost/beast/websocket.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07007#include <functional>
8
Ed Tanousc94ad492019-10-10 15:39:33 -07009#include "http_request.h"
Ed Tanous1b0044b2018-08-03 14:30:05 -070010
11#ifdef BMCWEB_ENABLE_SSL
12#include <boost/beast/websocket/ssl.hpp>
13#endif
Ed Tanous7045c8d2017-04-03 10:04:37 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace crow
16{
17namespace websocket
18{
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020019
Ed Tanous1abe55e2018-09-05 08:30:59 -070020struct Connection : std::enable_shared_from_this<Connection>
21{
22 public:
Jan Sowinskiee52ae12020-01-09 16:28:32 +000023 explicit Connection(const crow::Request& reqIn) :
24 req(reqIn.req), userdataPtr(nullptr){};
Ed Tanous911ac312017-08-15 09:37:42 -070025
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010026 explicit Connection(const crow::Request& reqIn, std::string user) :
27 req(reqIn.req), userName{std::move(user)}, userdataPtr(nullptr){};
28
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050029 virtual void sendBinary(const std::string_view msg) = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070030 virtual void sendBinary(std::string&& msg) = 0;
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050031 virtual void sendText(const std::string_view msg) = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 virtual void sendText(std::string&& msg) = 0;
Adriana Kobylakae29b8c2019-04-24 11:19:18 -050033 virtual void close(const std::string_view msg = "quit") = 0;
Ed Tanousceac6f72018-12-02 11:58:47 -080034 virtual boost::asio::io_context& get_io_context() = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 virtual ~Connection() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -070036
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 void userdata(void* u)
38 {
39 userdataPtr = u;
40 }
41 void* userdata()
42 {
43 return userdataPtr;
44 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070045
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010046 const std::string& getUserName() const
47 {
48 return userName;
49 }
50
Jan Sowinskiee52ae12020-01-09 16:28:32 +000051 boost::beast::http::request<boost::beast::http::string_body> req;
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020052 crow::Response res;
Ed Tanous911ac312017-08-15 09:37:42 -070053
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 private:
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010055 std::string userName{};
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 void* userdataPtr;
Ed Tanous7045c8d2017-04-03 10:04:37 -070057};
58
Ed Tanous1abe55e2018-09-05 08:30:59 -070059template <typename Adaptor> class ConnectionImpl : public Connection
60{
61 public:
62 ConnectionImpl(
Jan Sowinskiee52ae12020-01-09 16:28:32 +000063 const crow::Request& reqIn, Adaptor adaptorIn,
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020064 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
65 open_handler,
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 std::function<void(Connection&, const std::string&, bool)>
67 message_handler,
68 std::function<void(Connection&, const std::string&)> close_handler,
69 std::function<void(Connection&)> error_handler) :
Przemyslaw Czarnowski250b0eb2020-02-24 10:23:56 +010070 Connection(reqIn, reqIn.session->username),
Adriana Kobylak69664cf2019-03-12 10:49:48 -050071 ws(std::move(adaptorIn)), inString(), inBuffer(inString, 131088),
Ed Tanousceac6f72018-12-02 11:58:47 -080072 openHandler(std::move(open_handler)),
Ed Tanous55c7b7a2018-05-22 15:27:24 -070073 messageHandler(std::move(message_handler)),
74 closeHandler(std::move(close_handler)),
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 errorHandler(std::move(error_handler))
76 {
77 BMCWEB_LOG_DEBUG << "Creating new connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070078 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070079
Ed Tanousceac6f72018-12-02 11:58:47 -080080 boost::asio::io_context& get_io_context() override
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
Ed Tanous271584a2019-07-09 16:24:22 -070082 return static_cast<boost::asio::io_context&>(
83 ws.get_executor().context());
Ed Tanous911ac312017-08-15 09:37:42 -070084 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070085
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 void start()
87 {
88 BMCWEB_LOG_DEBUG << "starting connection " << this;
Ed Tanous7045c8d2017-04-03 10:04:37 -070089
Ed Tanousfe5b2162019-05-22 14:28:16 -070090 using bf = boost::beast::http::field;
91
Jan Sowinskiee52ae12020-01-09 16:28:32 +000092 std::string_view protocol = req[bf::sec_websocket_protocol];
Ed Tanous7045c8d2017-04-03 10:04:37 -070093
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 // Perform the websocket upgrade
95 ws.async_accept_ex(
Jan Sowinskiee52ae12020-01-09 16:28:32 +000096 req,
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 [protocol{std::string(protocol)}](
98 boost::beast::websocket::response_type& m) {
99 if (!protocol.empty())
100 {
Ed Tanousfe5b2162019-05-22 14:28:16 -0700101 m.insert(bf::sec_websocket_protocol, protocol);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 }
Ed Tanousfe5b2162019-05-22 14:28:16 -0700103
104 m.insert(bf::strict_transport_security, "max-age=31536000; "
105 "includeSubdomains; "
106 "preload");
107 m.insert(bf::pragma, "no-cache");
108 m.insert(bf::cache_control, "no-Store,no-Cache");
109 m.insert("Content-Security-Policy", "default-src 'self'");
110 m.insert("X-XSS-Protection", "1; "
111 "mode=block");
112 m.insert("X-Content-Type-Options", "nosniff");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 },
114 [this, self(shared_from_this())](boost::system::error_code ec) {
115 if (ec)
116 {
117 BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec;
118 return;
119 }
120 acceptDone();
121 });
122 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700123
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500124 void sendBinary(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125 {
126 ws.binary(true);
127 outBuffer.emplace_back(msg);
128 doWrite();
129 }
130
131 void sendBinary(std::string&& msg) override
132 {
133 ws.binary(true);
134 outBuffer.emplace_back(std::move(msg));
135 doWrite();
136 }
137
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500138 void sendText(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 {
140 ws.text(true);
141 outBuffer.emplace_back(msg);
142 doWrite();
143 }
144
145 void sendText(std::string&& msg) override
146 {
147 ws.text(true);
148 outBuffer.emplace_back(std::move(msg));
149 doWrite();
150 }
151
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500152 void close(const std::string_view msg) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 {
154 ws.async_close(
155 boost::beast::websocket::close_code::normal,
Ed Tanous43b761d2019-02-13 20:10:56 -0800156 [self(shared_from_this())](boost::system::error_code ec) {
Ed Tanousceac6f72018-12-02 11:58:47 -0800157 if (ec == boost::asio::error::operation_aborted)
158 {
159 return;
160 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 if (ec)
162 {
163 BMCWEB_LOG_ERROR << "Error closing websocket " << ec;
164 return;
165 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 });
167 }
168
169 void acceptDone()
170 {
171 BMCWEB_LOG_DEBUG << "Websocket accepted connection";
172
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200173 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(
174 res, [this, self(shared_from_this())]() { doRead(); });
175
176 asyncResp->res.result(boost::beast::http::status::ok);
177
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 if (openHandler)
179 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200180 openHandler(*this, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182 }
183
184 void doRead()
185 {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500186 ws.async_read(inBuffer,
187 [this, self(shared_from_this())](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 boost::beast::error_code ec, std::size_t bytes_read) {
Adriana Kobylakae29b8c2019-04-24 11:19:18 -0500189 if (ec)
190 {
191 if (ec != boost::beast::websocket::error::closed)
192 {
193 BMCWEB_LOG_ERROR << "doRead error " << ec;
194 }
195 if (closeHandler)
196 {
197 std::string_view reason = ws.reason().reason;
198 closeHandler(*this, std::string(reason));
199 }
200 return;
201 }
202 if (messageHandler)
203 {
204 messageHandler(*this, inString, ws.got_text());
205 }
206 inBuffer.consume(bytes_read);
207 inString.clear();
208 doRead();
209 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210 }
211
212 void doWrite()
213 {
214 // If we're already doing a write, ignore the request, it will be picked
215 // up when the current write is complete
216 if (doingWrite)
217 {
218 return;
219 }
220
221 if (outBuffer.empty())
222 {
223 // Done for now
224 return;
225 }
226 doingWrite = true;
227 ws.async_write(
228 boost::asio::buffer(outBuffer.front()),
229 [this, self(shared_from_this())](boost::beast::error_code ec,
230 std::size_t bytes_written) {
231 doingWrite = false;
232 outBuffer.erase(outBuffer.begin());
233 if (ec == boost::beast::websocket::error::closed)
234 {
235 // Do nothing here. doRead handler will call the
236 // closeHandler.
237 close("Write error");
238 return;
239 }
240 if (ec)
241 {
242 BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec;
243 return;
244 }
245 doWrite();
246 });
247 }
248
249 private:
Ed Tanousceac6f72018-12-02 11:58:47 -0800250 boost::beast::websocket::stream<Adaptor> ws;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251
Ed Tanous609145a2018-09-05 16:27:36 -0700252 std::string inString;
253 boost::asio::dynamic_string_buffer<std::string::value_type,
254 std::string::traits_type,
255 std::string::allocator_type>
256 inBuffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 std::vector<std::string> outBuffer;
258 bool doingWrite = false;
259
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200260 std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)>
261 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 std::function<void(Connection&, const std::string&, bool)> messageHandler;
263 std::function<void(Connection&, const std::string&)> closeHandler;
264 std::function<void(Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700265};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266} // namespace websocket
267} // namespace crow