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