blob: 0905b08e2618e5f6925d3f285897fc76f4cfec07 [file] [log] [blame]
Ed Tanous08bbe112023-04-06 13:10:02 -07001#pragma once
2
3#include "baserule.hpp"
4#include "websocket.hpp"
5
6#include <boost/beast/http/verb.hpp>
7
8#include <memory>
9#include <string>
10#include <vector>
11
12namespace crow
13{
14class WebSocketRule : public BaseRule
15{
16 using self_t = WebSocketRule;
17
18 public:
19 explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) {}
20
21 void validate() override {}
22
23 void handle(const Request& /*req*/,
24 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25 const std::vector<std::string>& /*params*/) override
26 {
27 asyncResp->res.result(boost::beast::http::status::not_found);
28 }
29
Ed Tanous08bbe112023-04-06 13:10:02 -070030 void handleUpgrade(const Request& req,
31 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
32 boost::asio::ip::tcp::socket&& adaptor) override
33 {
Ed Tanous62598e32023-07-17 17:06:25 -070034 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070035 std::shared_ptr<
36 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
37 myConnection = std::make_shared<
38 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080039 req.url(), req.session, std::move(adaptor), openHandler,
40 messageHandler, messageExHandler, closeHandler, errorHandler);
41 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070042 }
Ed Tanous8db83742024-04-13 09:11:15 -070043
Ed Tanous08bbe112023-04-06 13:10:02 -070044 void handleUpgrade(const Request& req,
45 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous003301a2024-04-16 09:59:19 -070046 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
Ed Tanous08bbe112023-04-06 13:10:02 -070047 adaptor) override
48 {
Ed Tanous62598e32023-07-17 17:06:25 -070049 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070050 std::shared_ptr<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070051 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070052 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070053 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080054 req.url(), req.session, std::move(adaptor), openHandler,
55 messageHandler, messageExHandler, closeHandler, errorHandler);
56 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070057 }
Ed Tanous08bbe112023-04-06 13:10:02 -070058
59 template <typename Func>
60 self_t& onopen(Func f)
61 {
62 openHandler = f;
63 return *this;
64 }
65
66 template <typename Func>
67 self_t& onmessage(Func f)
68 {
69 messageHandler = f;
70 return *this;
71 }
72
73 template <typename Func>
74 self_t& onmessageex(Func f)
75 {
76 messageExHandler = f;
77 return *this;
78 }
79
80 template <typename Func>
81 self_t& onclose(Func f)
82 {
83 closeHandler = f;
84 return *this;
85 }
86
87 template <typename Func>
88 self_t& onerror(Func f)
89 {
90 errorHandler = f;
91 return *this;
92 }
93
94 protected:
95 std::function<void(crow::websocket::Connection&)> openHandler;
96 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
97 messageHandler;
98 std::function<void(crow::websocket::Connection&, std::string_view,
99 crow::websocket::MessageType type,
100 std::function<void()>&& whenComplete)>
101 messageExHandler;
102 std::function<void(crow::websocket::Connection&, const std::string&)>
103 closeHandler;
104 std::function<void(crow::websocket::Connection&)> errorHandler;
105};
106} // namespace crow