V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 2 | #include "http_body.hpp" |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 3 | #include "http_request.hpp" |
| 4 | #include "http_response.hpp" |
| 5 | |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 6 | #include <boost/asio/buffer.hpp> |
| 7 | #include <boost/asio/steady_timer.hpp> |
| 8 | #include <boost/beast/core/multi_buffer.hpp> |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 9 | #include <boost/beast/websocket.hpp> |
| 10 | |
| 11 | #include <array> |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 12 | #include <cstddef> |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 13 | #include <functional> |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 14 | #include <optional> |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 15 | |
| 16 | namespace crow |
| 17 | { |
| 18 | |
| 19 | namespace sse_socket |
| 20 | { |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 21 | struct Connection : std::enable_shared_from_this<Connection> |
| 22 | { |
| 23 | public: |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 24 | Connection() = default; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 25 | |
| 26 | Connection(const Connection&) = delete; |
| 27 | Connection(Connection&&) = delete; |
| 28 | Connection& operator=(const Connection&) = delete; |
| 29 | Connection& operator=(const Connection&&) = delete; |
| 30 | virtual ~Connection() = default; |
| 31 | |
| 32 | virtual boost::asio::io_context& getIoContext() = 0; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 33 | virtual void close(std::string_view msg = "quit") = 0; |
| 34 | virtual void sendEvent(std::string_view id, std::string_view msg) = 0; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | template <typename Adaptor> |
| 38 | class ConnectionImpl : public Connection |
| 39 | { |
| 40 | public: |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 41 | ConnectionImpl(boost::asio::io_context& ioIn, Adaptor&& adaptorIn, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 42 | std::function<void(Connection&)> openHandlerIn, |
| 43 | std::function<void(Connection&)> closeHandlerIn) : |
| 44 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 45 | ioc(ioIn), timer(ioc), openHandler(std::move(openHandlerIn)), |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 46 | closeHandler(std::move(closeHandlerIn)) |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 47 | |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 48 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 49 | BMCWEB_LOG_DEBUG("SseConnectionImpl: SSE constructor {}", logPtr(this)); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | ConnectionImpl(const ConnectionImpl&) = delete; |
| 53 | ConnectionImpl(const ConnectionImpl&&) = delete; |
| 54 | ConnectionImpl& operator=(const ConnectionImpl&) = delete; |
| 55 | ConnectionImpl& operator=(const ConnectionImpl&&) = delete; |
| 56 | |
| 57 | ~ConnectionImpl() override |
| 58 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 59 | BMCWEB_LOG_DEBUG("SSE ConnectionImpl: SSE destructor {}", logPtr(this)); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | boost::asio::io_context& getIoContext() override |
| 63 | { |
| 64 | return static_cast<boost::asio::io_context&>( |
| 65 | adaptor.get_executor().context()); |
| 66 | } |
| 67 | |
| 68 | void start() |
| 69 | { |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 70 | if (!openHandler) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 71 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 72 | BMCWEB_LOG_CRITICAL("No open handler???"); |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 73 | return; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 74 | } |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 75 | openHandler(*this); |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 76 | sendSSEHeader(); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void close(const std::string_view msg) override |
| 80 | { |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 81 | BMCWEB_LOG_DEBUG("Closing connection with reason {}", msg); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 82 | // send notification to handler for cleanup |
| 83 | if (closeHandler) |
| 84 | { |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 85 | closeHandler(*this); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 86 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 87 | BMCWEB_LOG_DEBUG("Closing SSE connection {} - {}", logPtr(this), msg); |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 88 | boost::beast::get_lowest_layer(adaptor).close(); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 91 | void sendSSEHeader() |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 92 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 93 | BMCWEB_LOG_DEBUG("Starting SSE connection"); |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 94 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 95 | res.set(boost::beast::http::field::content_type, "text/event-stream"); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 96 | boost::beast::http::response_serializer<BodyType>& serial = |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 97 | serializer.emplace(res); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 98 | |
| 99 | boost::beast::http::async_write_header( |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 100 | adaptor, serial, |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 101 | std::bind_front(&ConnectionImpl::sendSSEHeaderCallback, this, |
| 102 | shared_from_this())); |
| 103 | } |
| 104 | |
| 105 | void sendSSEHeaderCallback(const std::shared_ptr<Connection>& /*self*/, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 106 | const boost::system::error_code& ec, |
| 107 | size_t /*bytesSent*/) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 108 | { |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 109 | serializer.reset(); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 110 | if (ec) |
| 111 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_ERROR("Error sending header{}", ec); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 113 | close("async_write_header failed"); |
| 114 | return; |
| 115 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 116 | BMCWEB_LOG_DEBUG("SSE header sent - Connection established"); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 117 | |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 118 | // SSE stream header sent, So let us setup monitor. |
| 119 | // Any read data on this stream will be error in case of SSE. |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 120 | adaptor.async_read_some(boost::asio::buffer(buffer), |
| 121 | std::bind_front(&ConnectionImpl::afterReadError, |
| 122 | this, shared_from_this())); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 125 | void afterReadError(const std::shared_ptr<Connection>& /*self*/, |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 126 | const boost::system::error_code& ec, size_t bytesRead) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 127 | { |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 128 | BMCWEB_LOG_DEBUG("Read {}", bytesRead); |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 129 | if (ec == boost::asio::error::operation_aborted) |
| 130 | { |
| 131 | return; |
| 132 | } |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 133 | if (ec) |
| 134 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 135 | BMCWEB_LOG_ERROR("Read error: {}", ec); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 136 | } |
| 137 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 138 | close("Close SSE connection"); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void doWrite() |
| 142 | { |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 143 | if (doingWrite) |
| 144 | { |
| 145 | return; |
| 146 | } |
| 147 | if (inputBuffer.size() == 0) |
| 148 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 149 | BMCWEB_LOG_DEBUG("inputBuffer is empty... Bailing out"); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 150 | return; |
| 151 | } |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 152 | startTimeout(); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 153 | doingWrite = true; |
| 154 | |
| 155 | adaptor.async_write_some( |
| 156 | inputBuffer.data(), |
| 157 | std::bind_front(&ConnectionImpl::doWriteCallback, this, |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 158 | shared_from_this())); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 159 | } |
| 160 | |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 161 | void doWriteCallback(const std::shared_ptr<Connection>& /*self*/, |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 162 | const boost::beast::error_code& ec, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 163 | size_t bytesTransferred) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 164 | { |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 165 | timer.cancel(); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 166 | doingWrite = false; |
| 167 | inputBuffer.consume(bytesTransferred); |
| 168 | |
| 169 | if (ec == boost::asio::error::eof) |
| 170 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 171 | BMCWEB_LOG_ERROR("async_write_some() SSE stream closed"); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 172 | close("SSE stream closed"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (ec) |
| 177 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 178 | BMCWEB_LOG_ERROR("async_write_some() failed: {}", ec.message()); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 179 | close("async_write_some failed"); |
| 180 | return; |
| 181 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 182 | BMCWEB_LOG_DEBUG("async_write_some() bytes transferred: {}", |
| 183 | bytesTransferred); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 184 | |
| 185 | doWrite(); |
| 186 | } |
| 187 | |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 188 | void sendEvent(std::string_view id, std::string_view msg) override |
| 189 | { |
| 190 | if (msg.empty()) |
| 191 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 192 | BMCWEB_LOG_DEBUG("Empty data, bailing out."); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 196 | dataFormat(id, msg); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 197 | |
| 198 | doWrite(); |
| 199 | } |
| 200 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 201 | void dataFormat(std::string_view id, std::string_view msg) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 202 | { |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 203 | constexpr size_t bufferLimit = 10485760U; // 10MB |
| 204 | if (id.size() + msg.size() + inputBuffer.size() >= bufferLimit) |
| 205 | { |
| 206 | BMCWEB_LOG_ERROR("SSE Buffer overflow while waiting for client"); |
| 207 | close("Buffer overflow"); |
| 208 | return; |
| 209 | } |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 210 | std::string rawData; |
| 211 | if (!id.empty()) |
| 212 | { |
| 213 | rawData += "id: "; |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 214 | rawData.append(id); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 215 | rawData += "\n"; |
| 216 | } |
| 217 | |
| 218 | rawData += "data: "; |
| 219 | for (char character : msg) |
| 220 | { |
| 221 | rawData += character; |
| 222 | if (character == '\n') |
| 223 | { |
| 224 | rawData += "data: "; |
| 225 | } |
| 226 | } |
| 227 | rawData += "\n\n"; |
| 228 | |
| 229 | boost::asio::buffer_copy(inputBuffer.prepare(rawData.size()), |
| 230 | boost::asio::buffer(rawData)); |
| 231 | inputBuffer.commit(rawData.size()); |
| 232 | } |
| 233 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 234 | void startTimeout() |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 235 | { |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 236 | std::weak_ptr<Connection> weakSelf = weak_from_this(); |
| 237 | timer.expires_after(std::chrono::seconds(30)); |
| 238 | timer.async_wait(std::bind_front(&ConnectionImpl::onTimeoutCallback, |
| 239 | this, weak_from_this())); |
| 240 | } |
| 241 | |
| 242 | void onTimeoutCallback(const std::weak_ptr<Connection>& weakSelf, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 243 | const boost::system::error_code& ec) |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 244 | { |
| 245 | std::shared_ptr<Connection> self = weakSelf.lock(); |
| 246 | if (!self) |
| 247 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 248 | BMCWEB_LOG_CRITICAL("{} Failed to capture connection", |
| 249 | logPtr(self.get())); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
| 253 | if (ec == boost::asio::error::operation_aborted) |
| 254 | { |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 255 | BMCWEB_LOG_DEBUG("Timer operation aborted"); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 256 | // Canceled wait means the path succeeded. |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | if (ec) |
| 260 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 261 | BMCWEB_LOG_CRITICAL("{} timer failed {}", logPtr(self.get()), ec); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 262 | } |
| 263 | |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 264 | BMCWEB_LOG_WARNING("{} Connection timed out, closing", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 265 | logPtr(self.get())); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 266 | |
| 267 | self->close("closing connection"); |
| 268 | } |
| 269 | |
| 270 | private: |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 271 | std::array<char, 1> buffer{}; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 272 | boost::beast::multi_buffer inputBuffer; |
| 273 | |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 274 | Adaptor adaptor; |
| 275 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 276 | using BodyType = bmcweb::HttpBody; |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 277 | boost::beast::http::response<BodyType> res; |
| 278 | std::optional<boost::beast::http::response_serializer<BodyType>> serializer; |
| 279 | boost::asio::io_context& ioc; |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 280 | boost::asio::steady_timer timer; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 281 | bool doingWrite = false; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 282 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 283 | std::function<void(Connection&)> openHandler; |
| 284 | std::function<void(Connection&)> closeHandler; |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 285 | }; |
| 286 | } // namespace sse_socket |
| 287 | } // namespace crow |