blob: e62943f160679ccf4aeea301782576872e3c609f [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:
Ed Tanousa3b9eb92024-06-03 08:39:37 -070019 explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
20 {
21 isUpgrade = true;
22 // Clear GET handler
23 methodsBitfield = 0;
24 }
Ed Tanous08bbe112023-04-06 13:10:02 -070025
26 void validate() override {}
27
28 void handle(const Request& /*req*/,
29 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30 const std::vector<std::string>& /*params*/) override
31 {
Ed Tanousa3b9eb92024-06-03 08:39:37 -070032 BMCWEB_LOG_ERROR(
33 "Handle called on websocket rule. This should never happen");
34 asyncResp->res.result(
35 boost::beast::http::status::internal_server_error);
Ed Tanous08bbe112023-04-06 13:10:02 -070036 }
37
Ed Tanous08bbe112023-04-06 13:10:02 -070038 void handleUpgrade(const Request& req,
39 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
40 boost::asio::ip::tcp::socket&& adaptor) override
41 {
Ed Tanous62598e32023-07-17 17:06:25 -070042 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070043 std::shared_ptr<
44 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
45 myConnection = std::make_shared<
46 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080047 req.url(), req.session, std::move(adaptor), openHandler,
48 messageHandler, messageExHandler, closeHandler, errorHandler);
49 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070050 }
Ed Tanous8db83742024-04-13 09:11:15 -070051
Ed Tanous08bbe112023-04-06 13:10:02 -070052 void handleUpgrade(const Request& req,
53 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 {
Ed Tanous62598e32023-07-17 17:06:25 -070057 BMCWEB_LOG_DEBUG("Websocket handles upgrade");
Ed Tanous08bbe112023-04-06 13:10:02 -070058 std::shared_ptr<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070059 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070060 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070061 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous5ebb9d32023-02-27 18:20:47 -080062 req.url(), req.session, std::move(adaptor), openHandler,
63 messageHandler, messageExHandler, closeHandler, errorHandler);
64 myConnection->start(req);
Ed Tanous08bbe112023-04-06 13:10:02 -070065 }
Ed Tanous08bbe112023-04-06 13:10:02 -070066
67 template <typename Func>
68 self_t& onopen(Func f)
69 {
70 openHandler = f;
71 return *this;
72 }
73
74 template <typename Func>
75 self_t& onmessage(Func f)
76 {
77 messageHandler = f;
78 return *this;
79 }
80
81 template <typename Func>
82 self_t& onmessageex(Func f)
83 {
84 messageExHandler = f;
85 return *this;
86 }
87
88 template <typename Func>
89 self_t& onclose(Func f)
90 {
91 closeHandler = f;
92 return *this;
93 }
94
95 template <typename Func>
96 self_t& onerror(Func f)
97 {
98 errorHandler = f;
99 return *this;
100 }
101
102 protected:
103 std::function<void(crow::websocket::Connection&)> openHandler;
104 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
105 messageHandler;
106 std::function<void(crow::websocket::Connection&, std::string_view,
107 crow::websocket::MessageType type,
108 std::function<void()>&& whenComplete)>
109 messageExHandler;
110 std::function<void(crow::websocket::Connection&, const std::string&)>
111 closeHandler;
112 std::function<void(crow::websocket::Connection&)> errorHandler;
113};
114} // namespace crow