From 8f79c5b6a2d53d7b2ea44a2a540369cb191a3b14 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Tue, 30 Jan 2024 15:56:37 -0800 Subject: Add unit test for SSE Writing this test exposed some bugs in SSE that got merged. sendSSEHeader was never called, leading to a connection that starts and immediately closes with no error code. This issue has been corrected in code, such that the sockets start. To allow for unit tests, the io_service needs to be passed into the class, previously, the SSE connection was pulling the io_context from the DBus connection, which is odd, given that the SSE connection has no other dependencies on DBus. Unit tests should help keep it working. Tested: Unit tests pass. Change-Id: I48080d2a94b6349989f556cd1c7b103bad498526 Signed-off-by: Ed Tanous --- http/routing/sserule.hpp | 8 +++--- http/server_sent_event.hpp | 68 ++++++++++++++++++++++------------------------ 2 files changed, 36 insertions(+), 40 deletions(-) (limited to 'http') diff --git a/http/routing/sserule.hpp b/http/routing/sserule.hpp index c0a4e504b3..1a422a8268 100644 --- a/http/routing/sserule.hpp +++ b/http/routing/sserule.hpp @@ -31,7 +31,7 @@ class SseSocketRule : public BaseRule } #ifndef BMCWEB_ENABLE_SSL - void handleUpgrade(const Request& /*req*/, + void handleUpgrade(const Request& req, const std::shared_ptr& /*asyncResp*/, boost::asio::ip::tcp::socket&& adaptor) override { @@ -39,11 +39,11 @@ class SseSocketRule : public BaseRule crow::sse_socket::ConnectionImpl> myConnection = std::make_shared< crow::sse_socket::ConnectionImpl>( - std::move(adaptor), openHandler, closeHandler); + *req.ioService, std::move(adaptor), openHandler, closeHandler); myConnection->start(); } #else - void handleUpgrade(const Request& /*req*/, + void handleUpgrade(const Request& req, const std::shared_ptr& /*asyncResp*/, boost::beast::ssl_stream&& adaptor) override @@ -52,7 +52,7 @@ class SseSocketRule : public BaseRule boost::beast::ssl_stream>> myConnection = std::make_shared>>( - std::move(adaptor), openHandler, closeHandler); + *req.ioService, std::move(adaptor), openHandler, closeHandler); myConnection->start(); } #endif diff --git a/http/server_sent_event.hpp b/http/server_sent_event.hpp index f4ad21a69f..730bdce51f 100644 --- a/http/server_sent_event.hpp +++ b/http/server_sent_event.hpp @@ -1,20 +1,16 @@ #pragma once -#include "dbus_singleton.hpp" #include "http_request.hpp" #include "http_response.hpp" #include #include #include -#include #include #include +#include #include - -#ifdef BMCWEB_ENABLE_SSL -#include -#endif +#include namespace crow { @@ -41,12 +37,13 @@ template class ConnectionImpl : public Connection { public: - ConnectionImpl(Adaptor&& adaptorIn, + ConnectionImpl(boost::asio::io_context& ioIn, Adaptor&& adaptorIn, std::function openHandlerIn, std::function closeHandlerIn) : adaptor(std::move(adaptorIn)), - timer(ioc), openHandler(std::move(openHandlerIn)), + ioc(ioIn), timer(ioc), openHandler(std::move(openHandlerIn)), closeHandler(std::move(closeHandlerIn)) + { BMCWEB_LOG_DEBUG("SseConnectionImpl: SSE constructor {}", logPtr(this)); } @@ -75,10 +72,12 @@ class ConnectionImpl : public Connection return; } openHandler(*this); + sendSSEHeader(); } void close(const std::string_view msg) override { + BMCWEB_LOG_DEBUG("Closing connection with reason {}", msg); // send notification to handler for cleanup if (closeHandler) { @@ -91,13 +90,10 @@ class ConnectionImpl : public Connection void sendSSEHeader() { BMCWEB_LOG_DEBUG("Starting SSE connection"); - using BodyType = boost::beast::http::buffer_body; - boost::beast::http::response res( - boost::beast::http::status::ok, 11, BodyType{}); + res.set(boost::beast::http::field::content_type, "text/event-stream"); - res.body().more = true; boost::beast::http::response_serializer& serial = - serializer.emplace(std::move(res)); + serializer.emplace(res); boost::beast::http::async_write_header( adaptor, serial, @@ -118,19 +114,17 @@ class ConnectionImpl : public Connection } BMCWEB_LOG_DEBUG("SSE header sent - Connection established"); - serializer.reset(); - // SSE stream header sent, So let us setup monitor. // Any read data on this stream will be error in case of SSE. - - adaptor.async_wait(boost::asio::ip::tcp::socket::wait_error, - std::bind_front(&ConnectionImpl::afterReadError, - this, shared_from_this())); + adaptor.async_read_some(boost::asio::buffer(buffer), + std::bind_front(&ConnectionImpl::afterReadError, + this, shared_from_this())); } void afterReadError(const std::shared_ptr& /*self*/, - const boost::system::error_code& ec) + const boost::system::error_code& ec, size_t bytesRead) { + BMCWEB_LOG_DEBUG("Read {}", bytesRead); if (ec == boost::asio::error::operation_aborted) { return; @@ -160,18 +154,13 @@ class ConnectionImpl : public Connection adaptor.async_write_some( inputBuffer.data(), std::bind_front(&ConnectionImpl::doWriteCallback, this, - weak_from_this())); + shared_from_this())); } - void doWriteCallback(const std::weak_ptr& weak, + void doWriteCallback(const std::shared_ptr& /*self*/, const boost::beast::error_code& ec, size_t bytesTransferred) { - auto self = weak.lock(); - if (self == nullptr) - { - return; - } timer.cancel(); doingWrite = false; inputBuffer.consume(bytesTransferred); @@ -210,6 +199,13 @@ class ConnectionImpl : public Connection void dataFormat(std::string_view id, std::string_view msg) { + constexpr size_t bufferLimit = 10485760U; // 10MB + if (id.size() + msg.size() + inputBuffer.size() >= bufferLimit) + { + BMCWEB_LOG_ERROR("SSE Buffer overflow while waiting for client"); + close("Buffer overflow"); + return; + } std::string rawData; if (!id.empty()) { @@ -255,7 +251,7 @@ class ConnectionImpl : public Connection if (ec == boost::asio::error::operation_aborted) { - BMCWEB_LOG_DEBUG("operation aborted"); + BMCWEB_LOG_DEBUG("Timer operation aborted"); // Canceled wait means the path succeeded. return; } @@ -264,22 +260,22 @@ class ConnectionImpl : public Connection BMCWEB_LOG_CRITICAL("{} timer failed {}", logPtr(self.get()), ec); } - BMCWEB_LOG_WARNING("{}Connection timed out, closing", + BMCWEB_LOG_WARNING("{} Connection timed out, closing", logPtr(self.get())); self->close("closing connection"); } private: - Adaptor adaptor; - + std::array buffer{}; boost::beast::multi_buffer inputBuffer; - std::optional> - serializer; - boost::asio::io_context& ioc = - crow::connections::systemBus->get_io_context(); + Adaptor adaptor; + + using BodyType = bmcweb::FileBody; + boost::beast::http::response res; + std::optional> serializer; + boost::asio::io_context& ioc; boost::asio::steady_timer timer; bool doingWrite = false; -- cgit v1.2.3