Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 2 | #include "async_resp.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "http_request.hpp" |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 4 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 5 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 6 | #include <boost/beast/core/multi_buffer.hpp> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 7 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | #include <array> |
| 10 | #include <functional> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 11 | |
| 12 | #ifdef BMCWEB_ENABLE_SSL |
| 13 | #include <boost/beast/websocket/ssl.hpp> |
| 14 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
| 18 | namespace websocket |
| 19 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 20 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 21 | enum class MessageType |
| 22 | { |
| 23 | Binary, |
| 24 | Text, |
| 25 | }; |
| 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | struct Connection : std::enable_shared_from_this<Connection> |
| 28 | { |
| 29 | public: |
Ed Tanous | e551b5f | 2023-02-27 14:19:07 -0800 | [diff] [blame] | 30 | explicit Connection(const crow::Request& reqIn) : req(reqIn.req) |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 31 | {} |
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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | virtual void sendBinary(std::string&& msg) = 0; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 40 | virtual void sendEx(MessageType type, std::string_view msg, |
| 41 | std::function<void()>&& onDone) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 42 | virtual void sendText(std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | virtual void sendText(std::string&& msg) = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 44 | virtual void close(std::string_view msg = "quit") = 0; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 45 | virtual void deferRead() = 0; |
| 46 | virtual void resumeRead() = 0; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 47 | virtual boost::asio::io_context& getIoContext() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | virtual ~Connection() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 49 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 50 | boost::beast::http::request<boost::beast::http::string_body> req; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 53 | template <typename Adaptor> |
| 54 | class ConnectionImpl : public Connection |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | ConnectionImpl( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 58 | const crow::Request& reqIn, Adaptor adaptorIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 59 | 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) : |
Ed Tanous | e551b5f | 2023-02-27 14:19:07 -0800 | [diff] [blame] | 68 | Connection(reqIn), |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 69 | ws(std::move(adaptorIn)), inBuffer(inString, 131088), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 70 | openHandler(std::move(openHandlerIn)), |
| 71 | messageHandler(std::move(messageHandlerIn)), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 72 | messageExHandler(std::move(messageExHandlerIn)), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 73 | closeHandler(std::move(closeHandlerIn)), |
| 74 | errorHandler(std::move(errorHandlerIn)), session(reqIn.session) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | { |
dhineskumare | 02bdd96 | 2021-07-08 16:06:49 +0530 | [diff] [blame] | 76 | /* Turn on the timeouts on websocket stream to server role */ |
| 77 | ws.set_option(boost::beast::websocket::stream_base::timeout::suggested( |
| 78 | boost::beast::role_type::server)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | BMCWEB_LOG_DEBUG << "Creating new connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 80 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 81 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 82 | boost::asio::io_context& getIoContext() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 84 | return static_cast<boost::asio::io_context&>( |
| 85 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 86 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | void start() |
| 89 | { |
| 90 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 91 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 92 | using bf = boost::beast::http::field; |
| 93 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 94 | std::string_view protocol = req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 95 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 96 | ws.set_option(boost::beast::websocket::stream_base::decorator( |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 97 | [session{session}, protocol{std::string(protocol)}]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | boost::beast::websocket::response_type& m) { |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 99 | |
| 100 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 101 | if (session != nullptr) |
| 102 | { |
| 103 | // use protocol for csrf checking |
Gunnar Mills | e628df8 | 2023-04-04 10:15:42 -0500 | [diff] [blame] | 104 | if (!crow::utility::constantTimeStringCompare( |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 105 | protocol, session->csrfToken)) |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 106 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 107 | BMCWEB_LOG_ERROR << "Websocket CSRF error"; |
| 108 | m.result(boost::beast::http::status::unauthorized); |
| 109 | return; |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 110 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 111 | } |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 112 | #endif |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 113 | if (!protocol.empty()) |
| 114 | { |
| 115 | m.insert(bf::sec_websocket_protocol, protocol); |
| 116 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 117 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 118 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 119 | "includeSubdomains; " |
| 120 | "preload"); |
| 121 | m.insert(bf::pragma, "no-cache"); |
| 122 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 123 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 124 | m.insert("X-XSS-Protection", "1; " |
| 125 | "mode=block"); |
| 126 | m.insert("X-Content-Type-Options", "nosniff"); |
| 127 | })); |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 128 | |
| 129 | // Perform the websocket upgrade |
| 130 | ws.async_accept(req, [this, self(shared_from_this())]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 131 | const boost::system::error_code& ec) { |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 132 | if (ec) |
| 133 | { |
| 134 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 135 | return; |
| 136 | } |
| 137 | acceptDone(); |
| 138 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 140 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 141 | void sendBinary(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | { |
| 143 | ws.binary(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 144 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 145 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 146 | doWrite(); |
| 147 | } |
| 148 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 149 | void sendEx(MessageType type, std::string_view msg, |
| 150 | std::function<void()>&& onDone) override |
| 151 | { |
| 152 | if (doingWrite) |
| 153 | { |
| 154 | BMCWEB_LOG_CRITICAL |
| 155 | << "Cannot mix sendEx usage with sendBinary or sendText"; |
| 156 | onDone(); |
| 157 | return; |
| 158 | } |
| 159 | ws.binary(type == MessageType::Binary); |
| 160 | |
| 161 | ws.async_write(boost::asio::buffer(msg), |
| 162 | [weak(weak_from_this()), onDone{std::move(onDone)}]( |
| 163 | const boost::beast::error_code& ec, size_t) { |
| 164 | std::shared_ptr<Connection> self = weak.lock(); |
| 165 | |
| 166 | // Call the done handler regardless of whether we |
| 167 | // errored, but before we close things out |
| 168 | onDone(); |
| 169 | |
| 170 | if (ec) |
| 171 | { |
| 172 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 173 | self->close("write error"); |
| 174 | } |
| 175 | }); |
| 176 | } |
| 177 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | void sendBinary(std::string&& msg) override |
| 179 | { |
| 180 | ws.binary(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 181 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 182 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | doWrite(); |
| 184 | } |
| 185 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 186 | void sendText(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 187 | { |
| 188 | ws.text(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 189 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 190 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 191 | doWrite(); |
| 192 | } |
| 193 | |
| 194 | void sendText(std::string&& msg) override |
| 195 | { |
| 196 | ws.text(true); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 197 | outBuffer.commit(boost::asio::buffer_copy(outBuffer.prepare(msg.size()), |
| 198 | boost::asio::buffer(msg))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | doWrite(); |
| 200 | } |
| 201 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 202 | void close(std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | { |
| 204 | ws.async_close( |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 205 | {boost::beast::websocket::close_code::normal, msg}, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 206 | [self(shared_from_this())](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 207 | if (ec == boost::asio::error::operation_aborted) |
| 208 | { |
| 209 | return; |
| 210 | } |
| 211 | if (ec) |
| 212 | { |
| 213 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 214 | return; |
| 215 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | }); |
| 217 | } |
| 218 | |
| 219 | void acceptDone() |
| 220 | { |
| 221 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 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 | { |
| 260 | if (ec != boost::beast::websocket::error::closed) |
| 261 | { |
| 262 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 263 | } |
| 264 | if (closeHandler) |
| 265 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 266 | std::string reason{ws.reason().reason.c_str()}; |
| 267 | closeHandler(*this, reason); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 268 | } |
| 269 | return; |
| 270 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 271 | |
| 272 | handleMessage(bytesRead); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 273 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | void doWrite() |
| 276 | { |
| 277 | // If we're already doing a write, ignore the request, it will be picked |
| 278 | // up when the current write is complete |
| 279 | if (doingWrite) |
| 280 | { |
| 281 | return; |
| 282 | } |
| 283 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 284 | if (outBuffer.size() == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | { |
| 286 | // Done for now |
| 287 | return; |
| 288 | } |
| 289 | doingWrite = true; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 290 | ws.async_write(outBuffer.data(), [this, self(shared_from_this())]( |
| 291 | const boost::beast::error_code& ec, |
| 292 | size_t bytesSent) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 293 | doingWrite = false; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 294 | outBuffer.consume(bytesSent); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 295 | if (ec == boost::beast::websocket::error::closed) |
| 296 | { |
| 297 | // Do nothing here. doRead handler will call the |
| 298 | // closeHandler. |
| 299 | close("Write error"); |
| 300 | return; |
| 301 | } |
| 302 | if (ec) |
| 303 | { |
| 304 | BMCWEB_LOG_ERROR << "Error in ws.async_write " << ec; |
| 305 | return; |
| 306 | } |
| 307 | doWrite(); |
| 308 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | private: |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 312 | void handleMessage(size_t bytesRead) |
| 313 | { |
| 314 | if (messageExHandler) |
| 315 | { |
| 316 | // Note, because of the interactions with the read buffers, |
| 317 | // this message handler overrides the normal message handler |
| 318 | messageExHandler(*this, inString, MessageType::Binary, |
| 319 | [this, self(shared_from_this()), bytesRead]() { |
| 320 | if (self == nullptr) |
| 321 | { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | inBuffer.consume(bytesRead); |
| 326 | inString.clear(); |
| 327 | |
| 328 | doRead(); |
| 329 | }); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (messageHandler) |
| 334 | { |
| 335 | messageHandler(*this, inString, ws.got_text()); |
| 336 | } |
| 337 | inBuffer.consume(bytesRead); |
| 338 | inString.clear(); |
| 339 | doRead(); |
| 340 | } |
| 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 |