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