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 "http_request.hpp" |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 3 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 4 | #include <async_resp.hpp> |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 5 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 6 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 8 | #include <array> |
| 9 | #include <functional> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 10 | |
| 11 | #ifdef BMCWEB_ENABLE_SSL |
| 12 | #include <boost/beast/websocket/ssl.hpp> |
| 13 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace crow |
| 16 | { |
| 17 | namespace websocket |
| 18 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | struct Connection : std::enable_shared_from_this<Connection> |
| 21 | { |
| 22 | public: |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 23 | explicit Connection(const crow::Request& reqIn) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 24 | req(reqIn.req), userdataPtr(nullptr) |
| 25 | {} |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 26 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 27 | explicit Connection(const crow::Request& reqIn, std::string user) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 28 | req(reqIn.req), userName{std::move(user)}, userdataPtr(nullptr) |
| 29 | {} |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 30 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 31 | Connection(const Connection&) = delete; |
| 32 | Connection(Connection&&) = delete; |
| 33 | Connection& operator=(const Connection&) = delete; |
| 34 | Connection& operator=(const Connection&&) = delete; |
| 35 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 36 | virtual void sendBinary(std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | virtual void sendBinary(std::string&& msg) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 38 | virtual void sendText(std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | virtual void sendText(std::string&& msg) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 40 | virtual void close(std::string_view msg = "quit") = 0; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 41 | virtual boost::asio::io_context& getIoContext() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | virtual ~Connection() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | void userdata(void* u) |
| 45 | { |
| 46 | userdataPtr = u; |
| 47 | } |
| 48 | void* userdata() |
| 49 | { |
| 50 | return userdataPtr; |
| 51 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 52 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 53 | const std::string& getUserName() const |
| 54 | { |
| 55 | return userName; |
| 56 | } |
| 57 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 58 | boost::beast::http::request<boost::beast::http::string_body> req; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 59 | crow::Response res; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 60 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | private: |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 62 | std::string userName{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | void* userdataPtr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 66 | template <typename Adaptor> |
| 67 | class ConnectionImpl : public Connection |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | { |
| 69 | public: |
| 70 | ConnectionImpl( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 71 | const crow::Request& reqIn, Adaptor adaptorIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 72 | std::function<void(Connection&)> openHandlerIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | std::function<void(Connection&, const std::string&, bool)> |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 74 | messageHandlerIn, |
| 75 | std::function<void(Connection&, const std::string&)> closeHandlerIn, |
| 76 | std::function<void(Connection&)> errorHandlerIn) : |
Nan Zhou | 93c0202 | 2022-02-24 18:21:07 -0800 | [diff] [blame] | 77 | Connection(reqIn, reqIn.session == nullptr ? std::string{} |
| 78 | : reqIn.session->username), |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 79 | ws(std::move(adaptorIn)), inBuffer(inString, 131088), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 80 | openHandler(std::move(openHandlerIn)), |
| 81 | messageHandler(std::move(messageHandlerIn)), |
| 82 | closeHandler(std::move(closeHandlerIn)), |
| 83 | errorHandler(std::move(errorHandlerIn)), session(reqIn.session) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
dhineskumare | 02bdd96 | 2021-07-08 16:06:49 +0530 | [diff] [blame] | 85 | /* Turn on the timeouts on websocket stream to server role */ |
| 86 | ws.set_option(boost::beast::websocket::stream_base::timeout::suggested( |
| 87 | boost::beast::role_type::server)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | BMCWEB_LOG_DEBUG << "Creating new connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 89 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 90 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 91 | boost::asio::io_context& getIoContext() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 93 | return static_cast<boost::asio::io_context&>( |
| 94 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 95 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 96 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | void start() |
| 98 | { |
| 99 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 101 | using bf = boost::beast::http::field; |
| 102 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 103 | std::string_view protocol = req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 104 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 105 | ws.set_option(boost::beast::websocket::stream_base::decorator( |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 106 | [session{session}, protocol{std::string(protocol)}]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | boost::beast::websocket::response_type& m) { |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 108 | |
| 109 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 110 | if (session != nullptr) |
| 111 | { |
| 112 | // use protocol for csrf checking |
| 113 | if (session->cookieAuth && |
| 114 | !crow::utility::constantTimeStringCompare( |
| 115 | protocol, session->csrfToken)) |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 116 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 117 | BMCWEB_LOG_ERROR << "Websocket CSRF error"; |
| 118 | m.result(boost::beast::http::status::unauthorized); |
| 119 | return; |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 120 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 121 | } |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 122 | #endif |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 123 | if (!protocol.empty()) |
| 124 | { |
| 125 | m.insert(bf::sec_websocket_protocol, protocol); |
| 126 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 127 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 128 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 129 | "includeSubdomains; " |
| 130 | "preload"); |
| 131 | m.insert(bf::pragma, "no-cache"); |
| 132 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 133 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 134 | m.insert("X-XSS-Protection", "1; " |
| 135 | "mode=block"); |
| 136 | m.insert("X-Content-Type-Options", "nosniff"); |
| 137 | })); |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 138 | |
| 139 | // Perform the websocket upgrade |
| 140 | ws.async_accept(req, [this, self(shared_from_this())]( |
| 141 | boost::system::error_code ec) { |
| 142 | if (ec) |
| 143 | { |
| 144 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 145 | return; |
| 146 | } |
| 147 | acceptDone(); |
| 148 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 150 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 151 | void sendBinary(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | { |
| 153 | ws.binary(true); |
| 154 | outBuffer.emplace_back(msg); |
| 155 | doWrite(); |
| 156 | } |
| 157 | |
| 158 | void sendBinary(std::string&& msg) override |
| 159 | { |
| 160 | ws.binary(true); |
| 161 | outBuffer.emplace_back(std::move(msg)); |
| 162 | doWrite(); |
| 163 | } |
| 164 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 165 | void sendText(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | { |
| 167 | ws.text(true); |
| 168 | outBuffer.emplace_back(msg); |
| 169 | doWrite(); |
| 170 | } |
| 171 | |
| 172 | void sendText(std::string&& msg) override |
| 173 | { |
| 174 | ws.text(true); |
| 175 | outBuffer.emplace_back(std::move(msg)); |
| 176 | doWrite(); |
| 177 | } |
| 178 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 179 | void close(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | { |
| 181 | ws.async_close( |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 182 | {boost::beast::websocket::close_code::normal, msg}, |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 183 | [self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 184 | if (ec == boost::asio::error::operation_aborted) |
| 185 | { |
| 186 | return; |
| 187 | } |
| 188 | if (ec) |
| 189 | { |
| 190 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 191 | return; |
| 192 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 193 | }); |
| 194 | } |
| 195 | |
| 196 | void acceptDone() |
| 197 | { |
| 198 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 199 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 200 | doRead(); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 201 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | if (openHandler) |
| 203 | { |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 204 | openHandler(*this); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void doRead() |
| 209 | { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 210 | ws.async_read(inBuffer, |
| 211 | [this, self(shared_from_this())]( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 212 | boost::beast::error_code ec, std::size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 213 | if (ec) |
| 214 | { |
| 215 | if (ec != boost::beast::websocket::error::closed) |
| 216 | { |
| 217 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 218 | } |
| 219 | if (closeHandler) |
| 220 | { |
| 221 | std::string_view reason = ws.reason().reason; |
| 222 | closeHandler(*this, std::string(reason)); |
| 223 | } |
| 224 | return; |
| 225 | } |
| 226 | if (messageHandler) |
| 227 | { |
| 228 | messageHandler(*this, inString, ws.got_text()); |
| 229 | } |
| 230 | inBuffer.consume(bytesRead); |
| 231 | inString.clear(); |
| 232 | doRead(); |
| 233 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void doWrite() |
| 237 | { |
| 238 | // If we're already doing a write, ignore the request, it will be picked |
| 239 | // up when the current write is complete |
| 240 | if (doingWrite) |
| 241 | { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (outBuffer.empty()) |
| 246 | { |
| 247 | // Done for now |
| 248 | return; |
| 249 | } |
| 250 | doingWrite = true; |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 251 | ws.async_write(boost::asio::buffer(outBuffer.front()), |
| 252 | [this, self(shared_from_this())]( |
| 253 | boost::beast::error_code ec, std::size_t) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 254 | doingWrite = false; |
| 255 | outBuffer.erase(outBuffer.begin()); |
| 256 | if (ec == boost::beast::websocket::error::closed) |
| 257 | { |
| 258 | // Do nothing here. doRead handler will call the |
| 259 | // closeHandler. |
| 260 | close("Write error"); |
| 261 | return; |
| 262 | } |
| 263 | if (ec) |
| 264 | { |
| 265 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 266 | return; |
| 267 | } |
| 268 | doWrite(); |
| 269 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
Ed Tanous | 2aee6ca | 2021-02-01 09:52:17 -0800 | [diff] [blame] | 273 | boost::beast::websocket::stream<Adaptor, false> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 275 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 280 | std::vector<std::string> outBuffer; |
| 281 | bool doingWrite = false; |
| 282 | |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 283 | std::function<void(Connection&)> openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
| 285 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 286 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 287 | std::shared_ptr<persistent_data::UserSession> session; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 288 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | } // namespace websocket |
| 290 | } // namespace crow |