Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 2 | #include "async_resp.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "http_request.hpp" |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 4 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 5 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 6 | #include <boost/beast/core/multi_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 | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 21 | enum class MessageType |
| 22 | { |
| 23 | Binary, |
| 24 | Text, |
| 25 | }; |
| 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | struct Connection : std::enable_shared_from_this<Connection> |
| 28 | { |
| 29 | public: |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 30 | Connection() = default; |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 31 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 32 | Connection(const Connection&) = delete; |
| 33 | Connection(Connection&&) = delete; |
| 34 | Connection& operator=(const Connection&) = delete; |
| 35 | Connection& operator=(const Connection&&) = delete; |
| 36 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 37 | virtual void sendBinary(std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | virtual void sendBinary(std::string&& msg) = 0; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 39 | virtual void sendEx(MessageType type, std::string_view msg, |
| 40 | std::function<void()>&& onDone) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 41 | virtual void sendText(std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | virtual void sendText(std::string&& msg) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 43 | virtual void close(std::string_view msg = "quit") = 0; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 44 | virtual void deferRead() = 0; |
| 45 | virtual void resumeRead() = 0; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 46 | virtual boost::asio::io_context& getIoContext() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | virtual ~Connection() = default; |
Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 48 | virtual boost::urls::url_view url() = 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 51 | template <typename Adaptor> |
| 52 | class ConnectionImpl : public Connection |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | { |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 54 | using self_t = ConnectionImpl<Adaptor>; |
| 55 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | public: |
| 57 | ConnectionImpl( |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 58 | const boost::urls::url_view& urlViewIn, |
| 59 | const std::shared_ptr<persistent_data::UserSession>& sessionIn, |
Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 60 | Adaptor adaptorIn, std::function<void(Connection&)> openHandlerIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | std::function<void(Connection&, const std::string&, bool)> |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 62 | messageHandlerIn, |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 63 | std::function<void(crow::websocket::Connection&, std::string_view, |
| 64 | crow::websocket::MessageType type, |
| 65 | std::function<void()>&& whenComplete)> |
| 66 | messageExHandlerIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 67 | std::function<void(Connection&, const std::string&)> closeHandlerIn, |
| 68 | std::function<void(Connection&)> errorHandlerIn) : |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 69 | uri(urlViewIn), |
| 70 | ws(std::move(adaptorIn)), inBuffer(inString, 131088), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 71 | openHandler(std::move(openHandlerIn)), |
| 72 | messageHandler(std::move(messageHandlerIn)), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 73 | messageExHandler(std::move(messageExHandlerIn)), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 74 | closeHandler(std::move(closeHandlerIn)), |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 75 | errorHandler(std::move(errorHandlerIn)), session(sessionIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | { |
dhineskumare | 02bdd96 | 2021-07-08 16:06:49 +0530 | [diff] [blame] | 77 | /* Turn on the timeouts on websocket stream to server role */ |
| 78 | ws.set_option(boost::beast::websocket::stream_base::timeout::suggested( |
| 79 | boost::beast::role_type::server)); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 80 | BMCWEB_LOG_DEBUG("Creating new connection {}", logPtr(this)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 81 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 82 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 83 | boost::asio::io_context& getIoContext() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 85 | return static_cast<boost::asio::io_context&>( |
| 86 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 87 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 88 | |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 89 | void start(const crow::Request& req) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 90 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 91 | BMCWEB_LOG_DEBUG("starting connection {}", logPtr(this)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 92 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 93 | using bf = boost::beast::http::field; |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 94 | std::string protocolHeader = req.req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 95 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 96 | ws.set_option(boost::beast::websocket::stream_base::decorator( |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 97 | [session{session}, |
| 98 | protocolHeader](boost::beast::websocket::response_type& m) { |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 99 | |
| 100 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 101 | if (session != nullptr) |
| 102 | { |
| 103 | // use protocol for csrf checking |
Ed Tanous | 7e9c08e | 2023-06-16 11:29:37 -0700 | [diff] [blame] | 104 | if (session->cookieAuth && |
| 105 | !crow::utility::constantTimeStringCompare( |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 106 | protocolHeader, session->csrfToken)) |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 107 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 108 | BMCWEB_LOG_ERROR("Websocket CSRF error"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 109 | m.result(boost::beast::http::status::unauthorized); |
| 110 | return; |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 111 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 112 | } |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 113 | #endif |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 114 | if (!protocolHeader.empty()) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 115 | { |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 116 | m.insert(bf::sec_websocket_protocol, protocolHeader); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 117 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 118 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 119 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 120 | "includeSubdomains; " |
| 121 | "preload"); |
| 122 | m.insert(bf::pragma, "no-cache"); |
| 123 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 124 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 125 | m.insert("X-XSS-Protection", "1; " |
| 126 | "mode=block"); |
| 127 | m.insert("X-Content-Type-Options", "nosniff"); |
| 128 | })); |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 129 | |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 130 | // Make a pointer to keep the req alive while we accept it. |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame^] | 131 | using Body = boost::beast::http::request<bmcweb::FileBody>; |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 132 | std::unique_ptr<Body> mobile = std::make_unique<Body>(req.req); |
| 133 | Body* ptr = mobile.get(); |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 134 | // Perform the websocket upgrade |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 135 | ws.async_accept(*ptr, |
| 136 | std::bind_front(&self_t::acceptDone, this, |
| 137 | shared_from_this(), std::move(mobile))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 139 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 140 | void sendBinary(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | { |
| 142 | ws.binary(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 143 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 144 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | doWrite(); |
| 146 | } |
| 147 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 148 | void sendEx(MessageType type, std::string_view msg, |
| 149 | std::function<void()>&& onDone) override |
| 150 | { |
| 151 | if (doingWrite) |
| 152 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 153 | BMCWEB_LOG_CRITICAL( |
| 154 | "Cannot mix sendEx usage with sendBinary or sendText"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 155 | onDone(); |
| 156 | return; |
| 157 | } |
| 158 | ws.binary(type == MessageType::Binary); |
| 159 | |
| 160 | ws.async_write(boost::asio::buffer(msg), |
| 161 | [weak(weak_from_this()), onDone{std::move(onDone)}]( |
| 162 | const boost::beast::error_code& ec, size_t) { |
| 163 | std::shared_ptr<Connection> self = weak.lock(); |
zhaogang.0108 | a889420 | 2023-12-22 08:53:40 +0000 | [diff] [blame] | 164 | if (!self) |
| 165 | { |
| 166 | BMCWEB_LOG_ERROR("Connection went away"); |
| 167 | return; |
| 168 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 169 | |
| 170 | // Call the done handler regardless of whether we |
| 171 | // errored, but before we close things out |
| 172 | onDone(); |
| 173 | |
| 174 | if (ec) |
| 175 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 176 | BMCWEB_LOG_ERROR("Error in ws.async_write {}", ec); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 177 | self->close("write error"); |
| 178 | } |
| 179 | }); |
| 180 | } |
| 181 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | void sendBinary(std::string&& msg) override |
| 183 | { |
| 184 | ws.binary(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 185 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 186 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 187 | doWrite(); |
| 188 | } |
| 189 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 190 | void sendText(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 191 | { |
| 192 | ws.text(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 193 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 194 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | doWrite(); |
| 196 | } |
| 197 | |
| 198 | void sendText(std::string&& msg) override |
| 199 | { |
| 200 | ws.text(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 201 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 202 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | doWrite(); |
| 204 | } |
| 205 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 206 | void close(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | { |
| 208 | ws.async_close( |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 209 | {boost::beast::websocket::close_code::normal, msg}, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 210 | [self(shared_from_this())](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 211 | if (ec == boost::asio::error::operation_aborted) |
| 212 | { |
| 213 | return; |
| 214 | } |
| 215 | if (ec) |
| 216 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 217 | BMCWEB_LOG_ERROR("Error closing websocket {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 218 | return; |
| 219 | } |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 220 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 223 | boost::urls::url_view url() override |
| 224 | { |
| 225 | return uri; |
| 226 | } |
| 227 | |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 228 | void acceptDone(const std::shared_ptr<Connection>& /*self*/, |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame^] | 229 | const std::unique_ptr< |
| 230 | boost::beast::http::request<bmcweb::FileBody>>& /*req*/, |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 231 | const boost::system::error_code& ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | { |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 233 | if (ec) |
| 234 | { |
| 235 | BMCWEB_LOG_ERROR("Error in ws.async_accept {}", ec); |
| 236 | return; |
| 237 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 238 | BMCWEB_LOG_DEBUG("Websocket accepted connection"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 239 | |
| 240 | if (openHandler) |
| 241 | { |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 242 | openHandler(*this); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 243 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 244 | doRead(); |
| 245 | } |
| 246 | |
| 247 | void deferRead() override |
| 248 | { |
| 249 | readingDefered = true; |
| 250 | |
| 251 | // If we're not actively reading, we need to take ownership of |
| 252 | // ourselves for a small portion of time, do that, and clear when we |
| 253 | // resume. |
| 254 | selfOwned = shared_from_this(); |
| 255 | } |
| 256 | |
| 257 | void resumeRead() override |
| 258 | { |
| 259 | readingDefered = false; |
| 260 | doRead(); |
| 261 | |
| 262 | // No longer need to keep ourselves alive now that read is active. |
| 263 | selfOwned.reset(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void doRead() |
| 267 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 268 | if (readingDefered) |
| 269 | { |
| 270 | return; |
| 271 | } |
| 272 | ws.async_read(inBuffer, [this, self(shared_from_this())]( |
| 273 | const boost::beast::error_code& ec, |
| 274 | size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 275 | if (ec) |
| 276 | { |
| 277 | if (ec != boost::beast::websocket::error::closed) |
| 278 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 279 | BMCWEB_LOG_ERROR("doRead error {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 280 | } |
| 281 | if (closeHandler) |
| 282 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 283 | std::string reason{ws.reason().reason.c_str()}; |
| 284 | closeHandler(*this, reason); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 285 | } |
| 286 | return; |
| 287 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 288 | |
| 289 | handleMessage(bytesRead); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 290 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 291 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 292 | void doWrite() |
| 293 | { |
| 294 | // If we're already doing a write, ignore the request, it will be picked |
| 295 | // up when the current write is complete |
| 296 | if (doingWrite) |
| 297 | { |
| 298 | return; |
| 299 | } |
| 300 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 301 | if (outBuffer.size() == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 302 | { |
| 303 | // Done for now |
| 304 | return; |
| 305 | } |
| 306 | doingWrite = true; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 307 | ws.async_write(outBuffer.data(), [this, self(shared_from_this())]( |
| 308 | const boost::beast::error_code& ec, |
| 309 | size_t bytesSent) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 310 | doingWrite = false; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 311 | outBuffer.consume(bytesSent); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 312 | if (ec == boost::beast::websocket::error::closed) |
| 313 | { |
| 314 | // Do nothing here. doRead handler will call the |
| 315 | // closeHandler. |
| 316 | close("Write error"); |
| 317 | return; |
| 318 | } |
| 319 | if (ec) |
| 320 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 321 | BMCWEB_LOG_ERROR("Error in ws.async_write {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | doWrite(); |
| 325 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private: |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 329 | void handleMessage(size_t bytesRead) |
| 330 | { |
| 331 | if (messageExHandler) |
| 332 | { |
| 333 | // Note, because of the interactions with the read buffers, |
| 334 | // this message handler overrides the normal message handler |
| 335 | messageExHandler(*this, inString, MessageType::Binary, |
| 336 | [this, self(shared_from_this()), bytesRead]() { |
| 337 | if (self == nullptr) |
| 338 | { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | inBuffer.consume(bytesRead); |
| 343 | inString.clear(); |
| 344 | |
| 345 | doRead(); |
| 346 | }); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (messageHandler) |
| 351 | { |
| 352 | messageHandler(*this, inString, ws.got_text()); |
| 353 | } |
| 354 | inBuffer.consume(bytesRead); |
| 355 | inString.clear(); |
| 356 | doRead(); |
| 357 | } |
| 358 | |
Ninad Palsule | 052bcbf | 2023-05-30 11:10:58 -0500 | [diff] [blame] | 359 | boost::urls::url uri; |
| 360 | |
Ed Tanous | 2aee6ca | 2021-02-01 09:52:17 -0800 | [diff] [blame] | 361 | boost::beast::websocket::stream<Adaptor, false> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 362 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 363 | bool readingDefered = false; |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 364 | std::string inString; |
| 365 | boost::asio::dynamic_string_buffer<std::string::value_type, |
| 366 | std::string::traits_type, |
| 367 | std::string::allocator_type> |
| 368 | inBuffer; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 369 | |
| 370 | boost::beast::multi_buffer outBuffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 371 | bool doingWrite = false; |
| 372 | |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 373 | std::function<void(Connection&)> openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 374 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 375 | std::function<void(crow::websocket::Connection&, std::string_view, |
| 376 | crow::websocket::MessageType type, |
| 377 | std::function<void()>&& whenComplete)> |
| 378 | messageExHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 379 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 380 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 381 | std::shared_ptr<persistent_data::UserSession> session; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 382 | |
| 383 | std::shared_ptr<Connection> selfOwned; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 384 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 385 | } // namespace websocket |
| 386 | } // namespace crow |