blob: 31db6fffe8f36c336d507e4e7cfa3b7a5c644450 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
V-Sanjana88ada3b2023-04-13 15:18:31 +05303#pragma once
Ed Tanous41fe81c2024-09-02 15:08:41 -07004#include "boost_formatters.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08005#include "http_body.hpp"
V-Sanjana88ada3b2023-04-13 15:18:31 +05306#include "http_request.hpp"
Ed Tanousd98a2f92025-02-06 17:36:31 -08007#include "io_context_singleton.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08008#include "logging.hpp"
V-Sanjana88ada3b2023-04-13 15:18:31 +05309
V-Sanjana88ada3b2023-04-13 15:18:31 +053010#include <boost/asio/buffer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080011#include <boost/asio/error.hpp>
V-Sanjana88ada3b2023-04-13 15:18:31 +053012#include <boost/asio/steady_timer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080013#include <boost/beast/core/error.hpp>
V-Sanjana88ada3b2023-04-13 15:18:31 +053014#include <boost/beast/core/multi_buffer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <boost/beast/http/field.hpp>
16#include <boost/beast/http/serializer.hpp>
17#include <boost/beast/http/write.hpp>
V-Sanjana88ada3b2023-04-13 15:18:31 +053018
19#include <array>
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <chrono>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080021#include <cstddef>
V-Sanjana88ada3b2023-04-13 15:18:31 +053022#include <functional>
Ed Tanousd7857202025-01-28 15:32:26 -080023#include <memory>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080024#include <optional>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <string>
26#include <string_view>
27#include <utility>
V-Sanjana88ada3b2023-04-13 15:18:31 +053028
29namespace crow
30{
31
32namespace sse_socket
33{
Ed Tanous93cf0ac2024-03-28 00:35:13 -070034struct Connection : public std::enable_shared_from_this<Connection>
V-Sanjana88ada3b2023-04-13 15:18:31 +053035{
36 public:
Ed Tanous6fde95f2023-06-01 07:33:34 -070037 Connection() = default;
V-Sanjana88ada3b2023-04-13 15:18:31 +053038
39 Connection(const Connection&) = delete;
40 Connection(Connection&&) = delete;
41 Connection& operator=(const Connection&) = delete;
42 Connection& operator=(const Connection&&) = delete;
43 virtual ~Connection() = default;
44
V-Sanjana88ada3b2023-04-13 15:18:31 +053045 virtual void close(std::string_view msg = "quit") = 0;
Ed Tanous6d799e12024-09-11 14:33:37 -070046 virtual void sendSseEvent(std::string_view id, std::string_view msg) = 0;
V-Sanjana88ada3b2023-04-13 15:18:31 +053047};
48
49template <typename Adaptor>
50class ConnectionImpl : public Connection
51{
52 public:
Ed Tanousf80a87f2024-06-16 12:10:33 -070053 ConnectionImpl(
54 Adaptor&& adaptorIn,
55 std::function<void(Connection&, const Request&)> openHandlerIn,
56 std::function<void(Connection&)> closeHandlerIn) :
Ed Tanousd98a2f92025-02-06 17:36:31 -080057 adaptor(std::move(adaptorIn)), timer(getIoContext()),
Ed Tanous93cf0ac2024-03-28 00:35:13 -070058 openHandler(std::move(openHandlerIn)),
V-Sanjana88ada3b2023-04-13 15:18:31 +053059 closeHandler(std::move(closeHandlerIn))
Ed Tanous8f79c5b2024-01-30 15:56:37 -080060
V-Sanjana88ada3b2023-04-13 15:18:31 +053061 {
Ed Tanous62598e32023-07-17 17:06:25 -070062 BMCWEB_LOG_DEBUG("SseConnectionImpl: SSE constructor {}", logPtr(this));
V-Sanjana88ada3b2023-04-13 15:18:31 +053063 }
64
65 ConnectionImpl(const ConnectionImpl&) = delete;
66 ConnectionImpl(const ConnectionImpl&&) = delete;
67 ConnectionImpl& operator=(const ConnectionImpl&) = delete;
68 ConnectionImpl& operator=(const ConnectionImpl&&) = delete;
69
70 ~ConnectionImpl() override
71 {
Ed Tanous62598e32023-07-17 17:06:25 -070072 BMCWEB_LOG_DEBUG("SSE ConnectionImpl: SSE destructor {}", logPtr(this));
V-Sanjana88ada3b2023-04-13 15:18:31 +053073 }
74
Ed Tanousf80a87f2024-06-16 12:10:33 -070075 void start(const Request& req)
V-Sanjana88ada3b2023-04-13 15:18:31 +053076 {
Ed Tanous464924c2024-12-20 14:49:45 -080077 BMCWEB_LOG_DEBUG("Starting SSE connection");
78
79 res.set(boost::beast::http::field::content_type, "text/event-stream");
80 boost::beast::http::response_serializer<BodyType>& serial =
81 serializer.emplace(res);
82
83 boost::beast::http::async_write_header(
84 adaptor, serial,
85 std::bind_front(&ConnectionImpl::sendSSEHeaderCallback, this,
86 shared_from_this(), req));
V-Sanjana88ada3b2023-04-13 15:18:31 +053087 }
88
89 void close(const std::string_view msg) override
90 {
Ed Tanous8f79c5b2024-01-30 15:56:37 -080091 BMCWEB_LOG_DEBUG("Closing connection with reason {}", msg);
V-Sanjana88ada3b2023-04-13 15:18:31 +053092 // send notification to handler for cleanup
93 if (closeHandler)
94 {
Ed Tanous6fde95f2023-06-01 07:33:34 -070095 closeHandler(*this);
V-Sanjana88ada3b2023-04-13 15:18:31 +053096 }
Ed Tanous62598e32023-07-17 17:06:25 -070097 BMCWEB_LOG_DEBUG("Closing SSE connection {} - {}", logPtr(this), msg);
Ed Tanous6fde95f2023-06-01 07:33:34 -070098 boost::beast::get_lowest_layer(adaptor).close();
V-Sanjana88ada3b2023-04-13 15:18:31 +053099 }
100
V-Sanjana88ada3b2023-04-13 15:18:31 +0530101 void sendSSEHeaderCallback(const std::shared_ptr<Connection>& /*self*/,
Ed Tanous464924c2024-12-20 14:49:45 -0800102 const Request& req,
Ed Tanous6fde95f2023-06-01 07:33:34 -0700103 const boost::system::error_code& ec,
104 size_t /*bytesSent*/)
V-Sanjana88ada3b2023-04-13 15:18:31 +0530105 {
Ed Tanous6fde95f2023-06-01 07:33:34 -0700106 serializer.reset();
V-Sanjana88ada3b2023-04-13 15:18:31 +0530107 if (ec)
108 {
Ed Tanous62598e32023-07-17 17:06:25 -0700109 BMCWEB_LOG_ERROR("Error sending header{}", ec);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530110 close("async_write_header failed");
111 return;
112 }
Ed Tanous62598e32023-07-17 17:06:25 -0700113 BMCWEB_LOG_DEBUG("SSE header sent - Connection established");
Ed Tanous464924c2024-12-20 14:49:45 -0800114 if (!openHandler)
115 {
116 BMCWEB_LOG_CRITICAL("No open handler???");
117 return;
118 }
119 openHandler(*this, req);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530120
V-Sanjana88ada3b2023-04-13 15:18:31 +0530121 // SSE stream header sent, So let us setup monitor.
122 // Any read data on this stream will be error in case of SSE.
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800123 adaptor.async_read_some(boost::asio::buffer(buffer),
124 std::bind_front(&ConnectionImpl::afterReadError,
125 this, shared_from_this()));
V-Sanjana88ada3b2023-04-13 15:18:31 +0530126 }
127
Ed Tanous6fde95f2023-06-01 07:33:34 -0700128 void afterReadError(const std::shared_ptr<Connection>& /*self*/,
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800129 const boost::system::error_code& ec, size_t bytesRead)
V-Sanjana88ada3b2023-04-13 15:18:31 +0530130 {
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800131 BMCWEB_LOG_DEBUG("Read {}", bytesRead);
Ed Tanous6fde95f2023-06-01 07:33:34 -0700132 if (ec == boost::asio::error::operation_aborted)
133 {
134 return;
135 }
V-Sanjana88ada3b2023-04-13 15:18:31 +0530136 if (ec)
137 {
Ed Tanous62598e32023-07-17 17:06:25 -0700138 BMCWEB_LOG_ERROR("Read error: {}", ec);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530139 }
140
Ed Tanous6fde95f2023-06-01 07:33:34 -0700141 close("Close SSE connection");
V-Sanjana88ada3b2023-04-13 15:18:31 +0530142 }
143
144 void doWrite()
145 {
V-Sanjana88ada3b2023-04-13 15:18:31 +0530146 if (doingWrite)
147 {
148 return;
149 }
150 if (inputBuffer.size() == 0)
151 {
Ed Tanous62598e32023-07-17 17:06:25 -0700152 BMCWEB_LOG_DEBUG("inputBuffer is empty... Bailing out");
V-Sanjana88ada3b2023-04-13 15:18:31 +0530153 return;
154 }
Ed Tanous6fde95f2023-06-01 07:33:34 -0700155 startTimeout();
V-Sanjana88ada3b2023-04-13 15:18:31 +0530156 doingWrite = true;
157
158 adaptor.async_write_some(
159 inputBuffer.data(),
160 std::bind_front(&ConnectionImpl::doWriteCallback, this,
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800161 shared_from_this()));
V-Sanjana88ada3b2023-04-13 15:18:31 +0530162 }
163
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800164 void doWriteCallback(const std::shared_ptr<Connection>& /*self*/,
V-Sanjana88ada3b2023-04-13 15:18:31 +0530165 const boost::beast::error_code& ec,
Ed Tanous6fde95f2023-06-01 07:33:34 -0700166 size_t bytesTransferred)
V-Sanjana88ada3b2023-04-13 15:18:31 +0530167 {
Ed Tanous6fde95f2023-06-01 07:33:34 -0700168 timer.cancel();
V-Sanjana88ada3b2023-04-13 15:18:31 +0530169 doingWrite = false;
170 inputBuffer.consume(bytesTransferred);
171
172 if (ec == boost::asio::error::eof)
173 {
Ed Tanous62598e32023-07-17 17:06:25 -0700174 BMCWEB_LOG_ERROR("async_write_some() SSE stream closed");
V-Sanjana88ada3b2023-04-13 15:18:31 +0530175 close("SSE stream closed");
176 return;
177 }
178
179 if (ec)
180 {
Ed Tanous62598e32023-07-17 17:06:25 -0700181 BMCWEB_LOG_ERROR("async_write_some() failed: {}", ec.message());
V-Sanjana88ada3b2023-04-13 15:18:31 +0530182 close("async_write_some failed");
183 return;
184 }
Ed Tanous62598e32023-07-17 17:06:25 -0700185 BMCWEB_LOG_DEBUG("async_write_some() bytes transferred: {}",
186 bytesTransferred);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530187
188 doWrite();
189 }
190
Ed Tanous6d799e12024-09-11 14:33:37 -0700191 void sendSseEvent(std::string_view id, std::string_view msg) override
V-Sanjana88ada3b2023-04-13 15:18:31 +0530192 {
193 if (msg.empty())
194 {
Ed Tanous62598e32023-07-17 17:06:25 -0700195 BMCWEB_LOG_DEBUG("Empty data, bailing out.");
V-Sanjana88ada3b2023-04-13 15:18:31 +0530196 return;
197 }
198
Ed Tanous6fde95f2023-06-01 07:33:34 -0700199 dataFormat(id, msg);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530200
201 doWrite();
202 }
203
Ed Tanous6fde95f2023-06-01 07:33:34 -0700204 void dataFormat(std::string_view id, std::string_view msg)
V-Sanjana88ada3b2023-04-13 15:18:31 +0530205 {
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800206 constexpr size_t bufferLimit = 10485760U; // 10MB
207 if (id.size() + msg.size() + inputBuffer.size() >= bufferLimit)
208 {
209 BMCWEB_LOG_ERROR("SSE Buffer overflow while waiting for client");
210 close("Buffer overflow");
211 return;
212 }
V-Sanjana88ada3b2023-04-13 15:18:31 +0530213 std::string rawData;
214 if (!id.empty())
215 {
216 rawData += "id: ";
Ed Tanous6fde95f2023-06-01 07:33:34 -0700217 rawData.append(id);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530218 rawData += "\n";
219 }
220
221 rawData += "data: ";
222 for (char character : msg)
223 {
224 rawData += character;
225 if (character == '\n')
226 {
227 rawData += "data: ";
228 }
229 }
230 rawData += "\n\n";
231
Ed Tanous44106f32024-04-06 13:48:50 -0700232 size_t copied = boost::asio::buffer_copy(
233 inputBuffer.prepare(rawData.size()), boost::asio::buffer(rawData));
234 inputBuffer.commit(copied);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530235 }
236
Ed Tanous6fde95f2023-06-01 07:33:34 -0700237 void startTimeout()
V-Sanjana88ada3b2023-04-13 15:18:31 +0530238 {
V-Sanjana88ada3b2023-04-13 15:18:31 +0530239 std::weak_ptr<Connection> weakSelf = weak_from_this();
240 timer.expires_after(std::chrono::seconds(30));
241 timer.async_wait(std::bind_front(&ConnectionImpl::onTimeoutCallback,
242 this, weak_from_this()));
243 }
244
245 void onTimeoutCallback(const std::weak_ptr<Connection>& weakSelf,
Ed Tanous6fde95f2023-06-01 07:33:34 -0700246 const boost::system::error_code& ec)
V-Sanjana88ada3b2023-04-13 15:18:31 +0530247 {
248 std::shared_ptr<Connection> self = weakSelf.lock();
249 if (!self)
250 {
Ed Tanous62598e32023-07-17 17:06:25 -0700251 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
252 logPtr(self.get()));
V-Sanjana88ada3b2023-04-13 15:18:31 +0530253 return;
254 }
255
256 if (ec == boost::asio::error::operation_aborted)
257 {
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800258 BMCWEB_LOG_DEBUG("Timer operation aborted");
Ed Tanous8ece0e42024-01-02 13:16:50 -0800259 // Canceled wait means the path succeeded.
V-Sanjana88ada3b2023-04-13 15:18:31 +0530260 return;
261 }
262 if (ec)
263 {
Ed Tanous62598e32023-07-17 17:06:25 -0700264 BMCWEB_LOG_CRITICAL("{} timer failed {}", logPtr(self.get()), ec);
V-Sanjana88ada3b2023-04-13 15:18:31 +0530265 }
266
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800267 BMCWEB_LOG_WARNING("{} Connection timed out, closing",
Ed Tanous62598e32023-07-17 17:06:25 -0700268 logPtr(self.get()));
V-Sanjana88ada3b2023-04-13 15:18:31 +0530269
270 self->close("closing connection");
271 }
272
273 private:
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800274 std::array<char, 1> buffer{};
V-Sanjana88ada3b2023-04-13 15:18:31 +0530275 boost::beast::multi_buffer inputBuffer;
276
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800277 Adaptor adaptor;
278
Ed Tanousb2896142024-01-31 15:25:47 -0800279 using BodyType = bmcweb::HttpBody;
Ed Tanous8f79c5b2024-01-30 15:56:37 -0800280 boost::beast::http::response<BodyType> res;
281 std::optional<boost::beast::http::response_serializer<BodyType>> serializer;
Ed Tanous6fde95f2023-06-01 07:33:34 -0700282 boost::asio::steady_timer timer;
V-Sanjana88ada3b2023-04-13 15:18:31 +0530283 bool doingWrite = false;
V-Sanjana88ada3b2023-04-13 15:18:31 +0530284
Ed Tanousf80a87f2024-06-16 12:10:33 -0700285 std::function<void(Connection&, const Request&)> openHandler;
Ed Tanous6fde95f2023-06-01 07:33:34 -0700286 std::function<void(Connection&)> closeHandler;
V-Sanjana88ada3b2023-04-13 15:18:31 +0530287};
288} // namespace sse_socket
289} // namespace crow