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