xref: /openbmc/bmcweb/http/routing/sserule.hpp (revision 003301a2)
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     void handleUpgrade(const Request& /*req*/,
34                        const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
35                        boost::asio::ip::tcp::socket&& adaptor) override
36     {
37         std::shared_ptr<
38             crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>
39             myConnection = std::make_shared<
40                 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
41                 std::move(adaptor), openHandler, closeHandler);
42         myConnection->start();
43     }
44     void handleUpgrade(const Request& /*req*/,
45                        const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
46                        boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
47                            adaptor) override
48     {
49         std::shared_ptr<crow::sse_socket::ConnectionImpl<
50             boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
51             myConnection = std::make_shared<crow::sse_socket::ConnectionImpl<
52                 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
53                 std::move(adaptor), openHandler, closeHandler);
54         myConnection->start();
55     }
56 
57     template <typename Func>
58     self_t& onopen(Func f)
59     {
60         openHandler = f;
61         return *this;
62     }
63 
64     template <typename Func>
65     self_t& onclose(Func f)
66     {
67         closeHandler = f;
68         return *this;
69     }
70 
71   private:
72     std::function<void(crow::sse_socket::Connection&)> openHandler;
73     std::function<void(crow::sse_socket::Connection&)> closeHandler;
74 };
75 
76 } // namespace crow
77