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