108bbe119SEd Tanous #pragma once 208bbe119SEd Tanous 308bbe119SEd Tanous #include "baserule.hpp" 408bbe119SEd Tanous #include "http_request.hpp" 508bbe119SEd Tanous #include "http_response.hpp" 608bbe119SEd Tanous #include "server_sent_event.hpp" 708bbe119SEd Tanous 808bbe119SEd Tanous #include <boost/beast/http/verb.hpp> 908bbe119SEd Tanous 1008bbe119SEd Tanous #include <functional> 1108bbe119SEd Tanous #include <memory> 1208bbe119SEd Tanous #include <string> 1308bbe119SEd Tanous 1408bbe119SEd Tanous namespace crow 1508bbe119SEd Tanous { 1608bbe119SEd Tanous 1708bbe119SEd Tanous class SseSocketRule : public BaseRule 1808bbe119SEd Tanous { 1908bbe119SEd Tanous using self_t = SseSocketRule; 2008bbe119SEd Tanous 2108bbe119SEd Tanous public: SseSocketRule(const std::string & ruleIn)22*a3b9eb98SEd Tanous explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) 23*a3b9eb98SEd Tanous { 24*a3b9eb98SEd Tanous isUpgrade = true; 25*a3b9eb98SEd Tanous // Clear GET handler 26*a3b9eb98SEd Tanous methodsBitfield = 0; 27*a3b9eb98SEd Tanous } 2808bbe119SEd Tanous validate()2908bbe119SEd Tanous void validate() override {} 3008bbe119SEd Tanous handle(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::vector<std::string> &)3108bbe119SEd Tanous void handle(const Request& /*req*/, 3208bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3308bbe119SEd Tanous const std::vector<std::string>& /*params*/) override 3408bbe119SEd Tanous { 35*a3b9eb98SEd Tanous BMCWEB_LOG_ERROR( 36*a3b9eb98SEd Tanous "Handle called on websocket rule. This should never happen"); 37*a3b9eb98SEd Tanous asyncResp->res.result( 38*a3b9eb98SEd Tanous boost::beast::http::status::internal_server_error); 3908bbe119SEd Tanous } 4008bbe119SEd Tanous handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> &,boost::asio::ip::tcp::socket && adaptor)4193cf0ac2SEd Tanous void handleUpgrade(const Request& /*req*/, 4208bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 4308bbe119SEd Tanous boost::asio::ip::tcp::socket&& adaptor) override 4408bbe119SEd Tanous { 4508bbe119SEd Tanous std::shared_ptr< 4608bbe119SEd Tanous crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>> 4708bbe119SEd Tanous myConnection = std::make_shared< 4808bbe119SEd Tanous crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>( 4993cf0ac2SEd Tanous std::move(adaptor), openHandler, closeHandler); 5008bbe119SEd Tanous myConnection->start(); 5108bbe119SEd Tanous } handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> &,boost::asio::ssl::stream<boost::asio::ip::tcp::socket> && adaptor)5293cf0ac2SEd Tanous void handleUpgrade(const Request& /*req*/, 5308bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 54003301a2SEd Tanous boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& 5508bbe119SEd Tanous adaptor) override 5608bbe119SEd Tanous { 5708bbe119SEd Tanous std::shared_ptr<crow::sse_socket::ConnectionImpl< 58003301a2SEd Tanous boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>> 5908bbe119SEd Tanous myConnection = std::make_shared<crow::sse_socket::ConnectionImpl< 60003301a2SEd Tanous boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>( 6193cf0ac2SEd Tanous std::move(adaptor), openHandler, closeHandler); 6208bbe119SEd Tanous myConnection->start(); 6308bbe119SEd Tanous } 6408bbe119SEd Tanous 6508bbe119SEd Tanous template <typename Func> onopen(Func f)6608bbe119SEd Tanous self_t& onopen(Func f) 6708bbe119SEd Tanous { 6808bbe119SEd Tanous openHandler = f; 6908bbe119SEd Tanous return *this; 7008bbe119SEd Tanous } 7108bbe119SEd Tanous 7208bbe119SEd Tanous template <typename Func> onclose(Func f)7308bbe119SEd Tanous self_t& onclose(Func f) 7408bbe119SEd Tanous { 7508bbe119SEd Tanous closeHandler = f; 7608bbe119SEd Tanous return *this; 7708bbe119SEd Tanous } 7808bbe119SEd Tanous 7908bbe119SEd Tanous private: 8008bbe119SEd Tanous std::function<void(crow::sse_socket::Connection&)> openHandler; 8108bbe119SEd Tanous std::function<void(crow::sse_socket::Connection&)> closeHandler; 8208bbe119SEd Tanous }; 8308bbe119SEd Tanous 8408bbe119SEd Tanous } // namespace crow 85