blob: c0a4e504b3b16d3ec91d6cf4be3c7272b231a7fb [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:
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
Ed Tanous93cf0ac2024-03-28 00:35:13 -070034 void handleUpgrade(const Request& /*req*/,
Ed Tanous08bbe112023-04-06 13:10:02 -070035 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>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070042 std::move(adaptor), openHandler, closeHandler);
Ed Tanous08bbe112023-04-06 13:10:02 -070043 myConnection->start();
44 }
45#else
Ed Tanous93cf0ac2024-03-28 00:35:13 -070046 void handleUpgrade(const Request& /*req*/,
Ed Tanous08bbe112023-04-06 13:10:02 -070047 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>>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070055 std::move(adaptor), openHandler, closeHandler);
Ed Tanous08bbe112023-04-06 13:10:02 -070056 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