Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2 | #include "http_request.h" |
| 3 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 4 | #include <async_resp.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 5 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 6 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 7 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | #include <array> |
| 10 | #include <functional> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 11 | |
| 12 | #ifdef BMCWEB_ENABLE_SSL |
| 13 | #include <boost/beast/websocket/ssl.hpp> |
| 14 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
| 18 | namespace websocket |
| 19 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 20 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | struct Connection : std::enable_shared_from_this<Connection> |
| 22 | { |
| 23 | public: |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 24 | explicit Connection(const crow::Request& reqIn) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 25 | req(reqIn.req), userdataPtr(nullptr) |
| 26 | {} |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 27 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 28 | explicit Connection(const crow::Request& reqIn, std::string user) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 29 | req(reqIn.req), userName{std::move(user)}, userdataPtr(nullptr) |
| 30 | {} |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 31 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 32 | virtual void sendBinary(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | virtual void sendBinary(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 34 | virtual void sendText(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | virtual void sendText(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 36 | virtual void close(const std::string_view msg = "quit") = 0; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 37 | virtual boost::asio::io_context& getIoContext() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | virtual ~Connection() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | void userdata(void* u) |
| 41 | { |
| 42 | userdataPtr = u; |
| 43 | } |
| 44 | void* userdata() |
| 45 | { |
| 46 | return userdataPtr; |
| 47 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 49 | const std::string& getUserName() const |
| 50 | { |
| 51 | return userName; |
| 52 | } |
| 53 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 54 | boost::beast::http::request<boost::beast::http::string_body> req; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 55 | crow::Response res; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | private: |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 58 | std::string userName{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | void* userdataPtr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 62 | template <typename Adaptor> |
| 63 | class ConnectionImpl : public Connection |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | { |
| 65 | public: |
| 66 | ConnectionImpl( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 67 | const crow::Request& reqIn, Adaptor adaptorIn, |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 68 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 69 | open_handler, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | 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 Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 74 | Connection(reqIn, reqIn.session->username), |
Adriana Kobylak | 69664cf | 2019-03-12 10:49:48 -0500 | [diff] [blame] | 75 | ws(std::move(adaptorIn)), inString(), inBuffer(inString, 131088), |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 76 | openHandler(std::move(open_handler)), |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 77 | messageHandler(std::move(message_handler)), |
| 78 | closeHandler(std::move(close_handler)), |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 79 | errorHandler(std::move(error_handler)), session(reqIn.session) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
| 81 | BMCWEB_LOG_DEBUG << "Creating new connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 82 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 84 | boost::asio::io_context& getIoContext() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 86 | return static_cast<boost::asio::io_context&>( |
| 87 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 88 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 89 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 90 | void start() |
| 91 | { |
| 92 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 93 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 94 | using bf = boost::beast::http::field; |
| 95 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 96 | std::string_view protocol = req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 97 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 98 | ws.set_option(boost::beast::websocket::stream_base::decorator( |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 99 | [session{session}, protocol{std::string(protocol)}]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | boost::beast::websocket::response_type& m) { |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 101 | |
| 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | if (!protocol.empty()) |
| 114 | { |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 115 | m.insert(bf::sec_websocket_protocol, protocol); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 117 | |
| 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 Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 127 | })); |
| 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 140 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 141 | void sendBinary(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | { |
| 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 Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 155 | void sendText(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 156 | { |
| 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 Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 169 | void close(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | { |
| 171 | ws.async_close( |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 172 | {boost::beast::websocket::close_code::normal, msg}, |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 173 | [self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 174 | if (ec == boost::asio::error::operation_aborted) |
| 175 | { |
| 176 | return; |
| 177 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | if (ec) |
| 179 | { |
| 180 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 181 | return; |
| 182 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | }); |
| 184 | } |
| 185 | |
| 186 | void acceptDone() |
| 187 | { |
| 188 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 189 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 190 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | if (openHandler) |
| 196 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 197 | openHandler(*this, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void doRead() |
| 202 | { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 203 | ws.async_read(inBuffer, |
| 204 | [this, self(shared_from_this())]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | boost::beast::error_code ec, std::size_t bytes_read) { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 206 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | } |
| 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 Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 244 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | private: |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 267 | boost::beast::websocket::stream<Adaptor> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 269 | 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 Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | std::vector<std::string> outBuffer; |
| 275 | bool doingWrite = false; |
| 276 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 277 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 278 | openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | 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 Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 282 | std::shared_ptr<persistent_data::UserSession> session; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 283 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | } // namespace websocket |
| 285 | } // namespace crow |