1 #pragma once 2 3 #include "baserule.hpp" 4 #include "http_request.hpp" 5 #include "http_response.hpp" 6 #include "server_sent_event.hpp" 7 8 #include <boost/beast/http/verb.hpp> 9 10 #include <functional> 11 #include <memory> 12 #include <string> 13 14 namespace crow 15 { 16 17 class SseSocketRule : public BaseRule 18 { 19 using self_t = SseSocketRule; 20 21 public: 22 explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) {} 23 24 void validate() override {} 25 26 void handle(const Request& /*req*/, 27 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 28 const std::vector<std::string>& /*params*/) override 29 { 30 asyncResp->res.result(boost::beast::http::status::not_found); 31 } 32 33 #ifndef BMCWEB_ENABLE_SSL 34 void handleUpgrade(const Request& /*req*/, 35 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 36 boost::asio::ip::tcp::socket&& adaptor) override 37 { 38 std::shared_ptr< 39 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>> 40 myConnection = std::make_shared< 41 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>( 42 std::move(adaptor), openHandler, closeHandler); 43 myConnection->start(); 44 } 45 #else 46 void handleUpgrade(const Request& /*req*/, 47 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 48 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& 49 adaptor) override 50 { 51 std::shared_ptr<crow::sse_socket::ConnectionImpl< 52 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>> 53 myConnection = std::make_shared<crow::sse_socket::ConnectionImpl< 54 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>( 55 std::move(adaptor), openHandler, closeHandler); 56 myConnection->start(); 57 } 58 #endif 59 60 template <typename Func> 61 self_t& onopen(Func f) 62 { 63 openHandler = f; 64 return *this; 65 } 66 67 template <typename Func> 68 self_t& onclose(Func f) 69 { 70 closeHandler = f; 71 return *this; 72 } 73 74 private: 75 std::function<void(crow::sse_socket::Connection&)> openHandler; 76 std::function<void(crow::sse_socket::Connection&)> closeHandler; 77 }; 78 79 } // namespace crow 80