blob: 1a51ab046c558c6dd69eec282f124afdfb15878b [file] [log] [blame]
Ed Tanous08bbe112023-04-06 13:10:02 -07001#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
14namespace crow
15{
16
17class SseSocketRule : public BaseRule
18{
19 using self_t = SseSocketRule;
20
21 public:
Ed Tanousa3b9eb92024-06-03 08:39:37 -070022 explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
23 {
24 isUpgrade = true;
25 // Clear GET handler
26 methodsBitfield = 0;
27 }
Ed Tanous08bbe112023-04-06 13:10:02 -070028
29 void validate() override {}
30
31 void handle(const Request& /*req*/,
32 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33 const std::vector<std::string>& /*params*/) override
34 {
Ed Tanousa3b9eb92024-06-03 08:39:37 -070035 BMCWEB_LOG_ERROR(
36 "Handle called on websocket rule. This should never happen");
37 asyncResp->res.result(
38 boost::beast::http::status::internal_server_error);
Ed Tanous08bbe112023-04-06 13:10:02 -070039 }
40
Ed Tanousf80a87f2024-06-16 12:10:33 -070041 void handleUpgrade(const Request& req,
Ed Tanous08bbe112023-04-06 13:10:02 -070042 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
43 boost::asio::ip::tcp::socket&& adaptor) override
44 {
45 std::shared_ptr<
46 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>
47 myConnection = std::make_shared<
48 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070049 std::move(adaptor), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070050 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070051 }
Ed Tanousf80a87f2024-06-16 12:10:33 -070052 void handleUpgrade(const Request& req,
Ed Tanous08bbe112023-04-06 13:10:02 -070053 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous003301a2024-04-16 09:59:19 -070054 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
Ed Tanous08bbe112023-04-06 13:10:02 -070055 adaptor) override
56 {
57 std::shared_ptr<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070058 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070059 myConnection = std::make_shared<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070060 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070061 std::move(adaptor), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070062 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070063 }
Ed Tanous08bbe112023-04-06 13:10:02 -070064
65 template <typename Func>
66 self_t& onopen(Func f)
67 {
68 openHandler = f;
69 return *this;
70 }
71
72 template <typename Func>
73 self_t& onclose(Func f)
74 {
75 closeHandler = f;
76 return *this;
77 }
78
79 private:
Ed Tanousf80a87f2024-06-16 12:10:33 -070080 std::function<void(crow::sse_socket::Connection&, const crow::Request&)>
81 openHandler;
Ed Tanous08bbe112023-04-06 13:10:02 -070082 std::function<void(crow::sse_socket::Connection&)> closeHandler;
83};
84
85} // namespace crow