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